Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite variance code so it is possible to get vcov across at #87

Closed
leeper opened this issue Feb 7, 2018 · 1 comment
Closed

Rewrite variance code so it is possible to get vcov across at #87

leeper opened this issue Feb 7, 2018 · 1 comment
Milestone

Comments

@leeper
Copy link
Owner

leeper commented Feb 7, 2018

This is currently possible in Stata but not R:

sysuse auto
reg price c.mpg##i.foreign
margins, dydx(mpg) at(foreign=(0 1)) post
lincom _b[mpg:1bn._at] - _b[mpg:2._at]

The reason is that Stata produces a single Jacobian across all at values:

. matrix list e(V)

symmetric e(V)[2,2]
                  mpg:        mpg:
                    1.          2.
                  _at         _at
mpg:1._at   5622.8176
mpg:2._at  -1.819e-11   7029.2128

. matrix list e(Jacobian)

e(Jacobian)[2,6]
                                 0b.           1.  0b.foreign#   1.foreign#             
                   mpg      foreign      foreign       co.mpg        c.mpg        _cons
mpg:1._at            1            0            0            0            0            0
mpg:2._at            1            0            0            0            1            0

Whereas the current R code does everything separately across at specifications. We could simply return the Jacobian for each at, rbind() them together, and then sandwich the result to get a full variance-covariance matrix in the style of Stata's return value. This could be returned by vcov() but will require some generalization and possibly rewriting of summary().

@leeper leeper added this to the v0.4.0 milestone Feb 8, 2018
@leeper
Copy link
Owner Author

leeper commented Feb 8, 2018

Solution:

m <- lm(price ~ mpg * factor(foreign), data = auto)
# difference in AMEs
diff(summary(margins(m, at = list(foreign = c("0", "1")), variables = "mpg"))$AME)
##      mpg 
## 78.88826
# standard error of difference
vc <- vcov(margins(m, at = list(foreign = c("0", "1")), variables = "mpg"))
sqrt(sum(diag(vc)) - 2*vc[1,2])
## [1] 112.7029

which matches Stata:

. lincom _b[mpg:1bn._at] - _b[mpg:2._at]

 ( 1)  [mpg]1bn._at - [mpg]2._at = 0

------------------------------------------------------------------------------
             |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         (1) |  -78.88826   112.4812    -0.70   0.485     -303.225    145.4485
------------------------------------------------------------------------------

@leeper leeper closed this as completed in 52c6757 Feb 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant