ycroissant / plm

Panel Data Econometrics with R
GNU General Public License v2.0
50 stars 13 forks source link

IV models with a single regressor don't work with fancy covariance matrices #4

Closed tcovert closed 3 years ago

tcovert commented 3 years ago

Consider a (modified) example from the help:

zz <- plm(log(gsp) ~ log(pcap) | log(pc) + log(emp),
         data = Produc, index = c("state","year"),
         model = "within")

When you type summary(zz) you get:

> summary(zz)
Oneway (individual) effect Within Model
Instrumental variable estimation

Call:
plm(formula = log(gsp) ~ log(pcap) | log(pc) + log(emp), data = Produc, 
    model = "within", index = c("state", "year"))

Balanced Panel: n = 48, T = 17, N = 816

Residuals:
      Min.    1st Qu.     Median    3rd Qu.       Max. 
-0.3779409 -0.0561652 -0.0055046  0.0440775  0.4590412 

Coefficients:
          Estimate Std. Error z-value  Pr(>|z|)    
log(pcap) 1.528846   0.035191  43.444 < 2.2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Total Sum of Squares:    18.941
Residual Sum of Squares: 7.0211
R-Squared:      0.69103
Adj. R-Squared: 0.67169
Chisq: 1887.37 on 1 DF, p-value: < 2.22e-16

Now, what about Driscoll-Kraay standard errors?

> summary(zz, vcov = vcovSCC)
Error in demX[tind[[i]], , drop = FALSE] : incorrect number of dimensions

I believe the problem is in the definition of vcovG, where the variable demX is defined for models with instrumental variables. If the model matrix has just one column (as in the above example), demX is a column vector, not a matrix. I've found that if you wrap that line in an as.matrix everything else works.

tappek commented 3 years ago

Thank you for reporting with a reproducible example and the analysis! I will commit a fix to our development repository later this day.

tappek commented 3 years ago

This is fixed in revision #1125 in our development repository (currently building): https://r-forge.r-project.org/R/?group_id=406

It affected all vcovXX functions making use of vcovG (vcovHC, vcovNW, vcovDC, vcovSCC) as well as the vcovBK which does not make use of vcovG.

chuqingjin commented 3 years ago

I found a similar issue in within_intercept, which also seems to not support models with one covariate.

Here is a reproducible example:

data("Produc", package = "plm")  
zz <- plm(log(gsp) ~ log(pcap), data = Produc, index = c("state","year"), model = "within")  
inter <- within_intercept(zz)

The error message is Error in colMeans(M) : 'x' must be an array of at least two dimensions.

Again, wrapping M with as.matrix seems to fix the issue.

tappek commented 3 years ago

Thank you for reporting! This is similar but code-wise independent.

Fixed by adding one of the famous drop = FALSE addition to matrix subsetting in within_intercept in rev. 1143 on R-Forge.

tappek commented 2 years ago

plm version 2.4-2 now on CRAN (2021-09-21) containing these fixes