我试图确定两组之间具有 8 个水平的分类变量的频率是否存在显着差异。在这种情况下,两组人被问到他们最喜欢的颜色,共有 8 种选择。我想知道第 1 组中的人们选择颜色的频率与第 2 组中的人们选择相同颜色的频率是否存在显着差异。
也就是说,第 1 组中有 64.2% 的人选择了橙色,而第 2 组的这一比例为 53%。这个差异显着吗? 这是使用 tabpct() 的频率表
tabpct(all_data$Colors, all_data$Group, graph = F)
Column percent
all_data$Group
all_data$Colors Grp 1 % Grp 2 %
Red 3 (1.3) 2 (1.0)
Blue 19 (8.4) 10 (5.0)
Yellow 1 (0.4) 2 (1.0)
Green 4 (1.8) 5 (2.5)
Purple 1 (0.4) 2 (1.0)
Orange 145 (64.2) 106 (53.0)
Pink 1 (0.4) 1 (0.5)
Brown 52 (23.0) 72 (36.0)
Total 226 (100) 200 (100)
我确信有一种更简单的方法,但我似乎无法弄清楚。任何帮助将不胜感激!
我尝试对 Anova 进行建模并对其进行 TukeyHSD 测试,但尽管事实上不存在 NA、NaN、Inf 或 0,但还是出现了错误:
ColorComp <- aov(Color ~ Group, data = all_data)
TukeyHSD(ColorComp)
> Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
> NA/NaN/Inf in 'y'
> In addition: Warning message:
> In storage.mode(v) <- "double" : NAs introduced by coercion
我也尝试过回归,但出现同样的错误。
使用蒙特卡罗模拟的费舍尔检验表明了分布差异的边缘暗示证据:
read.table(text=txt, head=TRUE)
Colors Grp1 X. Grp2 X..1
1 Red 3 (1.3) 2 (1.0)
2 Blue 19 (8.4) 10 (5.0)
3 Yellow 1 (0.4) 2 (1.0)
4 Green 4 (1.8) 5 (2.5)
5 Purple 1 (0.4) 2 (1.0)
6 Orange 145 (64.2) 106 (53.0)
7 Pink 1 (0.4) 1 (0.5)
8 Brown 52 (23.0) 72 (36.0)
> dat <-read.table(text=txt, head=TRUE)
> fisher.test(dat[c(2,4)])
Fisher's Exact Test for Count Data
data: dat[c(2, 4)]
p-value = 0.06452
alternative hypothesis: two.sided
可以进行卡方检验,但有效性值得怀疑。
chisq.test(dat[c(2,4)])
Pearson's Chi-squared test
data: dat[c(2, 4)]
X-squared = 11.512, df = 7, p-value = 0.1178
Warning message:
In chisq.test(dat[c(2, 4)]) : Chi-squared approximation may be incorrect