问题
I want to use lm.fit for speed, but second version gives NAs
sum <- summary(lm(y~x))
slope <- sum$coefficients[2]
or
sum <- lm.fit(as.matrix(x,ncol=1),y)
slope <- sum$coefficients[2]
EDIT 1
I now see that sum$coefficients only has 1 value. Why is that and what is it? The help doesn't explain this
回答1:
It would be easier to help with a reproducible example.
However, my guess is that you are missing an intercept in the second case. Try lm.fit(cbind(1,x),y)
and see if that gives you the comparison that you are looking for.
Other differences could be due to the other preprocessing that lm
does before calling lm.fit
, but we don't know what that is without seeing x
. Things like removing missing values, expanding a factor into dummy variables could also result in differences.
来源:https://stackoverflow.com/questions/16675614/how-to-use-lm-fit-instead-on-lm