TFT
TFT

Reputation: 231

2SLS standard errors with ivreg and plm in R

Could someone please confirm whether ivreg is calculating correct 2SLS standard errors by default or do I still have to manually correct them? I have an instrumental variable Z1 in my regression.

library(ivreg)
ivreg1 <- ivreg(log(Y) ~ log(X) + W1 + W2 + W3 | W1 + W2 + W3 + Z1, data = lending)

I then continue to combine IV with fixed effects. Do I have to manually correct the standard errors here? I need robust standard errors for sure.

library(plm)
IVpan <- plm(log(Y) ~ log(X) + W1 + W2 + W3 | W1 + W2 + W3 + Z1, model = "within", data = lending, index = c("countryIdentifier","year"))

In case a correction is needed, I guess I would have calculated the robust standard errors just like this:

covIVpan1         <- vcovHC(IVpan, type = "HC1")
robust_seIVpan1    <- sqrt(diag(covIVpan1))

Upvotes: 1

Views: 3490

Answers (1)

Helix123
Helix123

Reputation: 3687

Your assumption about plm and IV models estimated by it are correct. The conventional approach in R is to have the model estimation and the robust standard error estimation split in two different steps. Typically, summary methods for model objects will accept a vcov argument with other than the usual standard errors or a function to estimate them (typically also called vcov with varying arguments).

See below for a panel IV example with plm.

For ivreg, the typicall approach applies as well. See below (for a slightly extended) example from ?ivreg::ivreg making also use of pkg sandwich.

Example for plm::plm with panel IV model:

library(plm)
data("Crime", package = "plm")
FE2SLS <- plm(lcrmrte ~ lprbarr + lpolpc + lprbconv + lprbpris + lavgsen +
                ldensity + lwcon + lwtuc + lwtrd + lwfir + lwser + lwmfg + lwfed +
                lwsta + lwloc + lpctymle + lpctmin + region + smsa + factor(year)
              | . - lprbarr - lpolpc + ltaxpc + lmix,
              data = Crime, model = "within")

summary(FE2SLS, vcov = vcovHC)
#> Oneway (individual) effect Within Model
#> Instrumental variable estimation
#> 
#> Note: Coefficient variance-covariance matrix supplied: vcovHC
#> 
#> Call:
#> plm(formula = lcrmrte ~ lprbarr + lpolpc + lprbconv + lprbpris + 
#>     lavgsen + ldensity + lwcon + lwtuc + lwtrd + lwfir + lwser + 
#>     lwmfg + lwfed + lwsta + lwloc + lpctymle + lpctmin + region + 
#>     smsa + factor(year) | . - lprbarr - lpolpc + ltaxpc + lmix, 
#>     data = Crime, model = "within")
#> 
#> Balanced Panel: n = 90, T = 7, N = 630
#> 
#> Residuals:
#>       Min.    1st Qu.     Median    3rd Qu.       Max. 
#> -0.7209110 -0.0682207 -0.0041115  0.0759381  0.5661659 
#> 
#> Coefficients:
#>                  Estimate Std. Error z-value Pr(>|z|)
#> lprbarr        -0.5755058  0.7884162 -0.7300   0.4654
#> lpolpc          0.6575270  0.8624838  0.7624   0.4458
#> lprbconv       -0.4231446  0.4998464 -0.8465   0.3972
#> lprbpris       -0.2502550  0.2755415 -0.9082   0.3638
#> lavgsen         0.0090987  0.0550200  0.1654   0.8687
#> ldensity        0.1394120  0.9959261  0.1400   0.8887
#> lwcon          -0.0287308  0.0381071 -0.7539   0.4509
#> lwtuc           0.0391292  0.0281711  1.3890   0.1648
#> lwtrd          -0.0177536  0.0337546 -0.5260   0.5989
#> lwfir          -0.0093443  0.0240168 -0.3891   0.6972
#> lwser           0.0185854  0.0368987  0.5037   0.6145
#> lwmfg          -0.2431684  0.4342518 -0.5600   0.5755
#> lwfed          -0.4513372  0.5134606 -0.8790   0.3794
#> lwsta          -0.0187458  0.3083631 -0.0608   0.9515
#> lwloc           0.2632585  0.2924827  0.9001   0.3681
#> lpctymle        0.3511166  1.0707877  0.3279   0.7430
#> factor(year)82  0.0378562  0.0641333  0.5903   0.5550
#> factor(year)83 -0.0443801  0.0425536 -1.0429   0.2970
#> factor(year)84 -0.0451868  0.0610387 -0.7403   0.4591
#> factor(year)85 -0.0209411  0.0789048 -0.2654   0.7907
#> factor(year)86  0.0063234  0.1178049  0.0537   0.9572
#> factor(year)87  0.0435055  0.2002534  0.2173   0.8280
#> 
#> Total Sum of Squares:    17.991
#> Residual Sum of Squares: 11.537
#> R-Squared:      0.44364
#> Adj. R-Squared: 0.32442
#> Chisq: 228.359 on 22 DF, p-value: < 2.22e-16
sqrt(diag(vcovHC(FE2SLS)))
#>        lprbarr         lpolpc       lprbconv       lprbpris        lavgsen 
#>     0.78841618     0.86248381     0.49984636     0.27554147     0.05501995 
#>       ldensity          lwcon          lwtuc          lwtrd          lwfir 
#>     0.99592612     0.03810713     0.02817113     0.03375456     0.02401682 
#>          lwser          lwmfg          lwfed          lwsta          lwloc 
#>     0.03689867     0.43425180     0.51346057     0.30836311     0.29248269 
#>       lpctymle factor(year)82 factor(year)83 factor(year)84 factor(year)85 
#>     1.07078768     0.06413330     0.04255357     0.06103873     0.07890485 
#> factor(year)86 factor(year)87 
#>     0.11780486     0.20025343

