添加虚拟变量会更改系数

时间:2018-04-20 08:12:27

标签: r linear-regression dummy-variable

是否应在线性模型中为其他解释变量添加虚拟变量变量系数? 我认为它只会改变截距,但系数也会因非拦截项而改变。

以下是包含mtcars数据的示例代码(来自: http://rstudio-pubs-static.s3.amazonaws.com/20516_29b941670a4b42688292b4bb892a660f.html

data(mtcars)
mtcars$am_text <- as.factor(mtcars$am)
levels(mtcars$am_text) <- c("Automatic", "Manual")


fit1 <- lm(mpg ~ am_text + wt, data = mtcars)
summary(fit1)

Call:
lm(formula = mpg ~ am_text + wt, data = mtcars)

Residuals:
    Min      1Q  Median      3Q     Max 
-4.5295 -2.3619 -0.1317  1.4025  6.8782 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept)   37.32155    3.05464  12.218 5.84e-13 ***
am_textManual -0.02362    1.54565  -0.015    0.988    
wt            -5.35281    0.78824  -6.791 1.87e-07 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.098 on 29 degrees of freedom
Multiple R-squared:  0.7528,    Adjusted R-squared:  0.7358 
F-statistic: 44.17 on 2 and 29 DF,  p-value: 1.579e-09

现在运行带有子集数据的线性模型:

# Here is without dummy variable, but now with subset data
fit2 <- lm(mpg ~ wt, data = mtcars[mtcars$am_text == "Automatic",])
summary(fit2)

Call:
lm(formula = mpg ~ wt, data = mtcars[mtcars$am_text == "Automatic",])

Residuals:
    Min      1Q  Median      3Q     Max 
-3.6004 -1.5227 -0.2168  1.4816  5.0610 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  31.4161     2.9467  10.661 6.01e-09 ***
wt           -3.7859     0.7666  -4.939 0.000125 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.528 on 17 degrees of freedom
Multiple R-squared:  0.5893,    Adjusted R-squared:  0.5651 
F-statistic: 24.39 on 1 and 17 DF,  p-value: 0.0001246

3 个答案:

答案 0 :(得分:1)

lm中,当使用普通最小二乘法(OLS)拟合模型时,最小化残差平方和(SSR),这是模型参数的函数。通常在OLS中,对参数没有约束。

因此,添加参数通常会导致不同的参数估计,因为OLS估计仅对应于最小化SSR的那些参数值。如果添加虚拟变量(或任何其他变量),lm将只返回导致最低SSR的参数估计值。在最小化过程中,所有参数值都可以自由变化。

有关详细信息,请查看例如the Wikipedia entry on OLS或任何统计资料。

答案 1 :(得分:1)

实际上,问题是fit1中的斜率系数实际上是自动和手动汽车的组合,即使每个因素都有自己的拦截。如果您在am_textwt之间包含互动字词(am_text:wt),那么您可以更好地与仅自动汽车(fit2)的模型进行比较。

fit3 <- lm(mpg ~ am_text + wt + am_text:wt, data = mtcars)
summary(fit3)

# Call:
# lm(formula = mpg ~ am_text * wt, data = mtcars)
# 
# Residuals:
#     Min      1Q  Median      3Q     Max 
# -3.6004 -1.5446 -0.5325  0.9012  6.0909 
# 
# Coefficients:
#                  Estimate Std. Error t value Pr(>|t|)    
# (Intercept)       31.4161     3.0201  10.402 4.00e-11 ***
# am_textManual     14.8784     4.2640   3.489  0.00162 ** 
# wt                -3.7859     0.7856  -4.819 4.55e-05 ***
# am_textManual:wt  -5.2984     1.4447  -3.667  0.00102 ** 
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 2.591 on 28 degrees of freedom
# Multiple R-squared:  0.833,   Adjusted R-squared:  0.8151 
# F-statistic: 46.57 on 3 and 28 DF,  p-value: 5.209e-11

现在注意fit3的系数包含自动车辆的截距和斜率,它们匹配fit2的系数:

coef(fit2) # fit only to automatic
# (Intercept)          wt 
#   31.416055   -3.785908 

coef(fit3)
# (Intercept)    am_textManual               wt am_textManual:wt 
#   31.416055        14.878423        -3.785908        -5.298360 

答案 2 :(得分:0)

是的,如果要在模型中添加变量,则应该预期系数会发生变化。请记住,任何变量的系数始终与模型中存在的其他变量有关。

如果您有Y = aX1 + bX2 + cX3 + E,并且您在模型中添加X4,则应该预期a,b和c将发生变化(除非X4对模型没有任何影响)。