2017-07-22 6 views
2

J'effectue un test de Newey-West pour évaluer un estimateur d'une régression avec des résidus hétéroscédastiques et autocorrélés. J'utilise les paquets "sandwich" et "lmtest".R: Coeftest provoque une erreur

Alors que je peux facilement reproduire des exemples trouvés sur d'autres sites, mon propre script provoque l'erreur:

Erreur dans dimnames (cd) < - Liste (as.character (index (x)), colnames (x)): '' dimnames appliqués aux non-réseau

mon code:

ffregression <- lm(ex.return ~ fff$Mkt.RF + fff$SMB + fff$HML) 
coeftest(ffregression,vcov=NeweyWest) 

str (ffregression):

List of 12 
$ coefficients : Named num [1:4] 0.00604 0.72976 0.90351 0.13548 
    ..- attr(*, "names")= chr [1:4] "(Intercept)" "fff$Mkt.RF" "fff$SMB" "fff$HML" 
$ residuals :An ‘xts’ object on Mar 2014/Dec 2016 containing: 
    Data: num [1:34] -0.03637 0.0408 -0.00672 0.04648 -0.02275 ... 
    Indexed by objects of class: [yearmon] TZ: 
    Original class: 'double' 
    xts Attributes: 
NULL 
$ effects  :An ‘xts’ object on Mar 2014/Dec 2016 containing: 
    Data: num [1:34] -0.0606 0.1785 0.1379 0.0204 -0.0262 ... 
    Indexed by objects of class: [yearmon] TZ: 
    Original class: 'double' 
    xts Attributes: 
NULL 
$ rank   : int 4 
$ fitted.values:An ‘xts’ object on Mar 2014/Dec 2016 containing: 
    Data: num [1:34] -0.000725 -0.031716 0.003868 0.051386 -0.047005 ... 
    Indexed by objects of class: [yearmon] TZ: 
    Original class: 'double' 
    xts Attributes: 
NULL 
$ assign  : int [1:4] 0 1 2 3 
$ qr   :List of 5 
    ..$ qr : num [1:34, 1:4] -5.831 0.171 0.171 0.171 0.171 ... 
    .. ..- attr(*, "dimnames")=List of 2 
    .. .. ..$ : chr [1:34] "1" "2" "3" "4" ... 
    .. .. ..$ : chr [1:4] "(Intercept)" "fff$Mkt.RF" "fff$SMB" "fff$HML" 
    .. ..- attr(*, "assign")= int [1:4] 0 1 2 3 
    ..$ qraux: num [1:4] 1.17 1.05 1.14 1.14 
    ..$ pivot: int [1:4] 1 2 3 4 
    ..$ tol : num 1e-07 
    ..$ rank : int 4 
    ..- attr(*, "class")= chr "qr" 
$ df.residual : int 30 
$ xlevels  : Named list() 
$ call   : language lm(formula = ex.return ~ fff$Mkt.RF + fff$SMB + fff$HML) 
$ terms  :Classes 'terms', 'formula' language ex.return ~ fff$Mkt.RF + fff$SMB + fff$HML 
    .. ..- attr(*, "variables")= language list(ex.return, fff$Mkt.RF, fff$SMB, fff$HML) 
    .. ..- attr(*, "factors")= int [1:4, 1:3] 0 1 0 0 0 0 1 0 0 0 ... 
    .. .. ..- attr(*, "dimnames")=List of 2 
    .. .. .. ..$ : chr [1:4] "ex.return" "fff$Mkt.RF" "fff$SMB" "fff$HML" 
    .. .. .. ..$ : chr [1:3] "fff$Mkt.RF" "fff$SMB" "fff$HML" 
    .. ..- attr(*, "term.labels")= chr [1:3] "fff$Mkt.RF" "fff$SMB" "fff$HML" 
    .. ..- attr(*, "order")= int [1:3] 1 1 1 
    .. ..- attr(*, "intercept")= int 1 
    .. ..- attr(*, "response")= int 1 
    .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
    .. ..- attr(*, "predvars")= language list(ex.return, fff$Mkt.RF, fff$SMB, fff$HML) 
    .. ..- attr(*, "dataClasses")= Named chr [1:4] "nmatrix.1" "nmatrix.1" "nmatrix.1" "nmatrix.1" 
    .. .. ..- attr(*, "names")= chr [1:4] "ex.return" "fff$Mkt.RF" "fff$SMB" "fff$HML" 
