library(readr)
## Warning: package 'readr' was built under R version 3.6.1
mice <- read_csv("~/RPy Analytics/Dr Manatsa/mice.csv")
## Parsed with column specification:
## cols(
##   `Lab Number` = col_character(),
##   drug = col_character(),
##   status = col_character(),
##   tem_gene = col_character()
## )
#View(mice)
attach(mice)
library(gmodels)
## Warning: package 'gmodels' was built under R version 3.6.1
library(Rmisc)
## Warning: package 'Rmisc' was built under R version 3.6.1
## Loading required package: lattice
## Loading required package: plyr
## Warning: package 'plyr' was built under R version 3.6.1
drug=as.factor(drug)
status=as.factor(status)
tem_gene=as.factor(tem_gene)
mytable <- table(drug,tem_gene) # A will be rows, B will be columns 
mytable # print table 
##                              tem_gene
## drug                          gene absent gene present
##   Amoxycillin/Clavulinic acid          30           10
##   Ampicillin                           30           10
##   Cefotaxime                           30           10
##   Ceftazidime.                         30           10
##   Imipenem                             30           10
margin.table(mytable, 1) # A frequencies (summed over B) 
## drug
## Amoxycillin/Clavulinic acid                  Ampicillin 
##                          40                          40 
##                  Cefotaxime                Ceftazidime. 
##                          40                          40 
##                    Imipenem 
##                          40
margin.table(mytable, 2) # B frequencies (summed over A)
## tem_gene
##  gene absent gene present 
##          150           50
prop.table(mytable) # cell percentages
##                              tem_gene
## drug                          gene absent gene present
##   Amoxycillin/Clavulinic acid        0.15         0.05
##   Ampicillin                         0.15         0.05
##   Cefotaxime                         0.15         0.05
##   Ceftazidime.                       0.15         0.05
##   Imipenem                           0.15         0.05
prop.table(mytable, 1) # row percentages 
##                              tem_gene
## drug                          gene absent gene present
##   Amoxycillin/Clavulinic acid        0.75         0.25
##   Ampicillin                         0.75         0.25
##   Cefotaxime                         0.75         0.25
##   Ceftazidime.                       0.75         0.25
##   Imipenem                           0.75         0.25
prop.table(mytable, 2) # column percentages 
##                              tem_gene
## drug                          gene absent gene present
##   Amoxycillin/Clavulinic acid         0.2          0.2
##   Ampicillin                          0.2          0.2
##   Cefotaxime                          0.2          0.2
##   Ceftazidime.                        0.2          0.2
##   Imipenem                            0.2          0.2
# 2-Way Frequency Table 
mytable <- table(status,tem_gene) # A will be rows, B will be columns 
mytable # print table 
##                 tem_gene
## status           gene absent gene present
##   resistance              20           14
##   susceptibility         130           36
margin.table(mytable, 1) # A frequencies (summed over B) 
## status
##     resistance susceptibility 
##             34            166
margin.table(mytable, 2) # B frequencies (summed over A)
## tem_gene
##  gene absent gene present 
##          150           50
prop.table(mytable) # cell percentages
##                 tem_gene
## status           gene absent gene present
##   resistance            0.10         0.07
##   susceptibility        0.65         0.18
prop.table(mytable, 1) # row percentages 
##                 tem_gene
## status           gene absent gene present
##   resistance       0.5882353    0.4117647
##   susceptibility   0.7831325    0.2168675
prop.table(mytable, 2) # column percentages 
##                 tem_gene
## status           gene absent gene present
##   resistance       0.1333333    0.2800000
##   susceptibility   0.8666667    0.7200000
#Mantel-Haenszel chi-squared test with continuity correction

