Remove coefficients from lm output in R -
i remove of as.factor
elements output of ordinary least squares lm()
model in r. last line doesn't work, example:
frame <- data.frame(y = rnorm(100), x= rnorm(100), block = sample(c("a", "b", "c", "d"), 100, replace = true)) mod <- lm(y ~ x + as.factor(block), data = frame) summary(mod) summary(mod)$coefficients[3:5,] <- null
is there way remove of these elements saved `lm' object no longer has them? thanks.
one option use felm function in lfe package.
as stated in package:
the package intended linear models multiple group fixed effects, i.e. 2 or more factors large number of levels. performs similar functions lm
, uses special method projecting out multiple group fixed effects normal equations, hence faster.
set.seed(123) frame <- data.frame(y = rnorm(100), x= rnorm(100), block = sample(c("a", "b", "c", "d"), 100, replace = true)) id<-as.factor(frame$block) mod <- lm(y ~ x + id, data = frame) #lm summary(mod) call: lm(formula = y ~ x + id, data = frame) residuals: min 1q median 3q max -2.53394 -0.68372 0.04072 0.67805 2.00777 coefficients: estimate std. error t value pr(>|t|) (intercept) 0.18115 0.17201 1.053 0.2950 x -0.08310 0.09604 -0.865 0.3891 idb 0.04834 0.24645 0.196 0.8449 idc -0.51265 0.25052 -2.046 0.0435 * idd 0.04905 0.26073 0.188 0.8512 --- signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 residual standard error: 0.9002 on 95 degrees of freedom multiple r-squared: 0.06677, adjusted r-squared: 0.02747 f-statistic: 1.699 on 4 , 95 df, p-value: 0.1566 library(lfe) est <- felm(y ~ x| id) summary(est) call: felm(formula = y ~ x | id, data = frame) residuals: min 1q median 3q max -2.53394 -0.68372 0.04072 0.67805 2.00777 coefficients: estimate std. error t value pr(>|t|) x -0.08310 0.09604 -0.865 0.389 residual standard error: 0.9002 on 95 degrees of freedom multiple r-squared(full model): 0.06677 adjusted r-squared: 0.02747 multiple r-squared(proj model): 0.00782 adjusted r-squared: -0.03396 f-statistic(full model):1.699 on 4 , 95 df, p-value: 0.1566 f-statistic(proj model): 0.7487 on 1 , 95 df, p-value: 0.3891
p.s. similar program stata reghdfe.
Comments
Post a Comment