R:隐藏 lm 显示的控制变量
R: Hide control variables from lm display
我想做一个线性回归,y
是我的因变量,x1 x2 x3
是我的自变量。我也有 "control" 个变量 z1 z2 z3
,我想包含但不显示结果。
summary(lm(y~x1+x2+x3+z1+z2+z3))
有没有办法让汇总不显示控制变量的系数?
正如@MrFlick 所建议的那样,使用 summary.lm 会有帮助。
代码:
# Build Sample Data
df <- data.frame(y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100), x3 = rnorm(100), z1 = rnorm(100), z2 = rnorm(100), z3 = rnorm(100))
# Run Model
sum <- summary.lm(lm(y ~ x1 + x2 + x3 + z1 + z2 + z3, data = df))
# Remove z1:z3
sum$coefficients <- sum$coefficients[1:4,]
# Print Results
print(sum)
输出:
Call:
lm(formula = y ~ x1 + x2 + x3 + z1 + z2 + z3, data = df)
Residuals:
Min 1Q Median 3Q Max
-2.76472 -0.56958 -0.02673 0.50188 2.61362
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.18092 0.09966 1.815 0.0727 .
x1 0.12282 0.10231 1.201 0.2330
x2 -0.22411 0.10781 -2.079 0.0404 *
x3 -0.01096 0.09554 -0.115 0.9090
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.9596 on 93 degrees of freedom
Multiple R-squared: 0.07717, Adjusted R-squared: 0.01763
F-statistic: 1.296 on 6 and 93 DF, p-value: 0.2667
我想做一个线性回归,y
是我的因变量,x1 x2 x3
是我的自变量。我也有 "control" 个变量 z1 z2 z3
,我想包含但不显示结果。
summary(lm(y~x1+x2+x3+z1+z2+z3))
有没有办法让汇总不显示控制变量的系数?
正如@MrFlick 所建议的那样,使用 summary.lm 会有帮助。
代码:
# Build Sample Data
df <- data.frame(y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100), x3 = rnorm(100), z1 = rnorm(100), z2 = rnorm(100), z3 = rnorm(100))
# Run Model
sum <- summary.lm(lm(y ~ x1 + x2 + x3 + z1 + z2 + z3, data = df))
# Remove z1:z3
sum$coefficients <- sum$coefficients[1:4,]
# Print Results
print(sum)
输出:
Call:
lm(formula = y ~ x1 + x2 + x3 + z1 + z2 + z3, data = df)
Residuals:
Min 1Q Median 3Q Max
-2.76472 -0.56958 -0.02673 0.50188 2.61362
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.18092 0.09966 1.815 0.0727 .
x1 0.12282 0.10231 1.201 0.2330
x2 -0.22411 0.10781 -2.079 0.0404 *
x3 -0.01096 0.09554 -0.115 0.9090
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.9596 on 93 degrees of freedom
Multiple R-squared: 0.07717, Adjusted R-squared: 0.01763
F-statistic: 1.296 on 6 and 93 DF, p-value: 0.2667