简体   繁体   English

在 R 中为 ANOVA 设置对比度的问题

[英]Problems with setting contrasts for ANOVA in R

For testing a specific hypothesis, I am trying to contrast a factor in R.为了测试一个特定的假设,我试图对比 R 中的一个因素。

set.seed(24)
data <- data.frame(var = sample(1:100, 70, replace = TRUE),
              version = rep(c("v3", "v4", "v1", "v3", "v4","v2","v2"),times=10)) 

c1 <- c(1/3, -1, 1/3, 1/3) 
c2 <- c(0, -1, 1, 0) 
c3 <- c(0, -1, 0, 1)
c4 <- c(1, -1, 0, 0) 

mat <- cbind(c1, c2, c3, c4)

contrasts(data$version, how.many = 4) <- mat

model <- aov(var ~ version, data = data)

summary.aov(model, split=list(version=list("comparison1"=1,"comparison2"= 2,
                                       "comparison3"=3,"comparison4"= 4))) 

Why is there no result for comparison 4?为什么比较4没有结果? How can I fix that?我该如何解决? Thanks.谢谢。

We specify the how.many parameter and it should work.我们指定了how.many参数,它应该可以工作。 According to ?contrasts根据?contrasts

how.many -How many contrasts should be made. how.many - 应该进行多少对比。 Defaults to one less than the number of levels of x.默认为比 x 的级别数少 1。 This need not be the same as the number of columns of value.这不必与值的列数相同。

So, it is the default behavior we observe while doing the assignment without any how.many parameter因此,这是我们在没有任何how.many参数的情况下进行赋值时观察到的默认行为

contrasts(data$var, how.many = 4) <- mat
contrasts(data$var)
#             c1 c2 c3 c4
#var1  0.3333333  0  0  1
#var2 -1.0000000 -1 -1 -1
#var3  0.3333333  1  0  0
#var4  0.3333333  0  1  0

data数据

set.seed(24)
data <- data.frame(var = sample(paste0("var", 1:4), 20, replace = TRUE))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM