Skip to content

Commit

Permalink
-
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 Apr 9, 2021
1 parent 6c9efec commit d69f4e4
Show file tree
Hide file tree
Showing 16 changed files with 3,891 additions and 183 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: writR
Title: Inferential statistics and reporting in APA style
Version: 0.2.0.1
Version: 0.3.0
Date: 2021-03-05
Authors@R:
person(given = "Matías",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

export(aov_r)
export(cent_disp)
export(contingency)
export(report)
2 changes: 1 addition & 1 deletion R/aov.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ aov_r <- function(data
rt <- model[i,]
j <- if (grepl(pattern = ':', i)) gsub(':', '_', i) else i
result[['full']][[j]] <- paste0(
stats <- paste0("*F* ~", if(!is.null(within) && any(grepl(within, i))) at$correction else "Fisher"
stats <- paste0("*F*~", if(!is.null(within) && any(grepl(within, i))) at$correction else "Fisher"
, "~ (", rt$`num Df`
,", ",rt$`den Df`
,') = ',rt$F
Expand Down
10 changes: 5 additions & 5 deletions R/bipair.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ bipair <- function(data
d <- effectsize::effectsize(test, verbose = F)

desc <- if(markdown) {
list(m = '*M* = ', i = ', *SD* = ', t = '*t* ~Student~ (', p = ', *p* '
, d = "*d* ~Cohen's~ = ", ci = ', CI~95%~[') } else {
list(m = '$M$ = ', i = ', $SD$ = ', t = '$t_{~Student}$ (', p = ', $p$ '
, d = "$d_{~Cohen}$ = ", ci = ', CI~95%~[') } else {
list(m = 'M = ', i = ', SD = ', t = 't(', p = ', p '
, d = "d = ", ci = ', CI95% [') }

Expand Down Expand Up @@ -84,7 +84,7 @@ bipair <- function(data
, nboot = nboot)['AKP',]

desc <- if(markdown) {
list(m = '*M* = ', i = ', *SD* = ', t = '*t* ~Yuen~ (', p = ', *p* '
list(m = '$M$ = ', i = ', $SD$ = ', t = '$t_{~Yuen}$ (', p = ', $p$ '
, d = '$\\delta_R^{AKP}$ = ', ci = ', CI~95%~[') } else {
list(m = 'M = ', i = ', SD = ', t = 't(', p = ', p '
, d = "delta = ", ci = ', CI95% [') }
Expand Down Expand Up @@ -115,8 +115,8 @@ bipair <- function(data
paired = TRUE, verbose = FALSE)

desc <- if(markdown) {
list(m = '*Mdn* = ', i = ', *IQR* = ', v = '*V* = ', p = ', *p* '
, r = '*r* ~biserial~ = ', ci = ', CI~95%~[') } else {
list(m = '$Mdn$ = ', i = ', $IQR$ = ', v = '$V$ = ', p = ', $p$ '
, r = '$r_{~biserial}$ = ', ci = ', CI~95%~[') } else {
list(m = 'Mdn = ', i = ', IQR = ', v = 'V = ', p = ', p '
, r = 'r = ', ci = ', CI95% [') }

Expand Down
14 changes: 7 additions & 7 deletions R/bitwo.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ bitwo <- function(data
d <- effectsize::effectsize(test, verbose = F)

desc <- if(markdown) {
list(m = '*M* = ', i = ', *SD* = ', t = '*t* ~Student~ (', p = ', *p* '
, d = "*d* ~Cohen's~ = ", ci = ', CI~95%~[') } else {
list(m = '$M$ = ', i = ', $SD$ = ', t = '$t_{~Student}$ (', p = ', $p$ '
, d = "$d_{~Cohen}$ = ", ci = ', CI~95%~[') } else {
list(m = 'M = ', i = ', SD = ', t = 't(', p = ', p '
, d = "d = ", ci = ', CI95% [') }

Expand Down Expand Up @@ -87,8 +87,8 @@ bitwo <- function(data
d <- effectsize::effectsize(test, verbose = F)

desc <- if(markdown) {
list(m = '*M* = ', i = ', *SD* = ', t = '*t* ~Welch~ (', p = ', *p* '
, d = "*d* ~Cohen's~ = ", ci = ', CI~95%~[') } else {
list(m = '$M$ = ', i = ', $SD$ = ', t = '$t_{~Welch}$ (', p = ', $p$ '
, d = "$d_{~Cohen}$ = ", ci = ', CI~95%~[') } else {
list(m = 'M = ', i = ', SD = ', t = 't(', p = ', p '
, d = "d = ", ci = ', CI95% [') }

Expand Down Expand Up @@ -126,7 +126,7 @@ bitwo <- function(data
, nboot = nboot)

desc <- if(markdown) {
list(m = '*M* = ', i = ', *SD* = ', t = '*t* ~Yuen~ (', p = ', *p* '
list(m = '$M$ = ', i = ', $SD$ = ', t = '$t_{~Yuen}$ (', p = ', $p$ '
, d = '$\\xi$ = ', ci = ', CI~95%~[') } else {
list(m = 'M = ', i = ', SD = ', t = 't(', p = ', p '
, d = "xi = ", ci = ', CI95% [') }
Expand Down Expand Up @@ -156,8 +156,8 @@ bitwo <- function(data
r <- effectsize::rank_biserial(data[[variable]] ~ data[[by]], data = data)

desc <- if(markdown) {
list(m = '*Mdn* = ', i = ', *IQR* = ', w = '*W* =', p = ', *p* '
, r = '*r* ~biserial~ = ', ci = ', CI~95%~[') } else {
list(m = '$Mdn$ = ', i = ', $IQR$ = ', w = '$W$ =', p = ', $p$ '
, r = '$r_{~biserial}$ = ', ci = ', CI~95%~[') } else {
list(m = 'Mdn = ', i = ', IQR = ', w = 'W =', p = ', p '
, r = 'r = ', ci = ', CI95% [') }

Expand Down
4 changes: 2 additions & 2 deletions R/cent_disp.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cent_disp <- function (x
if(!is.numeric(x)) stop(paste(deparse(substitute(x)), "is not numeric."))
if(type == 'auto') {
.norm <- if(length(x) < 50)
shapiro.test(x)$p.value > 0.05 else
stats::shapiro.test(x)$p.value > 0.05 else
nortest::lillie.test(x)$p.value > 0.05
type <- if(.norm) "p" else "np"
}
Expand All @@ -25,6 +25,6 @@ cent_disp <- function (x
m <- round(.f$cent(x, na.rm = T), k)
i <- round(.f$disp(x, na.rm = T), k)
if(markdown)
paste0('*',.f$m,'* = ',m,', *',.f$i,'* = ',i) else
paste0('$',.f$m,'$ = ',m,', $',.f$i,'$ = ',i) else
paste0(.f$m,' = ',m,', ',.f$i,' = ',i)
}
202 changes: 202 additions & 0 deletions R/contingency.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
#' Report categorical analyses
#'
#' Perform nominal/ordinal analysis on 1 dimensional table for goodnes-of-fit chi-squared, and two dimensional data for Pearson chi-squared, Fisher exact test or (if paired), McNemar test reporting their corresponding stats and effect sizes in APA Style.
#' @param data Data frame containing the variables `x` and `y`.
#' @param x Factor variable, quoted or unquoted.
#' @param y Factor. If `NULL`, a goodness-of-fit is carried, otherwise a two-way analysis is performed.
#' @param paired Logical. If `TRUE` McNemar's Chi-squared test is carried on.
#' @param exact Logical. If `TRUE` then Fisher's Exact Test is carried on, but only when `paired = FALSE` (default). If is a 2 x 2 design, Odds Ratio (OR) is returned as effect size, otherwise it will only return the formated p-value.
#' @param markdown Whether you want the output formated for inline R Markdown or as plain text.
#' @param ... Currently not used.
#' @keywords contingency
#' @return A list of length 3 or 2 with statistical test and `$method` used.
#' @export

contingency <- function(data
, x
, y = NULL
, paired = FALSE
, exact = FALSE
, markdown = TRUE
, ...) {
.arg <- match.call()

x.var <- data[[.arg$x]]
if(is.null(.arg$y)) {
way <- "One"
} else {
y.var <- data[[.arg$y]]
way <- "Two"
}

result <- list()
test <- if(isTRUE(paired)) "Mcnemar" else
if(isTRUE(exact)) "Exact" else
"Chi"

if(test == "Chi") {
tab <- if(way == "One")
list(table(x.var),
"gof",
"Chi-squared test for given probabilities") else
list(table(x.var, y.var),
"Pearson",
"Pearson's Chi-squared test")
test <- stats::chisq.test(
x = tab[[1]],
correct = FALSE)
es <- effectsize::cramers_v(
x = tab[[1]],
adjust = FALSE)
expr <- if(isTRUE(markdown))
list(a = paste0("$\\chi^2_{~", tab[[2]], "}$ ("),
b = ", $p$ ",
c = "$V_{~Cramer}$ = ",
d = ', CI~95%~[') else
list(a = "X^2 (",
b = ", p ",
c = "V = ",
d = ', CI95% [')
result[['full']] <- paste0(
result[['stats']] <- paste0(
expr$a,
test$parameter,
") = ",
round(test$statistic,2),
expr$b,
ifelse(
test$p.value < 0.001,
'< 0.001',
paste(
'=',
round(test$p.value, 3)
)
)
), ', ',
result[['es']] <- paste0(
expr$c,
round(es$Cramers_v,2),
expr$d,
round(es$CI_low,2),
', ',
round(es$CI_high,2),
']')
)
result[['method']] <- tab[[3]]
return(result)

} else {
if(test == "Exact") {
tab <- table(x.var, y.var)
test <- stats::fisher.test(
x = tab)
error <- class(
try(
expr = {
(es <- effectsize::oddsratio(
x = x.var,
y = y.var) )
},
silent = TRUE)
) == "try-error"
if(isTRUE(error)) {
expr <- if(isTRUE(markdown))
list(a = "$p_{~FET}$ ") else
list(a = "FET, p ")
result[['full']] <- paste0(
result[['stats']] <- paste0(
expr$a,
ifelse(
test = test$p.value < 0.001,
yes = '< 0.001',
no = paste(
'=',
round(test$p.value, 3)
)
)
)
)
result[['es']] <- "Not available"
result[['method']] <- "Fisher's Exact Test for Count Data"
return(result)
} else {
expr <- if(isTRUE(markdown))
list(a = "$p_{~FET}$ ",
b = "$OR$ = ",
c = ', CI~95%~[') else
list(a = "FET: p ",
b = "OR = ",
c = ', CI95% [')
result[['full']] <- paste0(
result[['stats']] <- paste0(
expr$a,
ifelse(
test = test$p.value < 0.001,
yes = '< 0.001',
no = paste(
'=',
round(test$p.value, 3)
)
)
)
, ', ',
result[['es']] <- paste0(
expr$b,
round(es$Odds_ratio,2),
expr$c,
round(es$CI_low,2),
', ',
round(es$CI_high,2),
']')
)
result[['method']] <- "Fisher's Exact Test for Count Data"
return(result)
}
} else {
tab <- table(x.var,y.var)
test <- stats::mcnemar.test(
x = tab,
correct = FALSE)
es <- effectsize::cohens_g(
x = tab)
expr <- if(isTRUE(markdown))
list(a = paste0("$\\chi^2_{~McNemar}$ ("),
b = ", $p$ ",
c = "$g_{~Cohen}$ = ",
d = ', CI~95%~[') else
list(a = "X^2 (",
b = ", p ",
c = "g = ",
d = ', CI95% [')
result[['full']] <- paste0(
result[['stats']] <- paste0(
expr$a,
test$parameter,
") = ",
round(test$statistic,2),
expr$b,
ifelse(
test = test$p.value < 0.001,
yes = '< 0.001',
no = paste(
'=',
round(test$p.value, 3)
)
)
),
', ',
result[['es']] <- paste0(
expr$c,
round(es$Cohens_g,2),
expr$d,
round(es$CI_low,2),
', ',
round(es$CI_high,2),
']')
)
result[['method']] <- "McNemar's Chi-squared test"
return(result)
}
}
}

14 changes: 7 additions & 7 deletions R/multgroup.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ multgroup <- function(data
}

desc <- if(markdown) {
list(m = '*M* = ', i = ', *SD* = ', f = '*F* ~Fisher~ (', p = ', *p* '
, eta = '$\\eta$^2^ = ', ci = ', CI~95%~[') } else {
list(m = '$M$ = ', i = ', $SD$ = ', f = '$F_{~Fisher}$ (', p = ', $p$ '
, eta = '$\\eta^2$ = ', ci = ', CI~95%~[') } else {
list(m = 'M = ', i = ', SD = ', f = 'F(', p = ', p '
, eta = "eta^2 = ", ci = ', CI95% [') }

Expand Down Expand Up @@ -113,8 +113,8 @@ multgroup <- function(data
}

desc <- if(markdown) {
list(m = '*M* = ', i = ', *SD* = ', f = '*F* ~Welch~ (', p = ', *p* '
, eta = '$\\eta$^2^ = ', ci = ', CI~95%~[') } else {
list(m = '$M$ = ', i = ', $SD$ = ', f = '$F_{~Welch}$ (', p = ', $p$ '
, eta = '$\\eta^2$ = ', ci = ', CI~95%~[') } else {
list(m = 'M = ', i = ', SD = ', f = 'F(', p = ', p '
, eta = "eta^2 = ", ci = ', CI95% [') }

Expand Down Expand Up @@ -159,7 +159,7 @@ multgroup <- function(data
}

desc <- if(markdown) {
list(m = '*M* = ', i = ', *SD* = ', f = '*F* ~trimed-means~ (', p = ', *p* '
list(m = '$M$ = ', i = ', $SD$ = ', f = '$F_{~trimed-means}$ (', p = ', $p$ '
, xi = '$\\xi$ = ', ci = ', CI~95%~[') } else {
list(m = 'M = ', i = ', SD = ', f = 'F(', p = ', p '
, xi = "xi = ", ci = ', CI95% [') }
Expand Down Expand Up @@ -202,8 +202,8 @@ multgroup <- function(data
}

desc <- if(markdown) {
list(m = '*Mdn* = ', i = ', *IQR* = ', chi = '$\\chi$^2^ ~Kruskal-Wallis~ (', p = ', *p* '
, ep = '$\\epsilon$^2^ = ', ci = ', CI~95%~[') } else {
list(m = '$Mdn$ = ', i = ', $IQR$ = ', chi = '$\\chi^2_{~Kruskal-Wallis}$ (', p = ', $p$ '
, ep = '$\\epsilon^2$ = ', ci = ', CI~95%~[') } else {
list(m = 'Mdn = ', i = ', IQR = ', chi = 'X^2(', p = ', p '
, ep = 'epsilon^2 = ', ci = ', CI95% [') }

Expand Down
Loading

0 comments on commit d69f4e4

Please sign in to comment.