【问题标题】:change contrasts of interaction term specified with colon in lm()更改 lm() 中用冒号指定的交互项的对比
【发布时间】:2019-01-22 03:55:51
【问题描述】:

是否可以使用冒号: 表示法更改lm 中指定的交互项的contrasts

在下面的示例中,参考类别默认为gear:vs 生成的六个术语中的最后一个(即gear5:vs1)。我希望它使用六个中的第一个作为参考(即gear3:vs0)。

mtcars.1 <- mtcars %>%
  mutate(gear = as.factor(gear)) %>%
  mutate(vs = as.factor(vs))

lm(data=mtcars.1, mpg ~ gear:vs) %>%
  tidy
#> # A tibble: 6 x 5
#>   term        estimate std.error statistic      p.value
#>   <chr>          <dbl>     <dbl>     <dbl>        <dbl>
#> 1 (Intercept)    30.4       4.13      7.36 0.0000000824
#> 2 gear3:vs0     -15.4       4.30     -3.57 0.00143     
#> 3 gear4:vs0      -9.40      5.06     -1.86 0.0747      
#> 4 gear5:vs0     -11.3       4.62     -2.44 0.0218      
#> 5 gear3:vs1     -10.1       4.77     -2.11 0.0447      
#> 6 gear4:vs1      -5.16      4.33     -1.19 0.245

分别为gearvs 指定对比度似乎没有效果:

lm(data=mtcars.1, mpg ~ gear:vs, 
             contrasts = list(gear = contr.treatment(n=3,base=3),
                              vs = contr.treatment(n=2,base=2))) %>%
  tidy
#> # A tibble: 6 x 5
#>   term        estimate std.error statistic      p.value
#>   <chr>          <dbl>     <dbl>     <dbl>        <dbl>
#> 1 (Intercept)    30.4       4.13      7.36 0.0000000824
#> 2 gear3:vs0     -15.4       4.30     -3.57 0.00143     
#> 3 gear4:vs0      -9.40      5.06     -1.86 0.0747      
#> 4 gear5:vs0     -11.3       4.62     -2.44 0.0218      
#> 5 gear3:vs1     -10.1       4.77     -2.11 0.0447      
#> 6 gear4:vs1      -5.16      4.33     -1.19 0.245

我不确定如何直接为gear:vs 指定对比度:

lm(data=mtcars.1, mpg ~ gear:vs,
             contrasts = list("gear:vs" = contr.treatment(n=6,base=6))) %>%
  tidy
#> Warning in model.matrix.default(mt, mf, contrasts): variable 'gear:vs' is
#> absent, its contrast will be ignored
#> # A tibble: 6 x 5
#>   term        estimate std.error statistic      p.value
#>   <chr>          <dbl>     <dbl>     <dbl>        <dbl>
#> 1 (Intercept)    30.4       4.13      7.36 0.0000000824
#> 2 gear3:vs0     -15.4       4.30     -3.57 0.00143     
#> 3 gear4:vs0      -9.40      5.06     -1.86 0.0747      
#> 4 gear5:vs0     -11.3       4.62     -2.44 0.0218      
#> 5 gear3:vs1     -10.1       4.77     -2.11 0.0447      
#> 6 gear4:vs1      -5.16      4.33     -1.19 0.245

reprex package (v0.2.1) 于 2019-01-21 创建

【问题讨论】:

    标签: r regression lm


    【解决方案1】:

    解决此问题的一种方法是在回归之前预先计算交互项。

    为了演示,我们可以在mtcars 中创建一个因子列GV,其水平与您在lm 输出中观察到的水平相同。它生成相同的值:

    mtcars %>% 
      mutate(GV = interaction(factor(gear), factor(vs)), 
             GV = factor(GV, levels = c("5.1", "3.0", "4.0", "5.0", "3.1", "4.1"))) %>% 
      lm(mpg ~ GV, .) %>% 
      tidy() 
    
    # A tibble: 6 x 5
      term        estimate std.error statistic      p.value
      <chr>          <dbl>     <dbl>     <dbl>        <dbl>
    1 (Intercept)    30.4       4.13      7.36 0.0000000824
    2 GV3.0         -15.4       4.30     -3.57 0.00143     
    3 GV4.0          -9.4       5.06     -1.86 0.0747      
    4 GV5.0         -11.3       4.62     -2.44 0.0218      
    5 GV3.1         -10.1       4.77     -2.11 0.0447      
    6 GV4.1          -5.16      4.33     -1.19 0.245 
    

    现在我们省略第二个 mutate 术语,因此级别为 3.0、4.0、5.0、3.1、4.1、5.1。

    mtcars %>% 
      mutate(GV = interaction(factor(gear), factor(vs))) %>% 
      lm(mpg ~ GV, .) %>% 
      tidy()
    
      # A tibble: 6 x 5
      term        estimate std.error statistic  p.value
      <chr>          <dbl>     <dbl>     <dbl>    <dbl>
    1 (Intercept)    15.1       1.19     12.6  1.38e-12
    2 GV4.0           5.95      3.16      1.88 7.07e- 2
    3 GV5.0           4.08      2.39      1.71 9.96e- 2
    4 GV3.1           5.28      2.67      1.98 5.83e- 2
    5 GV4.1          10.2       1.77      5.76 4.61e- 6
    6 GV5.1          15.4       4.30      3.57 1.43e- 3
    

    使用interaction(factor(gear), factor(vs), lex.order = TRUE) 获取级别 3.0、3.1、4.0、4.1、5.0、5.1。

    mtcars %>% 
      mutate(GV = interaction(factor(gear), factor(vs), lex.order = TRUE)) %>%
      lm(mpg ~ GV, .) %>% 
      tidy()
    
    # A tibble: 6 x 5
      term        estimate std.error statistic  p.value
      <chr>          <dbl>     <dbl>     <dbl>    <dbl>
    1 (Intercept)    15.0       1.19     12.6  1.38e-12
    2 GV3.1           5.28      2.67      1.98 5.83e- 2
    3 GV4.0           5.95      3.16      1.88 7.07e- 2
    4 GV4.1          10.2       1.77      5.76 4.61e- 6
    5 GV5.0           4.07      2.39      1.71 9.96e- 2
    6 GV5.1          15.3       4.30      3.57 1.43e- 3
    

    【讨论】:

      猜你喜欢
      • 2021-09-01
      • 2019-02-27
      • 2012-03-09
      • 2020-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      相关资源
      最近更新 更多