2
$\begingroup$

Context: I am using Stata to recreate someone else's analysis which was done in R.

In Stata I have used the margins command to calculate the average adjusted predictions for a logistic model. I thought the other analyst used the lsmeans package to do the same thing however when I run margins in Stata I get a different output than R provides with lsmeans, even though the datasets and coefficients from the regressions are identical.

My understanding was that margins is the Stata equivalent of lsmeans (or can be used as such) but that does not appear to be the case. So I have a two questions:
1. What is the difference between lsmeans and margins (besides language)?
2. How can I replicate R's lsmeans in Stata?

$\endgroup$
1
  • $\begingroup$ You might want to provide some example code here for both, ideally on a toy dataset. $\endgroup$
    – dimitriy
    Sep 25 '18 at 3:56
2
$\begingroup$

Welcome to CrossValidated.

I don't know what Stata does, but I do know what lsmeans() does: it obtains predictions on a regular grid of factor values, and the marginal lsmeans are obtained by averaging those predictions together with equal weights.

So one possibility is that Stata margins does not use equal weights. If so, you can check to see if you get the same results as Stata if you change the weights -- e.g., lsmeans(..., weights = "prop"); see the documentation for lsmeans().

Another issue, given that this is logistic regression, is what scale those margins are computed on. The lsmeans package, by default, computes the marginal averages on the logit scale (i.e., the linear-predictor scale), and if you specify type = "response", it will back-transpose the marginal averages to the probability scale. But it is possible that margins back-transforms first and averages later, which would produce different results.

By the way, there has been no new development or updates of lsmeans for about a year, because it has been superseded by the emmeans package (which still provides an lsmeans() function as an alias for emmeans()). I recommend you install emmeans and use that in the future. The emmeans package has quite a few vignettes that you can access via https://cran.r-project.org/package=emmeans. The one on basics explains the rudiments of emmeans (aka lsmeans), and the one on transformations discusses how to change when the back-transforming is done relative to the averaging.

I hope this answer helps on the R end. I can't help with getting Stata to do the same thing, though; sorry.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.