Skip to content

Commit

Permalink
Add descriptive statistics in output list from report
Browse files Browse the repository at this point in the history
  • Loading branch information
Matías Castillo Aguilar authored and Matías Castillo Aguilar committed Mar 31, 2021
1 parent 9825ebb commit f4b197b
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 18 deletions.
18 changes: 18 additions & 0 deletions R/bipair.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ bipair <- function(data
test <- stats::t.test(stats::as.formula(paste(variable, by, sep = '~')), data, paired = T)
d <- effectsize::effectsize(test, verbose = F)

for(j in levels(data[[by]])) {
result[['desc']][j] <- list(paste0(
'M = '
, round(mean(data[data[[by]] == j,][[variable]], na.rm = T),2), ', SD = '
, round(sd(data[data[[by]] == j,][[variable]], na.rm = T),2) ) ) }

if(markdown) {
result[['full']] <- paste0(
stats <- paste0('*t* ~Student~ (',round(test$parameter,1)
Expand Down Expand Up @@ -85,6 +91,12 @@ bipair <- function(data
, tr = trim
, nboot = nboot)['AKP',]

for(j in levels(data[[by]])) {
result[['desc']][j] <- list(paste0(
'M = '
, round(mean(data[data[[by]] == j,][[variable]], na.rm = T, trim = trim),2), ', SD = '
, round(sd(data[data[[by]] == j,][[variable]], na.rm = T),2) ) ) }

if(markdown) {
result[['full']] <- paste0(
stats <- paste0('*t* ~Yuen~ (', round(test$df, 1)
Expand Down Expand Up @@ -118,6 +130,12 @@ bipair <- function(data
r <- effectsize::rank_biserial(data[[variable]] ~ data[[by]], data = data,
paired = TRUE, verbose = FALSE)

for(j in levels(data[[by]])) {
result[['desc']][j] <- list(paste0(
'Mdn = '
, round(median(data[data[[by]] == j,][[variable]], na.rm = T),2), ', IQR = '
, round(IQR(data[data[[by]] == j,][[variable]], na.rm = T),2) ) ) }

if(markdown) {
result[['full']] <- paste0(
stats <- paste0('*V* = ', round(test$statistic,2)
Expand Down
20 changes: 19 additions & 1 deletion R/bitwo.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ bitwo <- function(data
type <- if(n.test) { c('check') } else { 'np' }
}
if(type %in% c('p','check')) {

for(j in levels(data[[by]])) {
result[['desc']][j] <- list(paste0(
'M = '
, round(mean(data[data[[by]] == j,][[variable]], na.rm = T),2), ', SD = '
, round(sd(data[data[[by]] == j,][[variable]], na.rm = T),2) ) ) }

if(type == 'check') {
# Prueba de Levene ----
hvar.test <- car::leveneTest(data[[variable]], data[[by]] )[1,3] > 0.05
Expand Down Expand Up @@ -130,6 +137,12 @@ bitwo <- function(data
, tr = trim
, nboot = nboot)

for(j in levels(data[[by]])) {
result[['desc']][j] <- list(paste0(
'M = '
, round(mean(data[data[[by]] == j,][[variable]], na.rm = T, trim = trim),2), ', SD = '
, round(sd(data[data[[by]] == j,][[variable]], na.rm = T),2) ) ) }

if(markdown) {
result[['full']] <- paste0(
stats <- paste0('*t* ~Yuen~ (', round(test$df, 2)
Expand Down Expand Up @@ -163,8 +176,13 @@ bitwo <- function(data
test <- stats::wilcox.test(stats::as.formula(paste(variable, by, sep = "~")), data, correct = T, exact = F, paired = F)
r <- effectsize::rank_biserial(data[[variable]] ~ data[[by]], data = data)

if(markdown) {
for(j in levels(data[[by]])) {
result[['desc']][j] <- list(paste0(
'Mdn = '
, round(median(data[data[[by]] == j,][[variable]], na.rm = T),2), ', IQR = '
, round(IQR(data[data[[by]] == j,][[variable]], na.rm = T),2) ) ) }

if(markdown) {
result[['full']] <- paste0(
stats <- paste0('*W* = ', round(test$statistic,2)
, ', *p* ', ifelse(test$p.value < 0.001, '< 0.001', paste(
Expand Down
19 changes: 19 additions & 0 deletions R/multgroup.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ multgroup <- function(data
type <- if(n.test) { 'check' } else { 'np' }
}
if(type %in% c('p','check')) {

for(j in levels(data[[by]])) {
result[['desc']][j] <- list(paste0(
'M = '
, round(mean(data[data[[by]] == j,][[variable]], na.rm = T),2), ', SD = '
, round(sd(data[data[[by]] == j,][[variable]], na.rm = T),2) ) ) }

if(type == 'check') {
# Prueba de Levene ----
hvar.test <- car::leveneTest(data[[variable]], data[[by]] )[1,3] > 0.05
Expand Down Expand Up @@ -151,6 +158,12 @@ multgroup <- function(data
, data = data
, tr = trim)

for(j in levels(data[[by]])) {
result[['desc']][j] <- list(paste0(
'M = '
, round(mean(data[data[[by]] == j,][[variable]], na.rm = T, trim = trim),2), ', SD = '
, round(sd(data[data[[by]] == j,][[variable]], na.rm = T),2) ) ) }

if(pairwise.comp) {
# Post-Hoc ----
result[['pwc.method']] <- "Yuen's test on trimmed means"
Expand Down Expand Up @@ -198,6 +211,12 @@ multgroup <- function(data
test <- stats::kruskal.test(stats::as.formula(paste(variable, by, sep = '~')), data)
epsilon <- effectsize::rank_epsilon_squared(data[[variable]] ~ data[[by]], data)

for(j in levels(data[[by]])) {
result[['desc']][j] <- list(paste0(
'Mdn = '
, round(median(data[data[[by]] == j,][[variable]], na.rm = T),2), ', IQR = '
, round(IQR(data[data[[by]] == j,][[variable]], na.rm = T),2) ) ) }

if(pairwise.comp) {
# Post-Hoc: Dunn test ----
result[['pwc.method']] <- "Dunn test"
Expand Down
19 changes: 19 additions & 0 deletions R/multpair.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ multpair <- function(data
type <- if(n.test) { 'check' } else { 'np' }
}
if(type %in% c('p','check')) {

for(j in levels(data[[by]])) {
result[['desc']][j] <- list(paste0(
'M = '
, round(mean(data[data[[by]] == j,][[variable]], na.rm = T),2), ', SD = '
, round(sd(data[data[[by]] == j,][[variable]], na.rm = T),2) ) ) }

# ANOVA, medidas repetidas ----
model <- afex::aov_ez(id = 'rowid',
dv = as.character(variable),
Expand Down Expand Up @@ -183,6 +190,12 @@ multpair <- function(data
blocks = data[['rowid']],
tr = trim)

for(j in levels(data[[by]])) {
result[['desc']][j] <- list(paste0(
'M = '
, round(mean(data[data[[by]] == j,][[variable]], na.rm = T, trim = trim),2), ', SD = '
, round(sd(data[data[[by]] == j,][[variable]], na.rm = T),2) ) ) }

# Tamaño de efecto no disponible para RMANOVA

if(pairwise.comp) {
Expand Down Expand Up @@ -224,6 +237,12 @@ multpair <- function(data
output <- stats::friedman.test(stats::as.formula(paste(paste(variable, by, sep = '~'),'| rowid')), data)
kendall <- effectsize::kendalls_w(as.character(variable), as.character(by), 'rowid', data)

for(j in levels(data[[by]])) {
result[['desc']][j] <- list(paste0(
'Mdn = '
, round(median(data[data[[by]] == j,][[variable]], na.rm = T),2), ', IQR = '
, round(IQR(data[data[[by]] == j,][[variable]], na.rm = T),2) ) ) }

if(pairwise.comp) {
# Post-Hoc: Durbin test ----
result[['pwc.method']] <- "Durbin-Conover test"
Expand Down
10 changes: 5 additions & 5 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ sticker(p, package="writR",
dpi = 600)
```

<img src="README_files/logo.png" width="300"/>

<table>
<tr>
<th> <img src="README_files/logo.png" width="300"/> </th>
<th width="20"></th>
<th> <h1 style="text-align:left;">writR: Inferential statistics and reporting in APA style</h1> </br>
<p style="text-align:center;">For automated and basic inferential testing.</p> </th>
</tr>
Expand Down Expand Up @@ -149,13 +149,13 @@ result

## Inline results in APA style

The core function: `report` by default return a list of length two in Markdown format (as seen before) for inline results. An example using same data as before:
The core function: `report` by default return a list of length five in Markdown format (as seen before) for inline results. An example using same data as before:

> The analysis of the effects of the treatment shows an statistically significant difference between the groups, `result$full`, evaluated through `result$method`.
> The analysis of the effects of the treatment shows that treatment group had greater weight (`result$desc$Treatment`) than the control group (`result$desc$Control`). Further analyses shows an statistically significant difference between the groups, `result$full`, evaluated through `result$method`.
translates into this:

> The analysis of the effects of the treatment shows an statistically significant difference between the groups, *t* <sub>Student</sub> (98) = 2.509, *p* = 0.014, *d* <sub>Cohen's</sub> = 0.51, CI<sub>95%</sub>[0.1, 0.91], evaluated through Student's t-test for independent samples.
> The analysis of the effects of the treatment shows that treatment group had greater weight (`r result$desc$Treatment`) than the control group (`r result$desc$Control`). Further analyses shows an statistically significant difference between the groups, *t* <sub>Student</sub> (98) = 2.509, *p* = 0.014, *d* <sub>Cohen's</sub> = 0.51, CI<sub>95%</sub>[0.1, 0.91], evaluated through Student's t-test for independent samples.
## Mixed effects ANOVA

Expand Down
56 changes: 51 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

<img src="README_files/logo.png" width="300"/>

<table>
<tr>
<th> <img src="README_files/logo.png" width="300"/> </th>
<th width="20"></th>
<th> <h1 style="text-align:left;">writR: Inferential statistics and reporting in APA style</h1> </br>
<p style="text-align:center;">For automated and basic inferential testing.</p> </th>
</tr>
Expand Down Expand Up @@ -115,6 +115,14 @@ result
```

```
## $desc
## $desc$Treatment
## [1] "M = 70.24, SD = 6.48"
##
## $desc$Control
## [1] "M = 67.02, SD = 6.34"
##
##
## $full
## [1] "*t* ~Student~ (98) = 2.51, *p* = 0.014, *d* ~Cohen's~ = 0.51, CI~95%~[0.1, 0.91]"
##
Expand All @@ -130,13 +138,13 @@ result

## Inline results in APA style

The core function: `report` by default return a list of length two in Markdown format (as seen before) for inline results. An example using same data as before:
The core function: `report` by default return a list of length five in Markdown format (as seen before) for inline results. An example using same data as before:

> The analysis of the effects of the treatment shows an statistically significant difference between the groups, `result$full`, evaluated through `result$method`.
> The analysis of the effects of the treatment shows that treatment group had greater weight (`result$desc$Treatment`) than the control group (`result$desc$Control`). Further analyses shows an statistically significant difference between the groups, `result$full`, evaluated through `result$method`.
translates into this:

> The analysis of the effects of the treatment shows an statistically significant difference between the groups, *t* <sub>Student</sub> (98) = 2.509, *p* = 0.014, *d* <sub>Cohen's</sub> = 0.51, CI<sub>95%</sub>[0.1, 0.91], evaluated through Student's t-test for independent samples.
> The analysis of the effects of the treatment shows that treatment group had greater weight (M = 70.24, SD = 6.48) than the control group (M = 67.02, SD = 6.34). Further analyses shows an statistically significant difference between the groups, *t* <sub>Student</sub> (98) = 2.509, *p* = 0.014, *d* <sub>Cohen's</sub> = 0.51, CI<sub>95%</sub>[0.1, 0.91], evaluated through Student's t-test for independent samples.
## Mixed effects ANOVA

Expand Down Expand Up @@ -257,6 +265,17 @@ report(
```

```
## $desc
## $desc$Basal
## [1] "M = 100.56, SD = 13.83"
##
## $desc$`Time-1`
## [1] "M = 99.5, SD = 9.01"
##
## $desc$`Time-2`
## [1] "M = 96.82, SD = 4.99"
##
##
## $pwc.method
## [1] "Student's t-test for dependent samples"
##
Expand Down Expand Up @@ -313,6 +332,14 @@ report(
```

```
## $desc
## $desc$`Time-1`
## [1] "M = 99.5, SD = 9.01"
##
## $desc$`Time-2`
## [1] "M = 96.82, SD = 4.99"
##
##
## $full
## [1] "t(49) = 1.73, p = 0.089, d = 0.25, CI95% [-0.04, 0.53]"
##
Expand Down Expand Up @@ -367,6 +394,17 @@ report(
```

```
## $desc
## $desc$Control
## [1] "M = 99, SD = 19.59"
##
## $desc$`Drug A`
## [1] "M = 97.07, SD = 10.01"
##
## $desc$`Drug B`
## [1] "M = 90.43, SD = 13.07"
##
##
## $pwc.method
## [1] "Games Howell test"
##
Expand Down Expand Up @@ -422,6 +460,14 @@ report(
```

```
## $desc
## $desc$`Drug A`
## [1] "M = 97.07, SD = 10.01"
##
## $desc$`Drug B`
## [1] "M = 90.43, SD = 13.07"
##
##
## $full
## [1] "t(58) = 2.21, p = 0.031, d = 0.58, CI95% [0.05, 1.1]"
##
Expand Down
Loading

0 comments on commit f4b197b

Please sign in to comment.