2017-07-17 56 views
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.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

這裏回答我的問題。點估計的問題是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 

現在,所有模型函數的估計值都是相同的。