top of page

#pp.188-190   クロス集計表から直接に

 

CT <- matrix(c(40,20,10,30), nrow=2,byrow=TRUE)

chisq.test(CT,correct=F)

 

#適合度の検定 pp.191-192   例6.5

chisq.test(c(4,13,12,11,10), p=c(8,15,30,30,17)/100)

chisq.test(c(17,12,11,10), p=c(23,30,30,17)/100)

 

#参考1 

#ローデータがある場合

x <- read.csv("Table651.csv", header=F)
x
attach(x)

#クロス集計表の作成
table(x)

CT <- table(x)
chisq.test(CT)

chisq.test(CT,correct=F)

#連関係数 ファイ係数(2×2のクロス集計表の場合:第4章参照)

#パッケージpsychを利用するならば

library(psych)

phi(CT)

#参考2:対応のある場合→マクネマー検定

mcnemar.test(matrix(c(40, 20, 10, 30), 2, 2), correct=FALSE) 

#連続性の補正あり

mcnemar.test(matrix(c(40, 20, 10, 30), 2, 2)) 

​#正確な確率を求めると

binom.test(c(20, 10)) # 二項検定による正確な有意確率

#参考3

#2行2列よりも大きなクロス集計表の検定:連関係数 Cramer's V

## From Agresti(2007) p.39

M <- as.table(rbind(c(762, 327, 468), c(484, 239, 477)))

dimnames(M) <- list(gender = c("F", "M"), party = c("Democrat","Independent", "Republican"))

M

(Xsq <- chisq.test(M)) # Prints test summary

#パッケージ confintr をインストール

library(confintr)
cramersv(Xsq)
 

#残差分析 zの大きさ1.96 以上で p< .05 

Xsq$stdres # standardized residuals

bottom of page