使用 demeanlist() 获得 lm() 等价于权重
Using demeanlist() to get lm() equivalence with weights
我的目标是使用 lfe::demeanlist()
获得基于一组因素的 data.frame
贬低数据。然后我想证明在简单的 lm()
中使用这些数据等同于 lm()
和因子。这种等效性在没有权重的情况下有效,但是当我使用权重时,点估计值会稍微偏离。使用 felm()
和 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
在这里回答我自己的问题。点估计的问题是 demeanlist()
函数需要 sqrt()
形式的权重,因为模型正在估计 WLS。
newdat <- demeanlist(iris, list(iris$Species), weights = sqrt(iris$w))
summary(lm(Sepal.Length ~ Sepal.Width + Sepal.Width_sq,
data = newdat, weights = iris$w)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.312e-15 3.580e-02 0.000 1.000
Sepal.Width 9.661e-01 8.306e-01 1.163 0.247
Sepal.Width_sq -2.736e-02 1.320e-01 -0.207 0.836
现在模型函数的估计值相同。
我的目标是使用 lfe::demeanlist()
获得基于一组因素的 data.frame
贬低数据。然后我想证明在简单的 lm()
中使用这些数据等同于 lm()
和因子。这种等效性在没有权重的情况下有效,但是当我使用权重时,点估计值会稍微偏离。使用 felm()
和 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
在这里回答我自己的问题。点估计的问题是 demeanlist()
函数需要 sqrt()
形式的权重,因为模型正在估计 WLS。
newdat <- demeanlist(iris, list(iris$Species), weights = sqrt(iris$w))
summary(lm(Sepal.Length ~ Sepal.Width + Sepal.Width_sq,
data = newdat, weights = iris$w)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.312e-15 3.580e-02 0.000 1.000
Sepal.Width 9.661e-01 8.306e-01 1.163 0.247
Sepal.Width_sq -2.736e-02 1.320e-01 -0.207 0.836
现在模型函数的估计值相同。