抑制R中的固定效应系数

时间:2014-10-25 18:39:40

标签: r summary lm

有一种方法可以在使用summary()函数时抑制线性模型中固定效应的系数(例如stata中的absorb()函数的等价物)。例如,我希望汇总函数输出只是截距和x而不是因子的系数和标准误差:

frame <- data.frame(x = rnorm(100), y = rnorm(100), z = rep(c("A", "B", "C", "D"),25))
summary(lm(y~x + as.factor(z), data = frame)) 

Call:
lm(formula = y ~ x + as.factor(z), data = frame)

Residuals:
Min      1Q  Median      3Q     Max 
-2.2417 -0.6407  0.1783  0.5789  2.4347 

Coefficients:
          Estimate Std. Error t value Pr(>|t|)  
(Intercept)   -0.25829    0.19196  -1.346   0.1816  
 x              0.09983    0.09788   1.020   0.3104  

...

感谢。

2 个答案:

答案 0 :(得分:4)

您可以执行以下任何操作:

mod <- lm(y~x + as.factor(z), data = frame)
coef(mod)[c("(Intercept)", "x")]
# (Intercept)           x 
#  0.12357491 -0.06430765 
coef(mod)[grepl("Intercept|x", names(coef(mod)))]
# (Intercept)           x 
#  0.12357491 -0.06430765 
coef(mod)[1:2]
# (Intercept)           x 
#  0.12357491 -0.06430765 
mod$coefficients[1:2]
# (Intercept)           x 
#  0.12357491 -0.06430765 

答案 1 :(得分:0)

我强烈推荐stargazer软件包:

frame <- data.frame(x = rnorm(100), y = rnorm(100), z = rep(c("A", "B", "C", "D"),25))
summary(lm(y~x + as.factor(z), data = frame)) 
model <- lm(y~x + as.factor(z), data = frame)
library(stargazer)
stargazer(model, type="text")
stargazer(model, omit = "z", type="text")

enter image description here