Search code examples
rsummarylm

Suppress fixed effects coefficients in R


is there a way to suppress the coefficients for fixed effects in a linear model when using the summary() function (e.g. the equivalent of the absorb() function in stata). For example, I would like to have the summary function output just the intercept and x rather than the coefficients and standard errors for the factors as well:

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  

...

Thanks.


Solution

  • You could do any of these:

    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