wolfsatthedoor
wolfsatthedoor

Reputation: 7303

Is it possible to hide the coefficients that are from factors in R lm()?

I have a model with two types of many different fixed effects, and I am only interested in a few regressors and not the fixed effects themselves. I find it easier to include the as.factor variables to have fixed effects (rather than use a within estimator). Is it possible, however, to suppress the output of these factor coefficients?

Upvotes: 3

Views: 1466

Answers (1)

CephBirk
CephBirk

Reputation: 6710

I believe this should work... If your model is named m then try this:

coef(m)[!names(coef(m)) %in% paste0(rep(names(m$xlevels), times=sapply(m$xlevels, length)), unlist(sapply(names(m$xlevels), function(x) m$xlevels[[x]])))]

It's ugly but the concept is to use the xlevels attribute of the lm object to identify coefficients that are factors and remove them from the coefficients list. The rest is just the best way I could find to get the formatting right.

Upvotes: 1

Related Questions