summary(FE2SLS, vcov = vcovHC)
#> Oneway (individual) effect Within Model
#> Instrumental variable estimation
#> 
#> Note: Coefficient variance-covariance matrix supplied: vcovHC
#> 
#> Call:
#> plm(formula = lcrmrte ~ lprbarr + lpolpc + lprbconv + lprbpris + 
#>     lavgsen + ldensity + lwcon + lwtuc + lwtrd + lwfir + lwser + 
#>     lwmfg + lwfed + lwsta + lwloc + lpctymle + lpctmin + region + 
#>     smsa + factor(year) | . - lprbarr - lpolpc + ltaxpc + lmix, 
#>     data = Crime, model = "within")
#> 
#> Balanced Panel: n = 90, T = 7, N = 630
#> 
#> Residuals:
#>       Min.    1st Qu.     Median    3rd Qu.       Max. 
#> -0.7209110 -0.0682207 -0.0041115  0.0759381  0.5661659 
#> 
#> Coefficients:
#>                  Estimate Std. Error z-value Pr(>|z|)
#> lprbarr        -0.5755058  0.7884162 -0.7300   0.4654
#> lpolpc          0.6575270  0.8624838  0.7624   0.4458
#> lprbconv       -0.4231446  0.4998464 -0.8465   0.3972
#> lprbpris       -0.2502550  0.2755415 -0.9082   0.3638
#> lavgsen         0.0090987  0.0550200  0.1654   0.8687
#> ldensity        0.1394120  0.9959261  0.1400   0.8887
#> lwcon          -0.0287308  0.0381071 -0.7539   0.4509
#> lwtuc           0.0391292  0.0281711  1.3890   0.1648
#> lwtrd          -0.0177536  0.0337546 -0.5260   0.5989
#> lwfir          -0.0093443  0.0240168 -0.3891   0.6972
#> lwser           0.0185854  0.0368987  0.5037   0.6145
#> lwmfg          -0.2431684  0.4342518 -0.5600   0.5755
#> lwfed          -0.4513372  0.5134606 -0.8790   0.3794
#> lwsta          -0.0187458  0.3083631 -0.0608   0.9515
#> lwloc           0.2632585  0.2924827  0.9001   0.3681
#> lpctymle        0.3511166  1.0707877  0.3279   0.7430
#> factor(year)82  0.0378562  0.0641333  0.5903   0.5550
#> factor(year)83 -0.0443801  0.0425536 -1.0429   0.2970
#> factor(year)84 -0.0451868  0.0610387 -0.7403   0.4591
#> factor(year)85 -0.0209411  0.0789048 -0.2654   0.7907
#> factor(year)86  0.0063234  0.1178049  0.0537   0.9572
#> factor(year)87  0.0435055  0.2002534  0.2173   0.8280
#> 
#> Total Sum of Squares:    17.991
#> Residual Sum of Squares: 11.537
#> R-Squared:      0.44364
#> Adj. R-Squared: 0.32442
#> Chisq: 228.359 on 22 DF, p-value: < 2.22e-16

