2012-06-15 39 views
2

我想創建一個使用aprstable包強大的標準錯誤的估計結果表。但是,因爲我的模型具有不易避免的奇點(它們來自數百個關卡的重疊因素),因此aprstable拒絕提供我正在嘗試生成的表格。這裏的一個工作實施例中(改編自related thread):aprstable與奇點模型的健壯標準錯誤

library(sandwich) 
set.seed(101) 
dat<-data.frame(one=c(sample(1000:1239)), 
       two=c(sample(200:439)), 
       three=c(sample(600:839)), 
       Jan=c(rep(1,20),rep(0,220)), 
       Feb=c(rep(0,20),rep(1,20),rep(0,200)), 
       Mar=c(rep(0,40),rep(1,20),rep(0,180)), 
       Apr=c(rep(0,60),rep(1,20),rep(0,160)), 
       May=c(rep(0,80),rep(1,20),rep(0,140)), 
       Jun=c(rep(0,100),rep(1,20),rep(0,120)), 
       Jul=c(rep(0,120),rep(1,20),rep(0,100)), 
       Aug=c(rep(0,140),rep(1,20),rep(0,80)), 
       Sep=c(rep(0,160),rep(1,20),rep(0,60)), 
       Oct=c(rep(0,180),rep(1,20),rep(0,40)), 
       Nov=c(rep(0,200),rep(1,20),rep(0,20)), 
       Dec=c(rep(0,220),rep(1,20))) 
model <- lm(one ~ two + three + Jan + Feb + Mar + Apr + May + Jun + Jul + Aug + Sep + Oct + Nov + Dec, data=dat) 
summary(model) 
model$se <- vcovHC(model) 

library('apsrtable') 
apsrtable(model, se='robust') 

執行此代碼產生

> apsrtable(model, se='robust') 
Error in s$coefficients[, 3] <- tval <- est/x$se : 
    number of items to replace is not a multiple of replacement length 
In addition: Warning message: 
In est/x$se : 
    longer object length is not a multiple of shorter object length 

據我可以告訴此錯誤發生,因爲length(sqrt(diag(model$se)))是14,同時length(coefficients(model))是15,即有比他們的方差 - 協方差矩陣的對角線上的標準誤差更多的係數(也參見relevant bit of source code in the aprstable package)。

有沒有簡單的方法可以解決這個問題?在將NA行和列插入到適當位置的方差 - 協方差矩陣中之後,可能會將其傳遞給apsrtable()?更優雅的東西?

+0

RoyalTS的優秀問題。我認爲你對錯誤的分析是現貨。我確認我有同樣的錯誤。如果可以在apsrtable的新版本中解決這個問題,那將會很棒。 – Chris

+0

FWIW,後來我轉向了'stargazer'軟件包,它在許多方面效果更好,並且維護得更好。我還沒有嘗試,如果它遭受這個問題,但如果你卡住了,也許這是值得一試。 – RoyalTS

回答

3

這是一個簡單的修復 - coef(模型)返回NA,但coef(summary(模型))返回摘要的係數。我會盡快提交更新,但https://github.com/malecki/apsrtable的軟件包已更新。

+0

謝謝邁克爾! – Chris