options(scipen=999)
options(digits=9)
cat("\014") # clear screen
rm(list=ls(all=TRUE))

M <- as.table(rbind(c(431,14), c(291,14)))
dimnames(M) <- list(Vaccine = c("Yes", "No"),
                    Effect = c("Positive","Negative"))
(Xsq <- chisq.test(M,correct = FALSE))  # Prints test summary
Xsq$observed   # observed counts (same as M)
Xsq$expected   # expected counts under the null
Xsq$residuals  # Pearson residuals
Xsq$stdres     # standardized residuals

row1=c(431,14)
row2=c(291,14)
M=as.table(rbind(row1,row2))


dimnames(M) <- list(Vaccine = c("Yes", "No"),
                    Defect = c("Positive","Negative"))
(Xsq <- chisq.test(M))  # Prints test summary
Xsq$observed   # observed counts (same as M)
Xsq$expected   # expected counts under the null
Xsq$residuals  # Pearson residuals
Xsq$stdres     # standardized residuals

row1=c(75,25)
row2=c(65,35)
M=as.table(rbind(row1,row2))


dimnames(M) <- list(Vaccine = c("Yes", "No"),
                    Defect = c("Positive","Negative"))
(Xsq <- chisq.test(M))  # Prints test summary
Xsq$observed   # observed counts (same as M)
Xsq$expected   # expected counts under the null
Xsq$residuals  # Pearson residuals
Xsq$stdres     # standardized residuals





## From Agresti(2007) p.39
M <- as.table(rbind(c(182,215,203),c(154,136,110)))
dimnames(M) <- list(gender = c("F", "M"),
                    party = c("Democrat","Independent", "Republican"))
(Xsq <- chisq.test(M))  # Prints test summary
Xsq$observed   # observed counts (same as M)
Xsq$expected   # expected counts under the null
Xsq$residuals  # Pearson residuals
Xsq$stdres     # standardized residuals

options(scipen=999)
options(digits=10)
cat("\014") # clear screen
rm(list=ls(all=TRUE))

row1=c(431,14)
row2=c(291,14)
o=as.table(rbind(row1,row2))
M <- as.table(rbind(row1,row2),correct=FALSE)
dimnames(M) <- list(Vaccine = c("Yes", "No"),
                    Effect = c("Positive","Negative"))
(Xsq <- chisq.test(M,correct = FALSE))  # Prints test summary
Xsq$observed   # observed counts (same as M)
Xsq$expected   # expected counts under the null
Xsq$residuals  # Pearson residuals
Xsq$stdres     # standardized residuals

options(digits=10)
cat("\014") # clear screen

row1=c(431,14)
row2=c(291,14)
obs=rbind(row1,row2)
M <- as.table(obs)
dimnames(M) <- list(Vaccine = c("Yes","No"),
                    Effect = c("Positive","Negative"))
Xsq <- chisq.test(M,correct = FALSE)  # Prints test summary
Xsq
Xsq$observed  # observed counts (same as M)
Xsq$expected  # expected counts under the null


row1=c(431,14)
row2=c(291,14)
obs=rbind(row1,row2)
chisq.test(obs,correct = FALSE)