## same:
sqrt(diag(vcovHC(FE2SLS)))
#>        lprbarr         lpolpc       lprbconv       lprbpris        lavgsen 
#>     0.78841618     0.86248381     0.49984636     0.27554147     0.05501995 
#>       ldensity          lwcon          lwtuc          lwtrd          lwfir 
#>     0.99592612     0.03810713     0.02817113     0.03375456     0.02401682 
#>          lwser          lwmfg          lwfed          lwsta          lwloc 
#>     0.03689867     0.43425180     0.51346057     0.30836311     0.29248269 
#>       lpctymle factor(year)82 factor(year)83 factor(year)84 factor(year)85 
#>     1.07078768     0.06413330     0.04255357     0.06103873     0.07890485 
#> factor(year)86 factor(year)87 
#>     0.11780486     0.20025343

# with HC1 small sample correction
summary(FE2SLS, vcov = function(x) vcovHC(x, type="HC1"))
#> Oneway (individual) effect Within Model
#> Instrumental variable estimation
#> 
#> Note: Coefficient variance-covariance matrix supplied: function(x) vcovHC(x, type = "HC1")
#> 
#> Call:
#> plm(formula = lcrmrte ~ lprbarr + lpolpc + lprbconv + lprbpris + 
#>     lavgsen + ldensity + lwcon + lwtuc + lwtrd + lwfir + lwser + 
#>     lwmfg + lwfed + lwsta + lwloc + lpctymle + lpctmin + region + 
#>     smsa + factor(year) | . - lprbarr - lpolpc + ltaxpc + lmix, 
#>     data = Crime, model = "within")
#> 
#> Balanced Panel: n = 90, T = 7, N = 630
#> 
#> Residuals:
#>       Min.    1st Qu.     Median    3rd Qu.       Max. 
#> -0.7209110 -0.0682207 -0.0041115  0.0759381  0.5661659 
#> 
#> Coefficients:
#>                  Estimate Std. Error z-value Pr(>|z|)
#> lprbarr        -0.5755058  0.8025535 -0.7171   0.4733
#> lpolpc          0.6575270  0.8779493  0.7489   0.4539
#> lprbconv       -0.4231446  0.5088093 -0.8316   0.4056
#> lprbpris       -0.2502550  0.2804823 -0.8922   0.3723
#> lavgsen         0.0090987  0.0560065  0.1625   0.8709
#> ldensity        0.1394120  1.0137844  0.1375   0.8906
#> lwcon          -0.0287308  0.0387904 -0.7407   0.4589
#> lwtuc           0.0391292  0.0286763  1.3645   0.1724
#> lwtrd          -0.0177536  0.0343598 -0.5167   0.6054
#> lwfir          -0.0093443  0.0244475 -0.3822   0.7023
#> lwser           0.0185854  0.0375603  0.4948   0.6207
#> lwmfg          -0.2431684  0.4420385 -0.5501   0.5822
#> lwfed          -0.4513372  0.5226676 -0.8635   0.3878
#> lwsta          -0.0187458  0.3138925 -0.0597   0.9524
#> lwloc           0.2632585  0.2977273  0.8842   0.3766
#> lpctymle        0.3511166  1.0899883  0.3221   0.7474
#> factor(year)82  0.0378562  0.0652833  0.5799   0.5620
#> factor(year)83 -0.0443801  0.0433166 -1.0246   0.3056
#> factor(year)84 -0.0451868  0.0621332 -0.7273   0.4671
#> factor(year)85 -0.0209411  0.0803197 -0.2607   0.7943
#> factor(year)86  0.0063234  0.1199173  0.0527   0.9579
#> factor(year)87  0.0435055  0.2038442  0.2134   0.8310
#> 
#> Total Sum of Squares:    17.991
#> Residual Sum of Squares: 11.537
#> R-Squared:      0.44364
#> Adj. R-Squared: 0.32442
#> Chisq: 220.384 on 22 DF, p-value: < 2.22e-16

