2
我的目標是使用lfe::demeanlist()
根據一組因子獲得一個data.frame
降序數據。然後我想表明,在簡單的lm()
中使用這些數據相當於lm()
的因素。這種等效性沒有權重,但是當我使用權重時,點數估計值會被忽略。使用felm()
和lm()
與權重等效。使用demeanlist()來獲得lm()與權重的等價性
樣本數據
library(lfe)
set.seed(12345)
iris <- iris
# Create weights
iris$w <- rnorm(150, 10, 1)
# Quadratic term
iris$Sepal.Width_sq <- iris$Sepal.Width^2
等效lm()
和felm()
之間:
注意點估計Sepal.Width
和Sepal.Width_sq
是相同的**
# Simple lm()
> summary(lm(Sepal.Length ~ Sepal.Width + Sepal.Width_sq +
factor(Species), data = iris, weights = iris$w))
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.01987 1.31625 1.535 0.127
Sepal.Width 0.96610 0.83626 1.155 0.250
Sepal.Width_sq -0.02736 0.13291 -0.206 0.837
factor(Species)versicolor 1.45629 0.11285 12.904 <2e-16 ***
factor(Species)virginica 1.94694 0.10245 19.003 <2e-16 ***
---
# With felm()
summary(felm(Sepal.Length ~ Sepal.Width + Sepal.Width_sq| Species,
data = iris, weights = iris$w))
Coefficients:
Estimate Std. Error t value Pr(>|t|)
Sepal.Width 0.96610 0.83626 1.155 0.250
Sepal.Width_sq -0.02736 0.13291 -0.206 0.837
使用demeanlist()
貶低數據和權運行lm()
:
這給了不同的點估計:
# demean and lm()
> newdat <- demeanlist(iris, list(iris$Species))
> summary(lm(Sepal.Length ~ Sepal.Width + Sepal.Width_sq,
data = newdat, weights = iris$w))
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.003732 0.035799 0.104 0.917
Sepal.Width 0.965895 0.830550 1.163 0.247
Sepal.Width_sq -0.027335 0.132007 -0.207 0.836