Search code examples
rregressionlinear-regression

How to specify instruments in AER::ivreg?


In my regression, I want to instrument a variable pastS with lgrade and run the following code in R:

modiv <- ivreg(servS ~ horS + size + henley + lpd + tri + turn + rail + 
                 enc20 + wheat | pastS | lgrade, data = regdata)

I get an error: length(formula)[2] %in% 1:2 is not TRUE. Apparently, something is not right with my syntax, but I cannot find an error. Could you help?


Solution

  • AER::ivreg wants you to provide an instrument for each regressor after one separator |, so instead of

    > library(AER)
    > ivreg(log(packs) ~ population + tax | log(rprice) | rtdiff, data=c1995)
    Error in ivreg(log(packs) ~ population + tax | log(rprice) | rtdiff, data = c1995) : 
      length(formula)[2] %in% 1:2 is not TRUE
    

    it's

    > ivreg(log(packs) ~ log(rprice) + population + tax | rtdiff + population + tax, data=c1995)
    
    Call:
    ivreg(formula = log(packs) ~ log(rprice) + population + tax |     rtdiff + population + tax, data = c1995)
    
    Coefficients:
    (Intercept)  log(rprice)   population          tax  
      7.241e+00   -5.089e-01   -7.907e-09   -4.202e-03  
    

    You could consider lfe::felm instead which is more concise and might fit more to your intuition.

    > library(lfe)
    > felm(log(packs) ~ population + tax | 0 | (log(rprice) ~ rtdiff), data=c1995)
           (Intercept)         population                tax `log(rprice)(fit)` 
             7.241e+00         -7.907e-09         -4.202e-03         -5.089e-01 
    

    Data:

    > data("CigarettesSW", package="AER")
    > CigarettesSW <- transform(CigarettesSW,
    +                           rprice=price/cpi,
    +                           rtdiff=(taxs - tax)/cpi
    + )
    > c1995 <- subset(CigarettesSW, year == "1995")