英文:
Spread Error in the two-way repeated measure anova test
问题
我正在使用rstatix包进行双向重复测量的方差分析。以下是代码部分:
library(rstatix)
res.aov <- anova_test(data=kolding, dv=count, wid=id, within=treatment)
get_anova_table(res.aov)
这是数据的样子:
id treatment species count
1 K1 biohut Goldsinny Wrasse 0
2 K2 biohut Goldsinny Wrasse 2
3 K3 biohut Goldsinny Wrasse 1
4 K4 biohut Goldsinny Wrasse 1
5 K5 biohut Goldsinny Wrasse 1
6 K6 biohut Goldsinny Wrasse 0
7 K1 biohut Atlantic Cod 0
8 K2 biohut Atlantic Cod 0
9 K3 biohut Atlantic Cod 1
10 K4 biohut Atlantic Cod 1
11 K5 biohut Atlantic Cod 0
...
我一直在遇到一个“spread”错误,要求输出的每一行必须由唯一的键组合标识,但我不知道该如何处理它。
我尝试使用pivot_wider()
,但它没有效果。
英文:
I am working on a two-way repeated measure anova using the rstatix package.
library(rstatix)
res.aov <- anova_test(data=kolding, dv=count, wid=id, within=treatment)
get_anova_table(res.aov)
And here's how the data looks like:
id treatment species count
1 K1 biohut Goldsinny Wrasse 0
2 K2 biohut Goldsinny Wrasse 2
3 K3 biohut Goldsinny Wrasse 1
4 K4 biohut Goldsinny Wrasse 1
5 K5 biohut Goldsinny Wrasse 1
6 K6 biohut Goldsinny Wrasse 0
7 K1 biohut Atlantic Cod 0
8 K2 biohut Atlantic Cod 0
9 K3 biohut Atlantic Cod 1
10 K4 biohut Atlantic Cod 1
11 K5 biohut Atlantic Cod 0
...
I keep getting a spread error that each row of output must be identified by a unique combination of keys, and I don't know how to go about it.
I tried pivot_wider(), but it wouldn't budge.
答案1
得分: 0
问题在于您的id
变量用于不同的个体,即金线鳕鱼和大西洋鳕鱼都有K1
、K2
等等。您可以通过使用paste()
函数将id
和species
合并来创建唯一的标识符。
library(rstatix)
kolding$unique_id <- paste(kolding$id, kolding$species)
res.aov <- anova_test(data=kolding, dv=count, wid=unique_id, within=treatment)
get_anova_table(res.aov)
ANOVA表(III 类型检验)
效应 DFn DFd F p p<.05 ges
1 treatment 1 10 0.45 0.518 0.034
示例数据:
原始示例数据出现错误,因为它只包含了一个水平的treatment
,所以我添加了另一个水平:
set.seed(13)
kolding <- rbind(
kolding,
transform(
kolding,
treatment = "control",
count = sample(0:2, 11, replace = TRUE)
)
)
英文:
The problem is that your id
variable is reused for different individuals — ie, you have K1
, K2
, etc, for both goldsinny wrasse and Atlantic cod. You can make a unique identifier by paste()
ing id
and species
.
library(rstatix)
kolding$unique_id <- paste(kolding$id, kolding$species)
res.aov <- anova_test(data=kolding, dv=count, wid=unique_id, within=treatment)
get_anova_table(res.aov)
ANOVA Table (type III tests)
Effect DFn DFd F p p<.05 ges
1 treatment 1 10 0.45 0.518 0.034
Example data:
OP’s sample data initially threw an error because it only includes one level of treatment
, so I added another level:
set.seed(13)
kolding <- rbind(
kolding,
transform(
kolding,
treatment = "control",
count = sample(0:2, 11, replace = TRUE)
)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论