Set Working Directory
knitr::opts_knit$set(root.dir = normalizePath("C:\\Users\\Damian\\Dropbox\\Study\\Coursera\\Design of Experiments"))
Read in a data file with 2 response categories
prefsAB <- read.csv("prefsAB.csv")
View(prefsAB)
Convert to nomial factor
prefsAB$Subject <- factor(prefsAB$Subject)
summary(prefsAB)
Subject Pref
1 : 1 A:14
2 : 1 B:46
3 : 1
4 : 1
5 : 1
6 : 1
(Other):54
plot(prefsAB$Pref)
prfs <- xtabs(~Pref, data=prefsAB)
prfs
Pref
A B
14 46
chisq.test(prfs)
Chi-squared test for given probabilities
data: prfs
X-squared = 17.067, df = 1, p-value = 3.609e-05
The chi-squared test, the one sampled chi-square test is an asymptotic(점근성의) test. 다시 말해, p-value를 추측하는 것으로 데이터의 양이 많아질수록 나은 결과를 보인다. 주로 데이터가 1000개가 넘을 때 사용하기 좋다고 한다. 이에 대한 대안으로 the exact test(=binomial test)를 사용하도록 하자.
binom.test(prfs)
Exact binomial test
data: prfs
number of successes = 14, number of trials = 60,
p-value = 4.224e-05
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
0.1338373 0.3603828
sample estimates:
probability of success
0.2333333
Read in a data file with 3 response categories
prefsABC <- read.csv("prefsABC.csv")
View(prefsABC)
prefsABC$Subject <- factor(prefsABC$Subject)
summary(prefsABC)
prfs <- xtabs(~Pref, data=prefsABC)
chisq.test(prfs)
For multinomial test
library(XNomial)
xmulti(prfs, c(1/3, 1/3, 1/3), statName='Prob')
Post hoc binomial tests with correction for multiple comparisons (Pair wise differences)
aa <- binom.test(sum(prefsABC$Pref=="A"), nrow(prefsABC), p=1/3)
bb <- binom.test(sum(prefsABC$Pref=="B"), nrow(prefsABC), p=1/3)
cc <- binom.test(sum(prefsABC$Pref=="C"), nrow(prefsABC), p=1/3)
p.adjust(c(aa$p.value, bb$p.value, cc$p.value), method="holm")