I'm working with survey data of a complex sample to estimate binary outcome models. I am trying to report average marginal effects of a logit model, which I estimated through svyglm of the survey package in R. However, I get the following error when I use margins from the package of the same name:

margins(fit, design = lapop) %>% summary()

Error in h(simpleError(msg, call)) : error in evaluating the argument 'object' in selecting a method for function 'summary': arguments imply differing number of rows: 6068, 6054

Seems it is not the summary function, since the error pops up when executing the margins command with its arguments. I have tried to simply ignore the survey weights at all and shows me equal coefficients and AMEs but not standard errors. Obviously, I cannot present this work by ignoring the survey weights. So I guess what I really need is the standard errors.

I have been reading on the topic and have found no clear solution, I suspect it might have something to do with missing values of the X in the model, but as with any other linear model, R should be just working with complete cases.

I'm not sure if anybody knows anything about this, or if I should simply just report AMEs without standard errors (and thus without p-values). I have uploaded a MWE if anyone is interested, which can be found here.

  • 1
    $\begingroup$ Getting conditional effects would be trivial and would also allow you to ignore sample weights if weighting was done with respect to covariates in the model. What makes you interested in marginal estimates? $\endgroup$ Aug 21, 2021 at 22:57
  • $\begingroup$ @FrankHarrell not so sure what you mean: as far as I know, if I ignore the survey weights in any kind of regression analysis I'm violating the random sampling assumption, and thus getting unbiased estimates as well as invalid standard errors. Regarding a logit model, I also understand that the coefficients reported are not of interest other than knowing the direction of the effect, which is why I'd like to get average partial effects for this model. Not sure if there is a different statistical treatment for these kinds of problems. $\endgroup$ Aug 23, 2021 at 1:27
  • $\begingroup$ That is not correct unless you wish to get marginal estimates, i.e., mix apples and oranges e.g. estimate the probability of a person being pregnant without being given their sex. When you condition on covariates you don't need random sampling with respect to those covariates. $\endgroup$ Aug 23, 2021 at 12:53
  • $\begingroup$ Interesting: my models will estimate the probability of a person answering "Yes" to a question asking about corruption being justified in special circumstances. Thus, if my covariates are age, gender, urban/rural setting, political "wing", among others. You're suggesting it wouldn't be wrong to drop the survey weights? $\endgroup$ Aug 23, 2021 at 15:08
  • $\begingroup$ Correct, if the weights were derived from any or all of those covariates. If in addition weighting was done by geographical region, you'd need to put region in the model even if you are not that interested in the effect of region. $\endgroup$ Aug 23, 2021 at 16:42

2 Answers 2


What version of the margins package are you using? In 0.3.26 (which dates from January) there's a margins.svyglm method, and it seems to work

> fit<-svyglm(api00~ell+meals+mobility, design=dclus2)
> margins(fit)
Note: Estimating marginal effects without survey weights. Specify 'design' to adjust for weighting.
Average marginal effects
svyglm(formula = api00 ~ ell + meals + mobility, design = dclus2)

    ell  meals mobility
 -2.059 -1.777   0.3253
> margins(fit, design=dclus2)
Average marginal effects (survey-weighted)
svyglm(formula = api00 ~ ell + meals + mobility, design = dclus2)

    ell  meals mobility
 -2.059 -1.777   0.3253
> summary(margins(fit, design=dclus2))
   factor     AME     SE       z      p   lower  upper
      ell -2.0592 1.4076 -1.4629 0.1435 -4.8180 0.6997
    meals -1.7772 1.1053 -1.6078 0.1079 -3.9436 0.3892
 mobility  0.3253 0.5305  0.6131 0.5398 -0.7145 1.3650

If your problem is missing data you can subset those data out

fit2<-svyglm(ctol ~ y16 + age,
             design = lapop[-fit$na.action,],
             family = quasibinomial(link = 'logit'))
  • $\begingroup$ Yes, I am indeed trying to estimate marginal effects with this method with the 0.3.26 version by specifying design as an argument. However, this is when the error pops up, as can be seen in my MWE. $\endgroup$ Aug 23, 2021 at 1:39

So I've found out what was happening here: it is a programming issue and most likely belongs in StackOverflow, not here. If you are interested, you can see my answer here.

Regarding the statistical aspects of the question, it appears that it might not at all be needed to use survey weights as long as the variables which define the survey design are included in the regression. Special thanks to Frank Harrell for pointing this out. This working paper expands on Professor Harrell's comments.

  • $\begingroup$ It's perhaps worth noting that the answer is incorrect in general, though ok in this case. The issue is missing data, but to get correct standard errors you should remove the missing observation after declaring the survey design (as in my answer here), not before. $\endgroup$ Sep 4, 2021 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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