mantelhaen.test(drug,status,tem_gene)
## 
##  Cochran-Mantel-Haenszel test
## 
## data:  drug and status and tem_gene
## Cochran-Mantel-Haenszel M^2 = 48.458, df = 4, p-value = 7.575e-10
# 3-Way Frequency Table
mytable <- xtabs(~drug+status+tem_gene, data=mice)
ftable(mytable) # print table 
##                                            tem_gene gene absent gene present
## drug                        status                                          
## Amoxycillin/Clavulinic acid resistance                        4            2
##                             susceptibility                   26            8
## Ampicillin                  resistance                       10           10
##                             susceptibility                   20            0
## Cefotaxime                  resistance                        6            2
##                             susceptibility                   24            8
## Ceftazidime.                resistance                        0            0
##                             susceptibility                   30           10
## Imipenem                    resistance                        0            0
##                             susceptibility                   30           10
summary(mytable) # chi-square test of indepedence 
## Call: xtabs(formula = ~drug + status + tem_gene, data = mice)
## Number of cases in table: 200 
## Number of factors: 3 
## Test for independence of all factors:
##  Chisq = 71.49, df = 13, p-value = 4.271e-10
##  Chi-squared approximation may be incorrect
## cross tabultaions using gmodel package
## cardiovascular deseases
CrossTable(status,tem_gene) 
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  200 
## 
##  
##                | tem_gene 
##         status |  gene absent | gene present |    Row Total | 
## ---------------|--------------|--------------|--------------|
##     resistance |           20 |           14 |           34 | 
##                |        1.186 |        3.559 |              | 
##                |        0.588 |        0.412 |        0.170 | 
##                |        0.133 |        0.280 |              | 
##                |        0.100 |        0.070 |              | 
## ---------------|--------------|--------------|--------------|
## susceptibility |          130 |           36 |          166 | 
##                |        0.243 |        0.729 |              | 
##                |        0.783 |        0.217 |        0.830 | 
##                |        0.867 |        0.720 |              | 
##                |        0.650 |        0.180 |              | 
## ---------------|--------------|--------------|--------------|
##   Column Total |          150 |           50 |          200 | 
##                |        0.750 |        0.250 |              | 
## ---------------|--------------|--------------|--------------|
## 
## 
CrossTable(drug,tem_gene) 
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  200 
## 
##  
##                             | tem_gene 
##                        drug |  gene absent | gene present |    Row Total | 
## ----------------------------|--------------|--------------|--------------|
## Amoxycillin/Clavulinic acid |           30 |           10 |           40 | 
##                             |        0.000 |        0.000 |              | 
##                             |        0.750 |        0.250 |        0.200 | 
##                             |        0.200 |        0.200 |              | 
##                             |        0.150 |        0.050 |              | 
## ----------------------------|--------------|--------------|--------------|
##                  Ampicillin |           30 |           10 |           40 | 
##                             |        0.000 |        0.000 |              | 
##                             |        0.750 |        0.250 |        0.200 | 
##                             |        0.200 |        0.200 |              | 
##                             |        0.150 |        0.050 |              | 
## ----------------------------|--------------|--------------|--------------|
##                  Cefotaxime |           30 |           10 |           40 | 
##                             |        0.000 |        0.000 |              | 
##                             |        0.750 |        0.250 |        0.200 | 
##                             |        0.200 |        0.200 |              | 
##                             |        0.150 |        0.050 |              | 
## ----------------------------|--------------|--------------|--------------|
##                Ceftazidime. |           30 |           10 |           40 | 
##                             |        0.000 |        0.000 |              | 
##                             |        0.750 |        0.250 |        0.200 | 
##                             |        0.200 |        0.200 |              | 
##                             |        0.150 |        0.050 |              | 
## ----------------------------|--------------|--------------|--------------|
##                    Imipenem |           30 |           10 |           40 | 
##                             |        0.000 |        0.000 |              | 
##                             |        0.750 |        0.250 |        0.200 | 
##                             |        0.200 |        0.200 |              | 
##                             |        0.150 |        0.050 |              | 
## ----------------------------|--------------|--------------|--------------|
##                Column Total |          150 |           50 |          200 | 
##                             |        0.750 |        0.250 |              | 
## ----------------------------|--------------|--------------|--------------|
## 
## 
CrossTable(drug,status) 
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  200 
## 
##  
##                             | status 
##                        drug |     resistance | susceptibility |      Row Total | 
## ----------------------------|----------------|----------------|----------------|
## Amoxycillin/Clavulinic acid |              6 |             34 |             40 | 
##                             |          0.094 |          0.019 |                | 
##                             |          0.150 |          0.850 |          0.200 | 
##                             |          0.176 |          0.205 |                | 
##                             |          0.030 |          0.170 |                | 
## ----------------------------|----------------|----------------|----------------|
##                  Ampicillin |             20 |             20 |             40 | 
##                             |         25.624 |          5.248 |                | 
##                             |          0.500 |          0.500 |          0.200 | 
##                             |          0.588 |          0.120 |                | 
##                             |          0.100 |          0.100 |                | 
## ----------------------------|----------------|----------------|----------------|
##                  Cefotaxime |              8 |             32 |             40 | 
##                             |          0.212 |          0.043 |                | 
##                             |          0.200 |          0.800 |          0.200 | 
##                             |          0.235 |          0.193 |                | 
##                             |          0.040 |          0.160 |                | 
## ----------------------------|----------------|----------------|----------------|
##                Ceftazidime. |              0 |             40 |             40 | 
##                             |          6.800 |          1.393 |                | 
##                             |          0.000 |          1.000 |          0.200 | 
##                             |          0.000 |          0.241 |                | 
##                             |          0.000 |          0.200 |                | 
## ----------------------------|----------------|----------------|----------------|
##                    Imipenem |              0 |             40 |             40 | 
##                             |          6.800 |          1.393 |                | 
##                             |          0.000 |          1.000 |          0.200 | 
##                             |          0.000 |          0.241 |                | 
##                             |          0.000 |          0.200 |                | 
## ----------------------------|----------------|----------------|----------------|
##                Column Total |             34 |            166 |            200 | 
##                             |          0.170 |          0.830 |                | 
## ----------------------------|----------------|----------------|----------------|
## 
##