$ model  :'data.frame': 34 obs. of 4 variables: 
    ..$ ex.return :An ‘xts’ object on Mar 2014/Dec 2016 containing: 
    Data: num [1:34, 1] -0.03709 0.00909 -0.00285 0.09786 -0.06975 ... 
- attr(*, "dimnames")=List of 2 
    ..$ : NULL 
    ..$ : chr "RF" 
    Indexed by objects of class: [yearmon] TZ: 
    Original class: 'double' 
    xts Attributes: 
NULL 
    ..$ fff$Mkt.RF:An ‘xts’ object on Mar 2014/Dec 2016 containing: 
    Data: num [1:34, 1] 0.0043 -0.0019 0.0206 0.0261 -0.0204 0.0424 -0.0197 0.0252 0.0255 -0.0006 ... 
- attr(*, "dimnames")=List of 2 
    ..$ : NULL 
    ..$ : chr "Mkt.RF" 
    Indexed by objects of class: [yearmon] TZ: 
    xts Attributes: 
NULL 
    ..$ fff$SMB :An ‘xts’ object on Mar 2014/Dec 2016 containing: 
    Data: num [1:34, 1] -0.0185 -0.0419 -0.0185 0.0301 -0.0422 0.004 -0.038 0.0428 -0.0205 0.0259 ... 
- attr(*, "dimnames")=List of 2 
    ..$ : NULL 
    ..$ : chr "SMB" 
    Indexed by objects of class: [yearmon] TZ: 
    xts Attributes: 
NULL 
    ..$ fff$HML :An ‘xts’ object on Mar 2014/Dec 2016 containing: 
    Data: num [1:34, 1] 0.0503 0.011 -0.0036 -0.0066 -0.0002 -0.0055 -0.0119 -0.0168 -0.0298 0.0212 ... 
- attr(*, "dimnames")=List of 2 
    ..$ : NULL 
    ..$ : chr "HML" 
    Indexed by objects of class: [yearmon] TZ: 
    xts Attributes: 
NULL 
    ..- attr(*, "terms")=Classes 'terms', 'formula' language ex.return ~ fff$Mkt.RF + fff$SMB + fff$HML 
    .. .. ..- attr(*, "variables")= language list(ex.return, fff$Mkt.RF, fff$SMB, fff$HML) 
    .. .. ..- attr(*, "factors")= int [1:4, 1:3] 0 1 0 0 0 0 1 0 0 0 ... 
    .. .. .. ..- attr(*, "dimnames")=List of 2 
    .. .. .. .. ..$ : chr [1:4] "ex.return" "fff$Mkt.RF" "fff$SMB" "fff$HML" 
    .. .. .. .. ..$ : chr [1:3] "fff$Mkt.RF" "fff$SMB" "fff$HML" 
    .. .. ..- attr(*, "term.labels")= chr [1:3] "fff$Mkt.RF" "fff$SMB" "fff$HML" 
    .. .. ..- attr(*, "order")= int [1:3] 1 1 1 
    .. .. ..- attr(*, "intercept")= int 1 
    .. .. ..- attr(*, "response")= int 1 
    .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
    .. .. ..- attr(*, "predvars")= language list(ex.return, fff$Mkt.RF, fff$SMB, fff$HML) 
    .. .. ..- attr(*, "dataClasses")= Named chr [1:4] "nmatrix.1" "nmatrix.1" "nmatrix.1" "nmatrix.1" 
    .. .. .. ..- attr(*, "names")= chr [1:4] "ex.return" "fff$Mkt.RF" "fff$SMB" "fff$HML" 
- attr(*, "class")= chr "lm" 

Exemple:

set.seed(04012017) 
n<-34 
correlated_residuals<-arima.sim(list(ar = .9), n) 
y<-correlated_residuals 
x<-1:n 
plot(x,correlated_residuals) 
fit<-lm(y~x) 
abline(fit) 
summary(fit) # standard estimates 
coeftest(fit,vcov=NeweyWest(fit,verbose=T)) 

