This post is solutions of chapter 4 of `A Handbook of Statistical Analyses Using R, Second Edition'.
このポストはRによる統計解析ハンドブック(第2版)の第4章の解答になります。
1
Test based on chi-square can't reject independency of region and site of lesion, but Fisher's exact test rejects that.
カイ2乗値に基づく検定では地域と病変部位は独立でないと言えないが、Fisherの正確検定では棄却される。
data("orallesions", package="HSAUR2")
chisq.test(orallesions) # p-value = 0.1096, accept
fisher.test(orallesions) # p-value = 0.01904, reject
2
labels in graph warp each other, so should I change row names of the dataframe to avoid wapping in advance?
グラフのラベルが重なるのですが、予めrownames関数でデータフレームの行名を変えなければならないのでしょうか。
library("vcd")
data("orallesions", package="HSAUR2")
assoc(orallesions)
mosaic(orallesions)
3
Difference of p-value is derived from the that of mean. To check this, boxplot is helpful. By executing sum(p.diff>0) in R, it turns out that elements of p.diff is always larger than 0.
違いは平均値の差の大きさに由来する。これを確認するためには箱ひげ図が便利である。Rでsum(p.diff>0)とすることで、すべて0より大きな値をとっていることが分かる。
library("coin")
n.test <- 100 # number of test
p.diff <- matrix(nrow=n.test, ncol=4) # matrix to store the difference of p-value
colnames(p.diff) <- c("diff=0.5", "diff=1", "diff=1.5", "diff=2")
sz <- 12 # 15 is upper limit on my computer. Be sure to make sz less than 15!
for( i in 1:n.test ){
for( j in 1:4 ){
g1 <- rnorm(sz, mean=0, sd=1)
g2 <- rnorm(sz, mean=j/2, sd=1)
lev <- c(rep(1, sz), rep(2, sz))
ddd <- c(g1, g2)
gs <- as.data.frame(cbind(ddd, lev))
colnames(gs) <c("value", "gr")
gs$gr <- factor(gs$gr)
p.diff[i, j] <pvalue(wilcox_test(value ~ gr, data=gs, distribution=exact()))
- pvalue(independence_test(value ~ gr, data=gs, distribution=exact()))
}
}
boxplot(p.diff)
