# Task 1 #Creation of data frame with the data flo.df<-data.frame(K=c(0,0,1,1), Flo=c(0,1,0,1), freq=c(31,33,13, 51)) summary(flo.df) #Conversion of data frame to a contingency table flo.tab<-xtabs(freq~K+Flo, data=flo.df) # Flo # K 0 1 # 0 31 33 # 1 13 51 # Chisq test test.res<-chisq.test(flo.tab) test.res # Pearson's Chi-squared test with Yates' # continuity correction # # data: chol.tab # X-squared = 47.289, df = 1, p-value = # 6.124e-12 test.res$stdres# Standardized residuals #showing the direction of the association # Flo # K 0 1 # 0 3.349743 -3.349743 # 1 -3.349743 3.349743 # Installing and loading the psych package for function phi install.packages("psych") library(psych) phi(flo.tab)#Phi coefficient - positive indicates # positive correlation # [1] 0.3 #Plotting a stacked barplot. geom_col needs to be used # if the counts are already counted # Be aware of the content of the aes() - how the x, y and fill are defined library(ggplot2) ggplot(flo.df, aes(x=as.character(K), y=freq, fill=as.character(Flo)))+ geom_col()+theme_classic() #Task 2 tea.df<-data.frame(tea=c(0,0,"g", "g", "b", "b"), cancer=rep(c(0,1),3), freq=c(130,55, 357, 65,160,45)) summary(tea.df) tea.tab<-xtabs(freq~tea+cancer, data=tea.df) # chisq.test(tea.tab) # Pearson's Chi-squared test # # data: tea.tab # X-squared = 16.756, df = 2, p-value # = 0.0002299 #Instalation and loading of epitools needed for epitab function # install.packages("epitools") library(epitools) # Epitab showing the comparisons of the odds ratios: # black vs. no tea # green vs. no tea epitab(tea.tab)$tab # cancer # tea 0 p0 1 p1 oddsratio # 0 130 0.2009274 55 0.3333333 1.0000000 # b 160 0.2472952 45 0.2727273 0.6647727 # g 357 0.5517774 65 0.3939394 0.4303540 # cancer # tea lower upper p.value # 0 NA NA NA # b 0.4209577 1.0498032 8.281283e-02 # g 0.2852035 0.6493768 9.265908e-05 ggplot(tea.df, aes(x=as.character(tea), y=freq, fill=as.character(cancer)))+ geom_col()+theme_classic() #4 chol<-data.frame(vacc=c(1,1,0,0), surv=c(1, 0, 1, 0), freq=c(55, 5, 15, 40)) summary(chol) chol.tab<-xtabs(freq~vacc+surv, data=chol) chisq.test(chol.tab) # Pearson's Chi-squared test with Yates' # continuity correction # # data: chol.tab # X-squared = 47.289, df = 1, p-value = # 6.124e-12 phi(chol.tab) # [1] 0.66 # Here, be careful about the correlation (found) vs. # causality. The question was formulated in a causal way. # Thus, it cannot be addressed by observational data. # An experiment would be needed.