## This script provides the methods used to analyze contingency tables ## After opening this file on R (File > Open script ..), select all (by pressing "CTRL + A" and press "CTRL + R" ## The whole script will run in one go if all the required packages are already installed (epiR, psych, DescTools) # First, generate your contingency table as follows: n4 <- c(43, 28, 42, 58) # Enter the four numbers from your 2x2 table (first column and then second column) ct <- matrix(n4, nrow=2) # By default, byrow = FALSE # The 2x2 table is assigned to the name "ct" ct # To check the 2x2 table # Alternative ways of generating a contingency table directly from the dataframe: # ct <- table(infert$induced, infert$case) # This will generate a 3x2 table # Following results can be obtained with base R: fisher.test(ct) chisq.test(ct) # pwr: power analysis # If not installed, for installation, use the following command: # install.packages("pwr") library("pwr") pwr.chisq.test(w = 0.1, N = sum(n4), df = 1, sig.level = 0.05) # w = 0.1 denotes small effect size (see below) pwr.chisq.test(w = 0.3, N = sum(n4), df = 1, sig.level = 0.05) # w = 0.3 denotes medium effect size (see below) pwr.chisq.test(w = 0.5, N = sum(n4), df = 1, sig.level = 0.05) # w = 0.5 denotes large effect size (see below) # w is the effect size, N is the total sample size, and df is the degrees of freedom # Cohen suggests that w values of 0.1, 0.3, and 0.5 represent small, medium, and large effect sizes, respectively # epiR: sensitivity, specificity, PPV/NPV calculation # If not installed, for installation, use the following command: # install.packages("epiR") library("epiR") epi.tests(ct, conf.level = 0.95) epi.2by2(dat = as.table(ct), conf.level = 0.95) # Generate a ROC curve: # If not installed already, install "psych" package: # install.packages("psych") library("psych") AUC(n4) # Association measures # FROM: https://www.rdocumentation.org/packages/DescTools/versions/0.99.19/topics/Association%20measures # Package "DescTools": https://www.rdocumentation.org/packages/DescTools/versions/0.99.19/topics/Association%20measures # If not installed already, install "DescTools" package: # install.packages("DescTools") library("DescTools") Phi(ct) ContCoef(ct) CramerV(ct, conf.level = 0.95) TschuprowT(ct) YuleQ(ct) YuleY(ct) Assocs(ct) Desc(ct) # Power of Chi-squared test # SEE: DescTools::power.chisq.test(): https://www.rdocumentation.org/packages/DescTools/versions/0.99.19/topics/power.chisq.test