【问题标题】:Interaction between continuous and categorical variable in R: is there a way to include all categories?R中连续变量和分类变量之间的交互:有没有办法包括所有类别?
【发布时间】:2019-06-16 04:35:49
【问题描述】:

有没有办法使用 R 运行线性回归,其中包含连续变量和分类变量之间的交互项,但不包括连续变量本身?

我正在研究住房租金和居住面积之间的关系。我的数据集中有四个不同的区域,我假设它们之间的关系是不同的。我在region 上使用rent 的线性回归以及floorspaceregion 之间的交互,我想在region 和交互项上使用系数,但使用lm 和交互项力@987654327 @ 也显示为自变量。

事情是这样的:

lm(formula = rent ~ factor(region) + factor(region) * floorspace, 
    data = mydataset)

Coefficients:
                                 Estimate Std. Error t value Pr(>|t|)    
(Intercept)                       4.67252    0.06792  68.792  < 2e-16 ***
factor(region)2                  -0.39859    0.09453  -4.216 2.52e-05 ***
factor(region)3                  -0.23631    0.17870  -1.322 0.186078    
factor(region)4                  -0.49076    0.10329  -4.751 2.07e-06 ***
floorspace                       -0.38658    0.01539 -25.119  < 2e-16 ***
factor(region)2:floorspace        0.20481    0.02145   9.550  < 2e-16 ***
factor(region)3:floorspace       -0.00884    0.03987  -0.222 0.824552    
factor(region)4:floorspace        0.08022    0.02348   3.416 0.000638 ***

我想要的是这个:

Coefficients:
                                 Estimate Std. Error t value Pr(>|t|)    
(Intercept)                       4.67252    0.06792  68.792  < 2e-16 ***
factor(region)2                  -0.39859    0.09453  -4.216 2.52e-05 ***
factor(region)3                  -0.23631    0.17870  -1.322 0.186078    
factor(region)4                  -0.49076    0.10329  -4.751 2.07e-06 ***
factor(region)1:floorspace       -0.38658    0.01539 -25.119  < 2e-16 ***
factor(region)2:floorspace       -0.18177    ???????   ?????  ??????? 
factor(region)3:floorspace       -0.39543    ???????   ?????  ???????    
factor(region)4:floorspace       -0.30636    ???????   ?????  ??????? 

原因是,从解释的角度来看,单独显示每个区域的 floorspace 效果更有意义,而不是显示 region=1floorspace 的效果,其余部分作为效果之间的差异给定区域和region=1

【问题讨论】:

  • 使用:mydataset = data.frame(region=sample(1:4, 100,TRUE), floorspace=runif(100))制作测试数据集

标签: r linear-regression categorical-data interaction


【解决方案1】:

首先我将创建一个测试数据集:mydataset = data.frame(rent=runif(100), region=sample(1:4, 100,TRUE), floorspace=runif(100))

通过减法将floorspace中的线性项从公式中取出:

    summary(lm(formula = rent ~ factor(region) + factor(region) * floorspace - floorspace, data=mydataset))

    Call:
    lm(formula = rent ~ factor(region) + factor(region) * floorspace - 
        floorspace, data = mydataset)

    Residuals:
         Min       1Q   Median       3Q      Max 
    -0.52917 -0.26151  0.01225  0.24816  0.52392 

    Coefficients:
                               Estimate Std. Error t value Pr(>|t|)    
    (Intercept)                 0.50329    0.09238   5.448 4.23e-07 ***
    factor(region)2             0.01331    0.13804   0.096    0.923    
    factor(region)3             0.05716    0.16860   0.339    0.735    
    factor(region)4            -0.03252    0.16234  -0.200    0.842    
    factor(region)1:floorspace  0.16273    0.22805   0.714    0.477    
    factor(region)2:floorspace  0.01638    0.19894   0.082    0.935    
    factor(region)3:floorspace -0.14251    0.20262  -0.703    0.484    
    factor(region)4:floorspace -0.05094    0.24191  -0.211    0.834    

【讨论】:

  • 公式中的A*B 不是扩展为A + B + A:B 吗?你可以简单地做factor(region) + factor(region) : floorspace
  • 谢谢,它有效!我注意到我不应该将-floorspace 放在任何地方。至少,如果我把它放在交互项之前,上面的代码是行不通的。还有其他需要考虑的限制吗?
  • 我认为@RuiBarradas 对于相同的型号规格有一个更简洁的公式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 2014-02-19
  • 1970-01-01
  • 1970-01-01
  • 2020-02-19
  • 1970-01-01
相关资源
最近更新 更多