【问题标题】:User defined contrast codes contrast missing from output in RR中的输出中缺少用户定义的对比代码对比
【发布时间】:2021-10-29 15:46:49
【问题描述】:

我想创建三个对比代码以包含在 R 中的回归/ANOVA 分析中。

我尝试了两种不同的方法,虽然代码出现在对比矩阵中,但它没有出现在我的回归/ANOVA 输出中

这是我用来创建对比并将它们分配给感兴趣的条件的一种方法:

c1 <- c(1, 0, -1)
c2 <- c(1, -2, 1)
c3 <- c(2, -1, -1)

mat <- cbind(c1,c2,c3)
contrasts(df$condition) <- mat

然后我在回归分析中使用它:

summary(lm(Diff ~ condition, df))

这是我输出的系数:

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  42.6679     0.4249 100.425   <2e-16 ***
conditionc1   1.0108     0.5169   1.955   0.0524 .  
conditionc2   0.7829     0.3024   2.589   0.0106 *  

我期待的是 c1、c2 和 c3 的系数,但我不明白为什么我不能生成这个。

以下是我正在处理的数据:

structure(list(condition = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L), .Label = c("prompt_explicit", "prompt_gen", "prompt_less"
), contrasts = structure(c(1, 0, -1, 1, -2, 1), .Dim = 3:2, .Dimnames = list(
    c("prompt_explicit", "prompt_gen", "prompt_less"), c("c1", 
    "c2"))), class = "factor"), Diff = c(44L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 60L, 44L, 50L, 40L, 40L, 40L, 60L, 
40L, 44L, 40L, 40L, 52L, 50L, 50L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 50L, 40L, 60L, 42L, 42L, 40L, 48L, 40L, 60L, 40L, 
40L, 40L, 44L, 50L, 50L, 60L, 42L, 60L, 50L, 40L, 40L, 40L, 56L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 60L, 50L, 40L, 44L, 40L, 60L, 40L, 42L, 
50L, 50L, 42L, 46L, 40L, 40L, 50L, 40L, 40L, 40L, 44L, 42L, 40L, 
40L, 40L, 40L, 40L, 40L, 46L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 42L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 42L, 40L, 48L, 46L, 
50L, 44L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 50L, 52L, 
40L, 40L, 40L, 40L)), row.names = c(NA, -151L), class = "data.frame")

这里有什么我遗漏的吗?

【问题讨论】:

    标签: r regression anova


    【解决方案1】:

    您的因子有 3 个水平,应该是 2 个对比度水平,因为线性模型将包含截距。由于您指定了三个,因此只使用前两个。

    我猜你想测试你的具体假设,所以你可以先拟合模型:

    contrasts(df$condition) = NULL
    fit = lm(Diff ~ condition, df)
    

    然后提供你要测试的对比矩阵:

    library(multcomp)
    summary(glht(fit,linfct=t(mat)))
    
     Simultaneous Tests for General Linear Hypotheses
    
    Fit: lm(formula = Diff ~ condition, data = df)
    
    Linear Hypotheses:
            Estimate Std. Error t value Pr(>|t|)    
    c1 == 0   46.483      1.625   28.61   <2e-16 ***
    c2 == 0   49.159      2.205   22.29   <2e-16 ***
    c3 == 0   94.304      3.079   30.62   <2e-16 ***
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    (Adjusted p values reported -- single-step method)
    

    【讨论】:

      【解决方案2】:

      对于具有三个级别的因素来说,这绝对是正常情况。如果你想要c1, c2, c3,那么你可以使用contrasts(df$condition, 3) &lt;- mat,但在回归之后,它将返回NA's for c3

      【讨论】:

      • 好的,谢谢 - 你知道这是为什么吗?我在网上看到的一切都表明这会起作用,但它不会。
      • stats.stackexchange.com/questions/190427/… 此链接可能对您有所帮助。它对比 1-2、1-3,其中 1、2、3 是因子的三个级别,因此只需要 c1、c2
      猜你喜欢
      • 2020-10-27
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 2013-08-12
      • 1970-01-01
      相关资源
      最近更新 更多