diff --git a/DESCRIPTION b/DESCRIPTION index cedb8f4..1a5a825 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -11,11 +11,12 @@ Depends: License: MIT + file LICENSE URL: https://rsquaredacademy.github.io/inferr/, https://github.com/rsquaredacademy/inferr BugReports: https://github.com/rsquaredacademy/inferr/issues -Imports: +Imports: dplyr, magrittr, purrr, Rcpp, + rlang, shiny, tibble, tidyr diff --git a/NAMESPACE b/NAMESPACE index e08dbb3..beed2bd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -76,15 +76,24 @@ export(var_test) export(var_test_shiny) importFrom(Rcpp,sourceCpp) importFrom(dplyr,funs) +importFrom(dplyr,group_by) importFrom(dplyr,group_by_) importFrom(dplyr,mutate) +importFrom(dplyr,pull) importFrom(dplyr,select) importFrom(dplyr,select_) importFrom(dplyr,summarise_all) importFrom(magrittr,"%>%") +importFrom(magrittr,subtract) +importFrom(magrittr,use_series) importFrom(purrr,map) importFrom(purrr,map_dbl) +importFrom(purrr,map_df) importFrom(purrr,map_int) +importFrom(rlang,"!!!") +importFrom(rlang,"!!") +importFrom(rlang,enquo) +importFrom(rlang,quos) importFrom(shiny,runApp) importFrom(stats,anova) importFrom(stats,as.formula) diff --git a/R/RcppExports.R b/R/RcppExports.R index 97c2d3a..49b3de1 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -2,10 +2,10 @@ # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 nsignC <- function(x) { - .Call(`_inferr_nsignC`, x) + .Call('_inferr_nsignC', PACKAGE = 'inferr', x) } gvar <- function(ln, ly) { - .Call(`_inferr_gvar`, ln, ly) + .Call('_inferr_gvar', PACKAGE = 'inferr', ln, ly) } diff --git a/R/infer-anova.R b/R/infer-anova.R index 9b3c865..5f9e93f 100644 --- a/R/infer-anova.R +++ b/R/infer-anova.R @@ -1,9 +1,10 @@ #' @importFrom stats as.formula lm pf +#' @importFrom rlang enquo !! #' @title One Way ANOVA #' @description One way analysis of variance -#' @param data a data frame -#' @param x character vector; name of a continuous variable from \code{data} -#' @param y character vector; name of a categorical variable from \code{data} +#' @param data a \code{data.frame} or a \code{tibble} +#' @param x numeric; column in \code{data} +#' @param y factor; column in \code{data} #' @param ... additional arguments passed to or from other methods #' @return \code{owanova} returns an object of class \code{"owanova"}. #' An object of class \code{"owanova"} is a list containing the @@ -31,8 +32,8 @@ #' #' @seealso \code{\link[stats]{anova}} #' @examples -#' infer_oneway_anova(mtcars, 'mpg', 'cyl') -#' infer_oneway_anova(hsb, 'write', 'prog') +#' infer_oneway_anova(mtcars, mpg, cyl) +#' infer_oneway_anova(hsb, write, prog) #' @export #' infer_oneway_anova <- function(data, x, y, ...) UseMethod('infer_oneway_anova') @@ -40,28 +41,25 @@ infer_oneway_anova <- function(data, x, y, ...) UseMethod('infer_oneway_anova') #' @export infer_oneway_anova.default <- function(data, x, y, ...) { - if (!is.data.frame(data)) { - stop('data must be a data frame') - } + x1 <- enquo(x) + y1 <- enquo(y) - if (!x %in% colnames(data)) { - stop('x must be a column in data') - } + fdata <- + data %>% + select(!! x1, !! y1) - if (!y %in% colnames(data)) { - stop('y must be a column in data') - } + sample_mean <- anova_avg(fdata, !! x1) + sample_stats <- anova_split(fdata, !! x1, !! y1, sample_mean) + k <- anova_calc(fdata, sample_stats, !! x1, !! y1) - sample_mean <- anova_avg(data, x) - sample_stats <- anova_split(data, x, y, sample_mean) - k <- anova_calc(data, sample_stats, x, y) - - result <- list( between = k$sstr, within = k$ssee, total = k$total, df_btw = k$df_sstr, - df_within = k$df_sse, df_total = k$df_sst, ms_btw = k$mstr, - ms_within = k$mse, f = k$f, p = k$sig, r2 = round(k$reg$r.squared, 4), - ar2 = round(k$reg$adj.r.squared, 4), sigma = round(k$reg$sigma, 4), - obs = k$obs, tab = round(sample_stats[, c(1, 2, 3, 5)], 3)) + result <- list(between = k$sstr, within = k$ssee, total = k$total, + df_btw = k$df_sstr, df_within = k$df_sse, + df_total = k$df_sst, ms_btw = k$mstr, ms_within = k$mse, + f = k$f, p = k$sig, r2 = round(k$reg$r.squared, 4), + ar2 = round(k$reg$adj.r.squared, 4), + sigma = round(k$reg$sigma, 4), obs = k$obs, + tab = sample_stats[, c(1, 2, 3, 5)]) class(result) <- 'infer_oneway_anova' return(result) diff --git a/R/infer-binom-test.R b/R/infer-binom-test.R index 5fe03cc..fd0fb83 100644 --- a/R/infer-binom-test.R +++ b/R/infer-binom-test.R @@ -5,7 +5,8 @@ #' @param n number of observations #' @param success number of successes #' @param prob assumed probability of success on a trial -#' @param data binary/dichotomous factor +#' @param data a \code{data.frame} or a \code{tibble} +#' @param variable factor; column in \code{data} #' @param ... additional arguments passed to or from other methods #' @return \code{binom_test} returns an object of class \code{"binom_test"}. #' An object of class \code{"binom_test"} is a list containing the @@ -30,7 +31,7 @@ #' infer_binom_calc(32, 13, prob = 0.5) #' #' # using data set -#' infer_binom_test(as.factor(hsb$female), prob = 0.5) +#' infer_binom_test(hsb, female, prob = 0.5) #' @export #' infer_binom_calc <- function(n, success, prob = 0.5, ...) UseMethod('infer_binom_calc') @@ -81,13 +82,19 @@ print.infer_binom_calc <- function(x, ...) { #' @export #' @rdname infer_binom_calc -infer_binom_test <- function(data, prob = 0.5) { +infer_binom_test <- function(data, variable, prob = 0.5) { - if (!is.factor(data)) { - stop('data must be of type factor', call. = FALSE) + varyable <- enquo(variable) + + fdata <- + data %>% + pull(!! varyable) + + if (!is.factor(fdata)) { + stop('variable must be of type factor', call. = FALSE) } - if (nlevels(data) > 2) { + if (nlevels(fdata) > 2) { stop('Binomial test is applicable only to binary data i.e. categorical data with 2 levels.', call. = FALSE) } @@ -99,8 +106,8 @@ infer_binom_test <- function(data, prob = 0.5) { stop('prob must be between 0 and 1', call. = FALSE) } - n <- length(data) - k <- table(data)[[2]] + n <- length(fdata) + k <- table(fdata)[[2]] infer_binom_calc.default(n, k, prob) } @@ -111,6 +118,6 @@ infer_binom_test <- function(data, prob = 0.5) { binom_test <- function(data, prob = 0.5) { .Deprecated("infer_binom_test()") - infer_binom_test(data, prob = 0.5) + } diff --git a/R/infer-chisq-assoc-test.R b/R/infer-chisq-assoc-test.R index 48f0f8c..2b5a46e 100644 --- a/R/infer-chisq-assoc-test.R +++ b/R/infer-chisq-assoc-test.R @@ -1,9 +1,11 @@ #' @importFrom stats pchisq +#' @importFrom dplyr pull #' @title Chi Square Test of Association #' @description Chi Square test of association to examine if there is a #' relationship between two categorical variables. -#' @param x a categorical variable -#' @param y a categorical variable +#' @param data a \code{data.frame} or \code{tibble} +#' @param x factor; column in \code{data} +#' @param y factor; column in \code{data} #' @return \code{infer_chisq_assoc_test} returns an object of class #' \code{"infer_chisq_assoc_test"}. An object of class #' \code{"infer_chisq_assoc_test"} is a list containing the @@ -30,26 +32,37 @@ #' @references Sheskin, D. J. 2007. Handbook of Parametric and Nonparametric #' Statistical Procedures, 4th edition. : Chapman & Hall/CRC. #' @examples -#' infer_chisq_assoc_test(as.factor(hsb$female), as.factor(hsb$schtyp)) +#' infer_chisq_assoc_test(hsb, female, schtyp) #' -#' infer_chisq_assoc_test(as.factor(hsb$female), as.factor(hsb$ses)) +#' infer_chisq_assoc_test(hsb, female, ses) #' @export #' -infer_chisq_assoc_test <- function(x, y) UseMethod('infer_chisq_assoc_test') +infer_chisq_assoc_test <- function(data, x, y) UseMethod('infer_chisq_assoc_test') #' @export -infer_chisq_assoc_test.default <- function(x, y) { +infer_chisq_assoc_test.default <- function(data, x, y) { - if (!is.factor(x)) { + x1 <- enquo(x) + y1 <- enquo(y) + + xone <- + data %>% + pull(!! x1) + + yone <- + data %>% + pull(!! y1) + + if (!is.factor(xone)) { stop('x must be a categorical variable') } - if (!is.factor(y)) { + if (!is.factor(yone)) { stop('y must be a categorical variable') } # dimensions - k <- table(x, y) + k <- table(xone, yone) dk <- dim(k) ds <- prod(dk) nr <- dk[1] @@ -58,7 +71,7 @@ infer_chisq_assoc_test.default <- function(x, y) { if (ds == 4) { - twoway <- matrix(table(x, y), nrow = 2) + twoway <- matrix(table(xone, yone), nrow = 2) df <- df_chi(twoway) ef <- efmat(twoway) k <- pear_chsq(twoway, df, ef) @@ -68,7 +81,7 @@ infer_chisq_assoc_test.default <- function(x, y) { } else { - twoway <- matrix(table(x, y), nrow = dk[1]) + twoway <- matrix(table(xone, yone), nrow = dk[1]) ef <- efm(twoway, dk) df <- df_chi(twoway) k <- pear_chi(twoway, df, ef) @@ -76,7 +89,7 @@ infer_chisq_assoc_test.default <- function(x, y) { } - j <- chigf(x, y, k$chi) + j <- chigf(xone, yone, k$chi) result <- if (ds == 4) { list(chi = k$chi, chilr = m$chilr, chimh = p$chimh, chiy = n$chi_y, @@ -99,7 +112,7 @@ infer_chisq_assoc_test.default <- function(x, y) { chisq_test <- function(x, y) { .Deprecated("infer_chisq_assoc_test()") - infer_chisq_assoc_test(x, y) + } diff --git a/R/infer-chisq-gof-test.R b/R/infer-chisq-gof-test.R index ed6730c..707bd3c 100644 --- a/R/infer-chisq-gof-test.R +++ b/R/infer-chisq-gof-test.R @@ -1,7 +1,8 @@ #' @title Chi Square Goodness of Fit Test #' @description Test whether the observed proportions for a categorical variable #' differ from hypothesized proportions -#' @param x categorical variable +#' @param data a \code{data.frame} or \code{tibble} +#' @param x factor; column in \code{data} #' @param y expected proportions #' @param correct logical; if TRUE continuity correction is applied #' @return \code{infer_chisq_gof_test} returns an object of class @@ -27,18 +28,35 @@ #' @references Sheskin, D. J. 2007. Handbook of Parametric and Nonparametric #' Statistical Procedures, 4th edition. : Chapman & Hall/CRC. #' @examples -#' infer_chisq_gof_test(as.factor(hsb$race), c(20, 20, 20, 140)) +#' infer_chisq_gof_test(hsb, race, c(20, 20, 20, 140)) #' #' # apply continuity correction -#' infer_chisq_gof_test(as.factor(hsb$race), c(20, 20, 20, 140), correct = TRUE) +#' infer_chisq_gof_test(hsb, race, c(20, 20, 20, 140), correct = TRUE) #' @export #' -infer_chisq_gof_test <- function(x, y, correct = FALSE) UseMethod('infer_chisq_gof_test') +infer_chisq_gof_test <- function(data, x, y, correct = FALSE) UseMethod('infer_chisq_gof_test') #' @export -infer_chisq_gof_test.default <- function(x, y, correct = FALSE) { +infer_chisq_gof_test.default <- function(data, x, y, correct = FALSE) { - if (!is.factor(x)) { + x1 <- enquo(x) + + xcheck <- + data %>% + pull(!! x1) + + xlen <- + data %>% + pull(!! x1) %>% + length + + xone <- + data %>% + pull(!! x1) %>% + table %>% + as.vector + + if (!is.factor(xcheck)) { stop('x must be an object of class factor') } @@ -50,10 +68,13 @@ infer_chisq_gof_test.default <- function(x, y, correct = FALSE) { stop('correct must be either TRUE or FALSE') } - x1 <- x - varname <- l(deparse(substitute(x))) - x <- as.vector(table(x)) - n <- length(x) + + varname <- + data %>% + select(!! x1) %>% + names + + n <- length(xone) if (length(y) != n) { stop('Length of y must be equal to the number of categories in x') @@ -62,19 +83,19 @@ infer_chisq_gof_test.default <- function(x, y, correct = FALSE) { df <- n - 1 if (sum(y) == 1) { - y <- length(x1) * y + y <- xlen * y } if ((df == 1) || (correct == TRUE)) { - k <- chi_cort(x, y) + k <- chi_cort(xone, y) } else { - k <- chigof(x, y) + k <- chigof(xone, y) } sig <- round(pchisq(k$chi, df, lower.tail = FALSE), 4) - result <- list(chisquare = k$chi, pvalue = sig, df = df, ssize = length(x1), - names = levels(x1), level = nlevels(x1), obs = x, exp = y, + result <- list(chisquare = k$chi, pvalue = sig, df = df, ssize = length(xcheck), + names = levels(xcheck), level = nlevels(xcheck), obs = xone, exp = y, deviation = format(k$dev, nsmall = 2), std = format(k$std, nsmall = 2), varname = varname) @@ -89,7 +110,6 @@ infer_chisq_gof_test.default <- function(x, y, correct = FALSE) { chisq_gof <- function(x, y, correct = FALSE) { .Deprecated("infer_chisq_gof_test()") - infer_chisq_gof_test(x, y, correct = FALSE) } diff --git a/R/infer-cochran-q-test.R b/R/infer-cochran-q-test.R index e81f854..d21f1a3 100644 --- a/R/infer-cochran-q-test.R +++ b/R/infer-cochran-q-test.R @@ -1,8 +1,9 @@ +#' @importFrom rlang quos !!! #' @title Cochran Q Test #' @description Test if the proportions of 3 or more dichotomous variables are #' equal in the same population. -#' @param x a data frame/vector -#' @param ... numeric vectors +#' @param data a \code{data.frame} or \code{tibble} +#' @param ... columns in \code{data} #' @return \code{infer_cochran_qtest} returns an object of class #' \code{"infer_cochran_qtest"}. An object of class \code{"infer_cochran_qtest"} #' is a list containing the following components: @@ -18,25 +19,28 @@ #' Statistical Procedures, 4th edition. : Chapman & Hall/CRC. #' #' @examples -#' infer_cochran_qtest(exam) +#' infer_cochran_qtest(exam, exam1, exam2, exam3) #' @export #' -infer_cochran_qtest <- function(x, ...) UseMethod('infer_cochran_qtest') +infer_cochran_qtest <- function(data, ...) UseMethod('infer_cochran_qtest') #' @export -infer_cochran_qtest.default <- function(x, ...) { +infer_cochran_qtest.default <- function(data, ...) { - data <- coch_data(x, ...) + vars <- quos(...) - if (ncol(data) < 3) { + fdata <- data %>% + select(!!! vars) + + if (ncol(fdata) < 3) { stop('Please specify at least 3 variables.') } - if (any(sapply(lapply(data, as.factor), nlevels) > 2)) { + if (any(sapply(lapply(fdata, as.factor), nlevels) > 2)) { stop('Please specify dichotomous/binary variables only.') } - k <- cochran_comp(data) + k <- cochran_comp(fdata) result <- list(n = k$n, df = k$df, q = k$q, pvalue = k$pvalue) class(result) <- 'infer_cochran_qtest' return(result) @@ -50,7 +54,6 @@ infer_cochran_qtest.default <- function(x, ...) { cochran_test <- function(x, ...) { .Deprecated("infer_cochran_qtest()") - infer_cochran_qtest(x, ...) } diff --git a/R/infer-levene-test.R b/R/infer-levene-test.R index e8f8b11..f553b1b 100644 --- a/R/infer-levene-test.R +++ b/R/infer-levene-test.R @@ -67,8 +67,6 @@ infer_levene_test.default <- function(variable, ..., group_var = NULL, varname <- deparse(substitute(variable)) - - if (is.null(group_var)) { if (is.data.frame(variable)) { @@ -97,23 +95,11 @@ infer_levene_test.default <- function(variable, ..., group_var = NULL, } - if (!is.factor(group_var)) { group_var <- as.factor(group_var) } - k <- lev_comp(variable, group_var, trim.mean) - # comp <- complete.cases(variable, group_var) - # n <- length(comp) - # k <- nlevels(group_var) - # cvar <- variable[comp] - # gvar <- group_var[comp] - # lens <- tapply(cvar, gvar, length) - # avgs <- tapply(cvar, gvar, mean) - # sds <- tapply(cvar, gvar, sd) - - # bf <- lev_metric(cvar, gvar, mean) - # lev <- lev_metric(cvar, gvar, median) - # bft <- lev_metric(cvar, gvar, mean, trim = trim.mean) + + k <- lev_comp(variable, group_var, trim.mean) out <- list(bf = k$bf, p_bf = k$p_bf, lev = k$lev, p_lev = k$p_lev, bft = k$bft, p_bft = k$p_bft, avgs = k$avgs, sds = k$sds, diff --git a/R/infer-os-prop-test.R b/R/infer-os-prop-test.R index 53c87a6..90b15b8 100644 --- a/R/infer-os-prop-test.R +++ b/R/infer-os-prop-test.R @@ -33,7 +33,7 @@ #' infer_os_prop_test(200, prob = 0.5, phat = 0.3) #' #' # using data set -#' infer_os_prop_test(as.factor(hsb$female), prob = 0.5) +#' infer_os_prop_test(hsb$female, prob = 0.5) #' @export #' infer_os_prop_test <- function(n, prob = 0.5, alternative = c('both', 'less', @@ -117,8 +117,8 @@ infer_os_prop_test.factor <- function(n, prob = 0.5, } n1 <- length(n) - n2 <- table(n)[[2]] - phat <- round(n2 / n1, 4) + n2 <- table(n)[[2]] + phat <- round(n2 / n1, 4) prob <- prob alternative <- alternative diff --git a/R/infer-os-t-test.R b/R/infer-os-t-test.R index 29bc707..d378dbf 100644 --- a/R/infer-os-t-test.R +++ b/R/infer-os-t-test.R @@ -1,7 +1,8 @@ #' @title One Sample t Test #' @description \code{infer_os_t_test} performs t tests on the equality of means. It tests the #' hypothesis that a sample has a mean equal to a hypothesized value. -#' @param x a numeric vector +#' @param data a \code{data.frame} or \code{tibble} +#' @param x numeric; column in \code{data} #' @param mu a number indicating the true value of the mean #' @param alpha acceptable tolerance for type I error #' @param type a character string specifying the alternative hypothesis, must be @@ -37,27 +38,33 @@ #' #' @examples #' # lower tail -#' infer_os_t_test(hsb$write, mu = 50, type = 'less') +#' infer_os_t_test(hsb, write, mu = 50, type = 'less') #' #' # upper tail -#' infer_os_t_test(hsb$write, mu = 50, type = 'greater') +#' infer_os_t_test(hsb, write, mu = 50, type = 'greater') #' #' # both tails -#' infer_os_t_test(hsb$write, mu = 50, type = 'both') +#' infer_os_t_test(hsb, write, mu = 50, type = 'both') #' #' # all tails -#' infer_os_t_test(hsb$write, mu = 50, type = 'all') +#' infer_os_t_test(hsb, write, mu = 50, type = 'all') #' @export #' -infer_os_t_test <- function(x, mu = 0, alpha = 0.05, +infer_os_t_test <- function(data, x, mu = 0, alpha = 0.05, type = c("both", "less", "greater", "all"), ...) UseMethod('infer_os_t_test') #' @export #' -infer_os_t_test.default <- function(x, mu = 0, alpha = 0.05, +infer_os_t_test.default <- function(data, x, mu = 0, alpha = 0.05, type = c("both", "less", "greater", "all"), ...) { - if (!is.numeric(x)) { + x1 <- enquo(x) + + xone <- + data %>% + pull(!! x1) + + if (!is.numeric(xone)) { stop('x must be numeric') } if (!is.numeric(mu)) { @@ -67,46 +74,21 @@ infer_os_t_test.default <- function(x, mu = 0, alpha = 0.05, stop('alpha must be numeric') } - type <- match.arg(type) - var_name <- l(deparse(substitute(x))) - k <- ttest_comp(x, mu, alpha, type) - # n <- length(x) - # a <- (alpha / 2) - # df <- n - 1 - # conf <- 1 - alpha - # Mean <- round(mean(x), 4) - # stddev <- round(sd(x), 4) - # std_err <- round(stddev / sqrt(n), 4) - # test_stat <- round((Mean - mu) / std_err, 3) - - # if (type == 'less') { - # cint <- c(-Inf, test_stat + qt(1 - alpha, df) ) - # } else if (type == 'greater') { - # cint <- c(test_stat - qt(1 - alpha, df), Inf) - # } else { - # cint <- qt(1 - a, df) - # cint <- test_stat + c(-cint, cint) - # } + type <- match.arg(type) - # confint <- round(mu + cint * std_err, 4) - # mean_diff <- round((Mean - mu), 4) - # mean_diff_l <- confint[1] - mu - # mean_diff_u <- confint[2] - mu - # p_l <- pt(test_stat, df) - # p_u <- pt(test_stat, df, lower.tail = FALSE) + var_name <- + data %>% + select(!! x1) %>% + names - # if (p_l < 0.5) { - # p <- p_l * 2 - # } else { - # p <- p_u * 2 - # } + k <- ttest_comp(xone, mu, alpha, type) - - result <- list(mu = k$mu, n = k$n, df = k$df, Mean = k$Mean, stddev = k$stddev, - std_err = k$std_err, test_stat = k$test_stat, confint = k$confint, - mean_diff = k$mean_diff, mean_diff_l = k$mean_diff_l, - mean_diff_u = k$mean_diff_u, p_l = k$p_l, p_u = k$p_u, p = k$p, conf = k$conf, - type = type, var_name = var_name) + result <- list(mu = k$mu, n = k$n, df = k$df, Mean = k$Mean, + stddev = k$stddev, std_err = k$std_err, + test_stat = k$test_stat, confint = k$confint, + mean_diff = k$mean_diff, mean_diff_l = k$mean_diff_l, + mean_diff_u = k$mean_diff_u, p_l = k$p_l, p_u = k$p_u, + p = k$p, conf = k$conf, type = type, var_name = var_name) class(result) <- 'infer_os_t_test' return(result) @@ -121,7 +103,7 @@ ttest <- function(x, mu = 0, alpha = 0.05, type = c("both", "less", "greater", "all"), ...) { .Deprecated("infer_os_t_test()") - infer_os_t_test(x, mu, alpha, type, ...) + } diff --git a/R/infer-os-var-test.R b/R/infer-os-var-test.R index 6a73d24..41a665d 100644 --- a/R/infer-os-var-test.R +++ b/R/infer-os-var-test.R @@ -3,7 +3,8 @@ #' @description \code{infer_os_var_test} performs tests on the equality of standard #' deviations (variances).It tests that the standard deviation of a sample is #' equal to a hypothesized value. -#' @param x a numeric vector +#' @param data a \code{data.frame} or \code{tibble} +#' @param x numeric; column in \code{data} #' @param sd hypothesised standard deviation #' @param confint confidence level #' @param alternative a character string specifying the alternative hypothesis, @@ -36,27 +37,33 @@ #' @seealso \code{\link[stats]{var.test}} #' @examples #' # lower tail -#' infer_os_var_test(mtcars$mpg, 5, alternative = 'less') +#' infer_os_var_test(mtcars, mpg, 5, alternative = 'less') #' #' # upper tail -#' infer_os_var_test(mtcars$mpg, 5, alternative = 'greater') +#' infer_os_var_test(mtcars, mpg, 5, alternative = 'greater') #' #' # both tails -#' infer_os_var_test(mtcars$mpg, 5, alternative = 'both') +#' infer_os_var_test(mtcars, mpg, 5, alternative = 'both') #' #' # all tails -#' infer_os_var_test(mtcars$mpg, 5, alternative = 'all') +#' infer_os_var_test(mtcars, mpg, 5, alternative = 'all') #' @export #' -infer_os_var_test <- function(x, sd, confint = 0.95, +infer_os_var_test <- function(data, x, sd, confint = 0.95, alternative = c('both', 'less', 'greater', 'all'), ...) UseMethod('infer_os_var_test') #' @export #' -infer_os_var_test.default <- function(x, sd, confint = 0.95, +infer_os_var_test.default <- function(data, x, sd, confint = 0.95, alternative = c('both', 'less', 'greater', 'all'), ...) { - if (!is.numeric(x)) { + x1 <- enquo(x) + + xone <- + data %>% + pull(!! x1) + + if (!is.numeric(xone)) { stop('x must be numeric') } @@ -68,9 +75,14 @@ infer_os_var_test.default <- function(x, sd, confint = 0.95, stop('confint must be numeric') } - type <- match.arg(alternative) - varname <- l(deparse(substitute(x))) - k <- osvar_comp(x, sd, confint) + type <- match.arg(alternative) + + varname <- + data %>% + select(!! x1) %>% + names + + k <- osvar_comp(xone, sd, confint) result <- list(n = k$n, sd = k$sd, sigma = k$sigma, se = k$se, chi = k$chi, df = k$df, p_lower = k$p_lower, p_upper = k$p_upper, p_two = k$p_two, @@ -90,7 +102,7 @@ os_vartest <- function(x, sd, confint = 0.95, alternative = c('both', 'less', 'greater', 'all'), ...) { .Deprecated("infer_os_var_test()") - infer_os_var_test(x, sd, confint, alternative, ...) + } diff --git a/R/infer-output.R b/R/infer-output.R index 90b423d..64c017e 100644 --- a/R/infer-output.R +++ b/R/infer-output.R @@ -39,8 +39,8 @@ print_owanova <- function(data) { cat(fg('Category', w8), fs(), fg('N', w9), fs(), fg('Mean', w10), fs(), fg('Std. Dev.', w11), '\n') cat(rep("-", wr), sep = "", '\n') for (i in seq_len(q)) { - cat(fc(data$tab[[i, 1]], w8), fs(), fg(data$tab[[i, 2]], w9), fs(), fk(data$tab[[i, 3]], w10), - fs(), fk(data$tab[[i, 4]], w11), '\n') + cat(fc(data$tab[[i, 1]], w8), fs(), fg(data$tab[[i, 2]], w9), fs(), fk(format(round(data$tab[[i, 3]], 3), nsmall = 3), w10), + fs(), fk(format(round(data$tab[[i, 4]], 3), nsmall = 3), w11), '\n') } cat(rep("-", wr), sep = "", '\n\n') diff --git a/R/infer-runs-test.R b/R/infer-runs-test.R index b51146b..53b3d9d 100644 --- a/R/infer-runs-test.R +++ b/R/infer-runs-test.R @@ -63,8 +63,8 @@ infer_runs_test.default <- function(x, drop = FALSE, threshold = NA) { n <- length(x) - if (!(is.numeric(x) || is.integer(x))) - stop("x must be numeric or integer") + # if (!(is.numeric(x) || is.integer(x))) + # stop("x must be numeric or integer") if (is.na(threshold)) { y <- unique(x) diff --git a/R/infer-utils.R b/R/infer-utils.R index 85a0145..837773c 100644 --- a/R/infer-utils.R +++ b/R/infer-utils.R @@ -1,30 +1,62 @@ -#' @importFrom dplyr group_by_ select_ summarise_all funs mutate -#' @importFrom magrittr %>% +#' @importFrom dplyr group_by group_by_ select_ summarise_all funs mutate +#' @importFrom magrittr %>% use_series #' @importFrom stats var sd #' @importFrom tibble tibble as_data_frame anova_split <- function(data, x, y, sample_mean) { - by_factor <- data %>% - group_by_(y) %>% - select_(y, x) %>% - summarise_all(funs(length, mean, var, sd)) %>% - as_data_frame() %>% - mutate( - sst = length * ((mean - sample_mean) ^ 2), - sse = (length - 1) * var - ) - return(by_factor) + + x1 <- enquo(x) + y1 <- enquo(y) + + by_factor <- + data %>% + group_by(!! y1) %>% + select(!! y1, !! x1) %>% + summarise_all(funs(length, mean, var, sd)) %>% + as_data_frame() %>% + mutate( + sst = length * ((mean - sample_mean) ^ 2), + sse = (length - 1) * var + ) + + return(by_factor) + } anova_avg <- function(data, y) { - avg <- data %>% - select_(y) %>% + + y1 <- enquo(y) + + avg <- + data %>% + select(!! y1) %>% summarise_all(funs(mean)) + return(unlist(avg, use.names = FALSE)) + } anova_calc <- function(data, sample_stats, x, y) { - sstr <- round(sum(sample_stats$sst), 3) - ssee <- round(sum(sample_stats$sse), 3) + + x1 <- enquo(x) + y1 <- enquo(y) + + var_names <- + data %>% + select(!! x1, !! y1) %>% + names + + sstr <- + sample_stats %>% + use_series(sst) %>% + sum %>% + round(3) + + ssee <- + sample_stats %>% + use_series(sse) %>% + sum %>% + round(3) + total <- round(sstr + ssee, 3) df_sstr <- nrow(sample_stats) - 1 df_sse <- nrow(data) - nrow(sample_stats) @@ -34,7 +66,7 @@ anova_calc <- function(data, sample_stats, x, y) { f <- round(mstr / mse, 3) sig <- round(1- pf(f, df_sstr, df_sse), 3) obs <- nrow(data) - regs <- paste(x, '~ as.factor(', y, ')') + regs <- paste(var_names[1], '~ as.factor(', var_names[2], ')') model <- lm(as.formula(regs), data = data) reg <- summary(model) out <- list(sstr = sstr, ssee = ssee, total = total, df_sstr = df_sstr, @@ -209,17 +241,32 @@ coch_data <- function(x, ...) { return(data) } - +#' @importFrom purrr map_df +#' @importFrom magrittr subtract cochran_comp <- function(data) { + n <- nrow(data) k <- ncol(data) df <- k - 1 - cs <- sums(data) + + cs <- + data %>% + map_df(.f = as.numeric) %>% + subtract(1) %>% + sums + q <- coch(k, cs$cls_sum, cs$cl, cs$g, cs$gs_sum) + pvalue <- 1 - pchisq(q, df) - out <- list(n = n, df = df, q = q, pvalue = round(pvalue, 4)) + + out <- list(n = n, + df = df, + q = q, + pvalue = round(pvalue, 4)) + return(out) -} + + } # levene test @@ -343,7 +390,7 @@ prop_fact <- function(dat, p) { cases <- 1 - row_sum[2] ratio <- cases / controls odds_ratio <- p[1] / p[2] - out <- list(cases = cases, controls = controls, ratio = ratio, + out <- list(cases = cases, controls = controls, ratio = ratio, odds_ratio = odds_ratio) return(out) } @@ -374,14 +421,14 @@ dat_per[d1[1], d1[2]] <- 1.0 mccomp <- function(dat) { p <- mctestp(dat) - test_stat <- tetat(p) + test_stat <- tetat(p) df <- nrow(dat) - 1 - pvalue <- mcpval(test_stat, df) + pvalue <- mcpval(test_stat, df) exactp <- mcpex(dat) cstat <- mcstat(p) cpvalue <- mccpval(cstat, df) kappa <- mckappa(dat) - std_err <- mcserr(dat, kappa) + std_err <- mcserr(dat, kappa) clu <- mcconf(std_err, kappa) k <- prop_fact(dat, p) @@ -425,7 +472,7 @@ prop_comp <- function(n, prob, alternative, phat) { } out <- list(n = n, phat = phat, p = prob, z = z, sig = sig, alt = alt, - obs = obs, exp = exp, deviation = format(dev, nsmall = 2), + obs = obs, exp = exp, deviation = format(dev, nsmall = 2), std = format(std, nsmall = 2)) return(out) @@ -457,7 +504,7 @@ osvar_comp <- function(x, sd, confint) { c_upr <- round(tv / qchisq(a, df), 4) out <- list(n = n, sd = sd, sigma = sigma, se = se, chi = chi, df = df, - p_lower = p_lower, p_upper = p_upper, p_two = p_two, xbar = xbar, + p_lower = p_lower, p_upper = p_upper, p_two = p_two, xbar = xbar, c_lwr = c_lwr, c_upr = c_upr, conf = conf) return(out) @@ -520,7 +567,7 @@ prop_comp2 <- function(var1, var2, alt) { ut <- round(pnorm(z, lower.tail = FALSE), 4) tt <- round(pnorm(abs(z), lower.tail = FALSE) * 2, 4) - + if (alt == "all") { sig = c('two-tail' = tt, 'lower-tail' = lt, 'upper-tail' = ut) @@ -665,7 +712,7 @@ indsig <- function(n1, n2, s1, s2, mean_diff) { fsig <- function(s1, s2, n1, n2) { out <- round(min(pf(round(s1 / s2, 4), (n1 - 1), (n2 -1)), - pf(round(s1 / s2, 4), (n1 - 1), (n2 -1), + pf(round(s1 / s2, 4), (n1 - 1), (n2 -1), lower.tail = FALSE)) * 2, 4) return(out) } @@ -681,8 +728,8 @@ indpool <- function(n1, n2, mean_diff, se_dif) { } else { sig_pooled <- round(pt(t_pooled, df_pooled, lower.tail = FALSE) * 2, 4) } - out <- list(df_pooled = df_pooled, t_pooled = t_pooled, - sig_pooled_l = sig_pooled_l, sig_pooled_u = sig_pooled_u, + out <- list(df_pooled = df_pooled, t_pooled = t_pooled, + sig_pooled_l = sig_pooled_l, sig_pooled_u = sig_pooled_u, sig_pooled = sig_pooled) return(out) } @@ -969,4 +1016,4 @@ check_x <- function(data, x) { unlist() %>% (is.factor) %>% `!` -} \ No newline at end of file +} diff --git a/README.Rmd b/README.Rmd index abb367c..39759fa 100644 --- a/README.Rmd +++ b/README.Rmd @@ -82,19 +82,19 @@ library(inferr) ``` ```{r infer1} -infer_os_t_test(hsb$write, mu = 50, type = 'all') +infer_os_t_test(hsb, write, mu = 50, type = 'all') ``` ##### ANOVA ```{r anova} -infer_oneway_anova(hsb, 'write', 'prog') +infer_oneway_anova(hsb, write, prog) ``` ##### Chi Square Test of Independence ```{r chi1} -infer_chisq_assoc_test(as.factor(hsb$female), as.factor(hsb$schtyp)) +infer_chisq_assoc_test(hsb, female, schtyp) ``` ##### Levene's Test @@ -106,7 +106,7 @@ infer_levene_test(hsb$read, group_var = hsb$race) ##### Cochran's Q Test ```{r cochran} -infer_cochran_qtest(exam) +infer_cochran_qtest(exam, exam1, exam2, exam3) ``` ##### McNemar Test diff --git a/README.md b/README.md index a91bdec..ab46f03 100644 --- a/README.md +++ b/README.md @@ -1,44 +1,54 @@ -inferr: Inferential statistics with R --------------------------------------------------------------------------------- -**Author:** [Aravind Hebbali](http://www.aravindhebbali.com)
**License:** [MIT](https://opensource.org/licenses/MIT) +## inferr: Inferential statistics with R -[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/inferr)](https://cran.r-project.org/package=inferr) [![Travis-CI Build Status](https://travis-ci.org/rsquaredacademy/inferr.svg?branch=master)](https://travis-ci.org/rsquaredacademy/inferr) [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/rsquaredacademy/inferr?branch=master&svg=true)](https://ci.appveyor.com/project/rsquaredacademy/inferr) [![Coverage Status](https://img.shields.io/codecov/c/github/rsquaredacademy/inferr/master.svg)](https://codecov.io/github/rsquaredacademy/inferr?branch=master) [![](https://cranlogs.r-pkg.org/badges/grand-total/inferr)](https://cran.r-project.org/package=inferr) ![](https://img.shields.io/badge/lifecycle-maturing-blue.svg) +**Author:** [Aravind Hebbali](http://www.aravindhebbali.com)
+**License:** +[MIT](https://opensource.org/licenses/MIT) +[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/inferr)](https://cran.r-project.org/package=inferr) +[![Travis-CI Build +Status](https://travis-ci.org/rsquaredacademy/inferr.svg?branch=master)](https://travis-ci.org/rsquaredacademy/inferr) +[![AppVeyor Build +Status](https://ci.appveyor.com/api/projects/status/github/rsquaredacademy/inferr?branch=master&svg=true)](https://ci.appveyor.com/project/rsquaredacademy/inferr) +[![](https://cranlogs.r-pkg.org/badges/grand-total/inferr)](https://cran.r-project.org/package=inferr) -Overview --------- +## Overview -Inferential statistics allows us to make generalizations about populations using data drawn from the population. We use them when it is impractical or impossible to collect data about the whole population under study and instead, we have a sample that represents the population under study and using inferential statistics technique, we make generalizations about the population from the sample. +Inferential statistics allows us to make generalizations about +populations using data drawn from the population. We use them when it is +impractical or impossible to collect data about the whole population +under study and instead, we have a sample that represents the population +under study and using inferential statistics technique, we make +generalizations about the population from the sample. The **inferr** package: -- builds upon the statistical tests provided in **stats** -- provides additional and flexible input options -- more detailed and structured test results - -As of version 0.1, **inferr** includes a select set of parametric and non-parametric statistical tests which are listed below: - -- One Sample t Test -- Paired Sample t Test -- Independent Sample t Test -- One Sample Proportion Test -- Two Sample Proportion Test -- One Sample Variance Test -- Two Sample Variance Test -- Binomial Test -- ANOVA -- Chi Square Goodness of Fit Test -- Chi Square Independence Test -- Levene's Test -- Cochran's Q Test -- McNemar Test -- Runs Test for Randomness - -Installation ------------- + - builds upon the statistical tests provided in **stats** + - provides additional and flexible input options + - more detailed and structured test results + +As of version 0.1, **inferr** includes a select set of parametric and +non-parametric statistical tests which are listed below: + + - One Sample t Test + - Paired Sample t Test + - Independent Sample t Test + - One Sample Proportion Test + - Two Sample Proportion Test + - One Sample Variance Test + - Two Sample Variance Test + - Binomial Test + - ANOVA + - Chi Square Goodness of Fit Test + - Chi Square Independence Test + - Levene’s Test + - Cochran’s Q Test + - McNemar Test + - Runs Test for Randomness + +## Installation ``` r # install inferr from CRAN @@ -49,23 +59,21 @@ install.packages("inferr") devtools::install_github("rsquaredacademy/inferr") ``` -Shiny App ---------- +## Shiny App Use `infer_launch_shiny_app()` to explore the package using a shiny app. -Vignettes ---------- +## Vignettes -- [Introduction to inferr](http://www.rsquaredacademy.com/inferr/articles/index.html) + - [Introduction to + inferr](http://www.rsquaredacademy.com/inferr/articles/index.html) -Usage ------ +## Usage ##### One Sample t Test ``` r -infer_os_t_test(hsb$write, mu = 50, type = 'all') +infer_os_t_test(hsb, write, mu = 50, type = 'all') #> One-Sample Statistics #> --------------------------------------------------------------------------------- #> Variable Obs Mean Std. Err. Std. Dev. [95% Conf. Interval] @@ -83,7 +91,7 @@ infer_os_t_test(hsb$write, mu = 50, type = 'all') ##### ANOVA ``` r -infer_oneway_anova(hsb, 'write', 'prog') +infer_oneway_anova(hsb, write, prog) #> ANOVA #> ---------------------------------------------------------------------- #> Sum of @@ -96,11 +104,11 @@ infer_oneway_anova(hsb, 'write', 'prog') #> #> Report #> ----------------------------------------- -#> Category N Mean Std. Dev. +#> Category N Mean Std. Dev. #> ----------------------------------------- -#> 1 45 51.333 9.398 -#> 2 105 56.257 7.943 -#> 3 50 46.760 9.319 +#> 1 45 51.333 9.398 +#> 2 105 56.257 7.943 +#> 3 50 46.760 9.319 #> ----------------------------------------- #> #> Number of obs = 200 R-squared = 0.1776 @@ -110,7 +118,7 @@ infer_oneway_anova(hsb, 'write', 'prog') ##### Chi Square Test of Independence ``` r -infer_chisq_assoc_test(as.factor(hsb$female), as.factor(hsb$schtyp)) +infer_chisq_assoc_test(hsb, female, schtyp) #> Chi Square Statistics #> #> Statistics DF Value Prob @@ -125,7 +133,7 @@ infer_chisq_assoc_test(as.factor(hsb$female), as.factor(hsb$schtyp)) #> ---------------------------------------------------- ``` -##### Levene's Test +##### Levene’s Test ``` r infer_levene_test(hsb$read, group_var = hsb$race) @@ -150,10 +158,10 @@ infer_levene_test(hsb$read, group_var = hsb$race) #> ------------------------------------------------------------------------- ``` -##### Cochran's Q Test +##### Cochran’s Q Test ``` r -infer_cochran_qtest(exam) +infer_cochran_qtest(exam, exam1, exam2, exam3) #> Test Statistics #> ---------------------- #> N 15 @@ -204,4 +212,6 @@ infer_mcnemar_test(table(himath, hiread)) #> ---------------------- ``` -Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). By participating in this project you agree to abide by its terms. +Please note that this project is released with a [Contributor Code of +Conduct](CONDUCT.md). By participating in this project you agree to +abide by its terms. diff --git a/data/hsb.rda b/data/hsb.rda index 56130c6..799e9a8 100644 Binary files a/data/hsb.rda and b/data/hsb.rda differ diff --git a/docs/articles/intro.html b/docs/articles/intro.html index fc674a0..7b13de8 100644 --- a/docs/articles/intro.html +++ b/docs/articles/intro.html @@ -80,7 +80,7 @@ @@ -121,7 +121,7 @@

Example

Using the hsb data, test whether the average of write differs significantly from 50.

-
infer_os_t_test(hsb$write, mu = 50, type = 'all')
+
infer_os_t_test(hsb, write, mu = 50, type = 'all')
##                               One-Sample Statistics                               
 ## ---------------------------------------------------------------------------------
 ##  Variable    Obs     Mean     Std. Err.    Std. Dev.    [95% Conf. Interval] 
@@ -358,7 +358,7 @@ 

Examples

Using the mtcars data, compare the standard deviation of mpg to a hypothesized value.

# Lower Tail Test
-infer_os_var_test(mtcars$mpg, 0.3, alternative = 'less')
+infer_os_var_test(mtcars, mpg, 0.3, alternative = 'less')
##                             One-Sample Statistics                             
 ## -----------------------------------------------------------------------------
 ##  Variable    Obs     Mean      Std. Err.    Std. Dev.    [95% Conf. Interval] 
@@ -378,7 +378,8 @@ 

## mpg 12511.436 31 1.0000 ## ----------------------------------------

# Test all alternatives
-infer_os_var_test(mtcars$mpg, 0.3, alternative = 'all')
+ +infer_os_var_test(mtcars, mpg, 0.3, alternative = 'all')
##                             One-Sample Statistics                             
 ## -----------------------------------------------------------------------------
 ##  Variable    Obs     Mean      Std. Err.    Std. Dev.    [95% Conf. Interval] 
@@ -475,7 +476,8 @@ 

Examples

Using the hsb data, test whether the proportion of females and males are equal.

# Using variables
-infer_binom_test(as.factor(hsb$female), prob = 0.5)
+ +infer_binom_test(hsb, female, prob = 0.5)
##              Binomial Test              
 ##  ---------------------------------------
 ##   Group     N     Obs. Prop    Exp. Prop 
@@ -524,7 +526,8 @@ 

Examples

Using the hsb data, test whether the mean of write differs between the three program types.

-
infer_oneway_anova(hsb, 'write', 'prog')
+ +
infer_oneway_anova(hsb, write, prog)
##                                 ANOVA                                  
 ## ----------------------------------------------------------------------
 ##                    Sum of                                             
@@ -537,11 +540,11 @@ 

## ## Report ## ----------------------------------------- -## Category N Mean Std. Dev. +## Category N Mean Std. Dev. ## ----------------------------------------- -## 1 45 51.333 9.398 -## 2 105 56.257 7.943 -## 3 50 46.760 9.319 +## 1 45 51.333 9.398 +## 2 105 56.257 7.943 +## 3 50 46.760 9.319 ## ----------------------------------------- ## ## Number of obs = 200 R-squared = 0.1776 @@ -557,8 +560,8 @@

Example

Using the hsb data, test whether the observed proportions for race differs significantly from the hypothesized proportions.

+ +infer_chisq_gof_test(hsb, race, c(20, 20, 20 , 140))
##     Test Statistics     
 ## -----------------------
 ## Chi-Square       5.0286 
@@ -579,8 +582,8 @@ 

Continuity Correction

+ +infer_chisq_gof_test(hsb, race, c(20, 20, 20 , 140), correct = TRUE)
##     Test Statistics     
 ## -----------------------
 ## Chi-Square       4.3821 
@@ -608,7 +611,8 @@ 

Examples

Using the hsb data, test if there is a relationship between the type of school attended (schtyp) and students’ gender (female).

-
infer_chisq_assoc_test(as.factor(hsb$female), as.factor(hsb$schtyp))
+ +
infer_chisq_assoc_test(hsb, female, schtyp)
##                Chi Square Statistics                 
 ## 
 ## Statistics                     DF    Value      Prob 
@@ -622,7 +626,8 @@ 

## Cramer's V 0.0153 ## ----------------------------------------------------

Using the hsb data, test if there is a relationship between the type of school attended (schtyp) and students’ socio economic status (ses).

-
infer_chisq_assoc_test(as.factor(hsb$schtyp), as.factor(hsb$ses))
+ +
infer_chisq_assoc_test(hsb, schtyp, ses)
##                Chi Square Statistics                 
 ## 
 ## Statistics                     DF    Value      Prob 
@@ -752,7 +757,8 @@ 

Example

The exam data set contains scores of 15 students for three exams (exam1, exam2, exam3). Test if three exams are equally difficult.

- + +
infer_cochran_qtest(exam, exam1, exam2, exam3)
##    Test Statistics     
 ## ----------------------
 ## N                   15 
diff --git a/docs/hex_inferr.png b/docs/hex_inferr.png
new file mode 100644
index 0000000..0a0d656
Binary files /dev/null and b/docs/hex_inferr.png differ
diff --git a/docs/reference/infer_binom_calc.html b/docs/reference/infer_binom_calc.html
index 656dc8a..643edad 100644
--- a/docs/reference/infer_binom_calc.html
+++ b/docs/reference/infer_binom_calc.html
@@ -6,7 +6,7 @@
 
 
 
-Binomial Test — infer_binom_calc • inferr
+Binomial Test — infer_binom_calc • inferr
 
 
 
@@ -111,7 +111,7 @@ 

Binomial Test

infer_binom_calc(n, success, prob = 0.5, ...)
 
-infer_binom_test(data, prob = 0.5)
+infer_binom_test(data, variable, prob = 0.5)

Arguments

@@ -134,7 +134,13 @@

Ar

- + + + + + + +
data

binary/dichotomous factor

a data.frame or a tibble

variable

factor; column in data

@@ -186,7 +192,7 @@

Examp #> Upper Pr(k >= 13) 0.892336 #> --------------------------------------------
# using data set -infer_binom_test(as.factor(hsb$female), prob = 0.5)
#> Binomial Test +infer_binom_test(hsb, female, prob = 0.5)
#> Binomial Test #> --------------------------------------- #> Group N Obs. Prop Exp. Prop #> --------------------------------------- diff --git a/docs/reference/infer_chisq_assoc_test.html b/docs/reference/infer_chisq_assoc_test.html index 209beb6..b41688f 100644 --- a/docs/reference/infer_chisq_assoc_test.html +++ b/docs/reference/infer_chisq_assoc_test.html @@ -11,8 +11,8 @@ - + @@ -23,14 +23,17 @@ - + + - + + + @@ -106,26 +109,48 @@

Chi Square Test of Association

relationship between two categorical variables.

-
infer_chisq_assoc_test(x, y)
+
infer_chisq_assoc_test(data, x, y)

Arguments

-
-
x
-
a categorical variable
-
y
-
a categorical variable
-
+ + + + + + + + + + + + + + +
data

a data.frame or tibble

x

factor; column in data

y

factor; column in data

Value

infer_chisq_assoc_test returns an object of class -"infer_chisq_assoc_test". An object of class -"infer_chisq_assoc_test" is a list containing the +"infer_chisq_assoc_test". An object of class +"infer_chisq_assoc_test" is a list containing the following components:

+
chi

chi square

+
chilr

likelihood ratio chi square

+
chimh

mantel haenszel chi square

+
chiy

continuity adjusted chi square

+
sig

p-value of chi square

+
siglr

p-value of likelihood ratio chi square

+
sigmh

p-value of mantel haenszel chi square

+
sigy

p-value of continuity adjusted chi square

+
phi

phi coefficient

+
cc

contingency coefficient

+
cv

cramer's v

+
ds

product of dimensions of the table of x and y

+
df

degrees of freedom

+

Deprecated Function

-

chisq_test() has been deprecated. Instead use infer_chisq_assoc_test().

@@ -140,7 +165,7 @@

See a

Examples

-
infer_chisq_assoc_test(as.factor(hsb$female), as.factor(hsb$schtyp))
#> Chi Square Statistics +
infer_chisq_assoc_test(hsb, female, schtyp)
#> Chi Square Statistics #> #> Statistics DF Value Prob #> ---------------------------------------------------- @@ -150,9 +175,9 @@

Examp #> Mantel-Haenszel Chi-Square 1 0.0468 0.8287 #> Phi Coefficient 0.0153 #> Contingency Coefficient 0.0153 -#> Cramer's V 0.0153 +#> Cramer's V 0.0153 #> ----------------------------------------------------

-infer_chisq_assoc_test(as.factor(hsb$female), as.factor(hsb$ses))
#> Chi Square Statistics +infer_chisq_assoc_test(hsb, female, ses)
#> Chi Square Statistics #> #> Statistics DF Value Prob #> ---------------------------------------------------- @@ -160,7 +185,7 @@

Examp #> Likelihood Ratio Chi-Square 2 4.6789 0.0964 #> Phi Coefficient 0.1513 #> Contingency Coefficient 0.1496 -#> Cramer's V 0.1513 +#> Cramer's V 0.1513 #> ----------------------------------------------------