LM交互使用*和省略了不同的级别:

时间:2019-06-19 16:20:57

标签: r regression lm

我正在运行具有交互作用的线性模型。

当我使用星号*交互项时,省略第一级(以防止多重共线性)。
但是,当我使用冒号:交互项时,省略了最后一个级别。我希望它保持一致并删除相同的级别。

当我手动将第一个级别移动到最后一个级别时,我得到了预期的结果。

library(data.table)
library(forcats)
library(stargazer)
dt<-data.table("id"=as.factor(rep(c("a","b","c","d"),2)),
               "y"=rnorm(20),
               "time"=as.factor(rep(1:5,each=4)))
dt$treat<-as.factor(ifelse(dt$id=="a"|dt$id=="b",1,0))


lm1<-lm(y~factor(id)+factor(time)*factor(treat),dt)
lm2.wrong<-lm(y~factor(id)+factor(time):factor(treat),dt)
### Here is my Fix
dt$time<-fct_relevel(dt$time,"1", after = Inf) # ":" Interaction removes the last level...
lm2.right<-lm(y~factor(id)+factor(time):factor(treat),dt)
stargazer(lm1,lm2.wrong,lm2.right,type = "text")

以下是结果:

==========================================================
                                  Dependent variable:     
                             -----------------------------
                                           y              
                                (1)       (2)       (3)   
----------------------------------------------------------
factor(id)b                   -0.074    -0.074    -0.074  
                              (0.481)   (0.481)   (0.481) 

factor(id)c                   -0.619     1.044    -0.619  
                              (0.833)   (0.833)   (0.833) 

factor(id)d                   -0.019    1.644*    -0.019  
                              (0.833)   (0.833)   (0.833) 

factor(time)2                 -0.425                      
                              (0.760)                     

factor(time)3                  0.036                      
                              (0.760)                     

factor(time)4                  0.995                      
                              (0.760)                     

factor(time)5                 -0.061                      
                              (0.760)                     

factor(treat)1                                            


factor(time)1:factor(treat)0             0.061            
                                        (0.760)           

factor(time)2:factor(treat)0            -0.364    -0.425  
                                        (0.760)   (0.760) 

factor(time)3:factor(treat)0             0.098     0.036  
                                        (0.760)   (0.760) 

factor(time)4:factor(treat)0             1.056     0.995  
                                        (0.760)   (0.760) 

factor(time)5:factor(treat)0                      -0.061  
                                                  (0.760) 

factor(time)1:factor(treat)1            1.724*            
                                        (0.760)           

factor(time)2:factor(treat)1   0.090     1.389    -0.335  
                              (1.075)   (0.760)   (0.760) 

factor(time)3:factor(treat)1   0.180    1.941**    0.217  
                              (1.075)   (0.760)   (0.760) 

factor(time)4:factor(treat)1  -2.462*    0.257    -1.467* 
                              (1.075)   (0.760)   (0.760) 

factor(time)5:factor(treat)1  -1.663              -1.724* 
                              (1.075)             (0.760) 

Constant                       0.744    -0.980     0.744  
                              (0.589)   (0.589)   (0.589) 

----------------------------------------------------------
Observations                    20        20        20    
R2                             0.695     0.695     0.695  
Adjusted R2                    0.275     0.275     0.275  
Residual Std. Error (df = 8)   0.760     0.760     0.760  
F Statistic (df = 11; 8)       1.656     1.656     1.656  
==========================================================
Note:                          *p<0.1; **p<0.05; ***p<0.01

我希望前两个规范省略第一级(factor(time)1)。无需手动修改级别。有什么系统的方法吗?

0 个答案:

没有答案