str (TRG):

List of 12 
$ coefficients : Named num [1:2] -0.179 0.148 
    ..- attr(*, "names")= chr [1:2] "(Intercept)" "x" 
$ residuals : Named num [1:34] -0.9529 0.976 0.3025 -0.0486 -1.1214 ... 
    ..- attr(*, "names")= chr [1:34] "1" "2" "3" "4" ... 
$ effects  : Named num [1:34] -14.026 8.449 0.25 -0.085 -1.142 ... 
    ..- attr(*, "names")= chr [1:34] "(Intercept)" "x" "" "" ... 
$ rank   : int 2 
$ fitted.values: Named num [1:34] -0.0314 0.1163 0.264 0.4116 0.5593 ... 
    ..- attr(*, "names")= chr [1:34] "1" "2" "3" "4" ... 
$ assign  : int [1:2] 0 1 
$ qr   :List of 5 
    ..$ qr : num [1:34, 1:2] -5.831 0.171 0.171 0.171 0.171 ... 
    .. ..- attr(*, "dimnames")=List of 2 
    .. .. ..$ : chr [1:34] "1" "2" "3" "4" ... 
    .. .. ..$ : chr [1:2] "(Intercept)" "x" 
    .. ..- attr(*, "assign")= int [1:2] 0 1 
    ..$ qraux: num [1:2] 1.17 1.23 
    ..$ pivot: int [1:2] 1 2 
    ..$ tol : num 1e-07 
    ..$ rank : int 2 
    ..- attr(*, "class")= chr "qr" 
$ df.residual : int 32 
$ xlevels  : Named list() 
$ call   : language lm(formula = y ~ x) 
$ terms  :Classes 'terms', 'formula' language y ~ x 
    .. ..- attr(*, "variables")= language list(y, x) 
    .. ..- attr(*, "factors")= int [1:2, 1] 0 1 
    .. .. ..- attr(*, "dimnames")=List of 2 
    .. .. .. ..$ : chr [1:2] "y" "x" 
    .. .. .. ..$ : chr "x" 
    .. ..- attr(*, "term.labels")= chr "x" 
    .. ..- attr(*, "order")= int 1 
    .. ..- attr(*, "intercept")= int 1 
    .. ..- attr(*, "response")= int 1 
    .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
    .. ..- attr(*, "predvars")= language list(y, x) 
    .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric" 
    .. .. ..- attr(*, "names")= chr [1:2] "y" "x" 
$ model  :'data.frame': 34 obs. of 2 variables: 
    ..$ y: num [1:34] -0.984 1.092 0.566 0.363 -0.562 ... 
    ..$ x: int [1:34] 1 2 3 4 5 6 7 8 9 10 ... 
    ..- attr(*, "terms")=Classes 'terms', 'formula' language y ~ x 
    .. .. ..- attr(*, "variables")= language list(y, x) 
    .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1 
    .. .. .. ..- attr(*, "dimnames")=List of 2 
    .. .. .. .. ..$ : chr [1:2] "y" "x" 
    .. .. .. .. ..$ : chr "x" 
    .. .. ..- attr(*, "term.labels")= chr "x" 
    .. .. ..- attr(*, "order")= int 1 
    .. .. ..- attr(*, "intercept")= int 1 
    .. .. ..- attr(*, "response")= int 1 
    .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
    .. .. ..- attr(*, "predvars")= language list(y, x) 
    .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric" 
    .. .. .. ..- attr(*, "names")= chr [1:2] "y" "x" 
- attr(*, "class")= chr "lm" 

Répondre

1

Il semble que R se plaint qu'il ne peut pas attribuer dimnames parce que vos données provient d'un non-tableau. Essayez d'utiliser un seul objet xts pour vos données à la place.

fff$ex.return <- ex.return 
ffregression <- lm(ex.return ~ Mkt.RF + SMB + HML, data = fff) 
coeftest(ffregression,vcov=NeweyWest) 
+0

C'était le problème, merci Paul! – user169340