使用demeanlist()来获得与权重的lm()等价

时间:2017-07-17 04:05:58

标签: r lm

我的目标是使用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.WidthSepal.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

1 个答案:

答案 0 :(得分:1)

在这里回答我自己的问题。点估计的问题是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

现在估算模型函数的估计值是相同的。