我想进行一次线性回归,其中y
是我的因变量,x1 x2 x3
是我的自变量。 我还有“控制”变量z1 z2 z3
,我想将其包括在内,但不显示结果。
summary(lm(y~x1+x2+x3+z1+z2+z3))
有没有办法让摘要不显示我控制变量的系数?
# 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
summary.lm
对象中挑选出任何您想要的值,并以您喜欢的方式显示。请参见?summary.lm
帮助页面。如果默认打印方法允许您隐藏变量,那将会非常令人困惑,因此这是不可取的。 - MrFlick