[R] Include manually an intercept in lm without breaking it?

Matthieu Stigler matthieu.stigler at gmail.com
Sat Nov 28 16:14:47 CET 2009


Hi

Say I want to add manually an intercept in the function lm. Even if 
almost all results will be identical, few stats are different as DF 
counting will be different as intercept will not be included in 
"automatic" case, while it will be in "manual" case. See:

###usual lm on freeny
fr<-lm(freeny.y~freeny.x)
###manual lm on freeny
man<-cbind(1,freeny.x)
colnames(man)<-c("const",colnames(freeny.x))
fr_man<-lm(freeny.y~man-1)

###coef are the same
cbind(coef(fr), coef(fr_man))

###but summary output is different (but should be the same!).
#Difference comes from fact that in the "automatic case", DF are 4 
(intercept not included)
summary(fr)
summary(fr_man)

###Workaround:
attr(fr_man$terms, "intercept") <- 1

##so now:
summary(fr)
summary(fr_man)


###but have negative effect that intercept is used twice in 
model.matrix, see:
model.matrix(fr_man)

So I could not find a good way to add manually an intercept and 
preserving the right output... any idea?

Thanks a lot!!

Matthieu Stigler




More information about the R-help mailing list