## same:
sqrt(diag(vcovHC(FE2SLS, type="HC1")))
#>        lprbarr         lpolpc       lprbconv       lprbpris        lavgsen 
#>     0.80255354     0.87794930     0.50880927     0.28048230     0.05600654 
#>       ldensity          lwcon          lwtuc          lwtrd          lwfir 
#>     1.01378441     0.03879045     0.02867628     0.03435982     0.02444747 
#>          lwser          lwmfg          lwfed          lwsta          lwloc 
#>     0.03756031     0.44203851     0.52266760     0.31389248     0.29772729 
#>       lpctymle factor(year)82 factor(year)83 factor(year)84 factor(year)85 
#>     1.08998834     0.06528329     0.04331662     0.06213323     0.08031972 
#> factor(year)86 factor(year)87 
#>     0.11991726     0.20384425

Example for ivreg::ivreg:

library(ivreg)
data("CigaretteDemand", package = "ivreg")

## model 
m <- ivreg(log(packs) ~ log(rprice) + log(rincome) | salestax + log(rincome),
           data = CigaretteDemand)
summary(m)
#> 
#> Call:
#> ivreg(formula = log(packs) ~ log(rprice) + log(rincome) | salestax + 
#>     log(rincome), data = CigaretteDemand)
#> 
#> Residuals:
#>       Min        1Q    Median        3Q       Max 
#> -0.611000 -0.086072  0.009423  0.106912  0.393159 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)    9.4307     1.3584   6.943 1.24e-08 ***
#> log(rprice)   -1.1434     0.3595  -3.181  0.00266 ** 
#> log(rincome)   0.2145     0.2686   0.799  0.42867    
#> 
#> Diagnostic tests:
#>                  df1 df2 statistic  p-value    
#> Weak instruments   1  45    45.158 2.65e-08 ***
#> Wu-Hausman         1  44     1.102      0.3    
#> Sargan             0  NA        NA       NA    
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 0.1896 on 45 degrees of freedom
#> Multiple R-Squared: 0.4189,  Adjusted R-squared: 0.3931 
#> Wald test: 6.534 on 2 and 45 DF,  p-value: 0.003227
summary(m, vcov = sandwich::sandwich, df = Inf)
#> 
#> Call:
#> ivreg(formula = log(packs) ~ log(rprice) + log(rincome) | salestax + 
#>     log(rincome), data = CigaretteDemand)
#> 
#> Residuals:
#>       Min        1Q    Median        3Q       Max 
#> -0.611000 -0.086072  0.009423  0.106912  0.393159 
#> 
#> Coefficients:
#>              Estimate Std. Error z value Pr(>|z|)    
#> (Intercept)    9.4307     1.2194   7.734 1.04e-14 ***
#> log(rprice)   -1.1434     0.3605  -3.172  0.00151 ** 
#> log(rincome)   0.2145     0.3018   0.711  0.47729    
#> 
#> Diagnostic tests:
#>                  df1 df2 statistic p-value    
#> Weak instruments   1  45    47.713 1.4e-08 ***
#> Wu-Hausman         1  44     1.287   0.263    
#> Sargan             0  NA        NA      NA    
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 0.1896 on Inf degrees of freedom
#> Multiple R-Squared: 0.4189,  Adjusted R-squared: 0.3931 
#> Wald test:     2 on NA DF,  p-value: NA

## same:
sqrt(diag(sandwich::sandwich(m)))
#>  (Intercept)  log(rprice) log(rincome) 
#>    1.2194016    0.3604805    0.3018477

Upvotes: 2

Related Questions