-
Notifications
You must be signed in to change notification settings - Fork 40
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
design in margins.svyglm #124
Comments
I tried a little myself and apart of this there is also a problem within prediction package and error message obtained with calling a code I provided applies to that problem. Nevertheless problem of getting weights from data (survey design's subset) on which model actually was estimated still needs to be solved afterwards and solution I've proposed above seems to work. |
Little correction, so it all works also with svrepdesign objects - weights should be obtained as:
|
Oh, and I've just realized that this (what should come instead of lines 25-32) should be a little more complicated to allow use of survey design objects as data argument: if (inherits(data, c("survey.design", "svyrep.design"))) {
wts <- weights(data, type = "sampling")
data <- find_data(data)
} else {
wts <- weights(model$survey.design, type = "sampling")
} |
You mentioned replacing the code lines 25-32 in the margins.svyglm.R in margins package 0.3.23. However, when I try replacing the code line 25-32 of version 0.3.26 with the code above, it does not work. Do you know what lines 25-32 in the older version correspond to in the version 0.3.26? Many thanks, |
Well, @leeper has updated the package since I wrote this post and it became a little incompatible.
|
@tzoltak thank you! The forks above worked on the margins version I have. I've been trying to find a solution for this for days! I really appreciate the quick response and the solution. |
@marcondesfel, your welcome! :) |
@tzoltak does the margins package in R allow to calculate pairwise comparisons (for diff-in-diff analysis)? Or should I use another package like emmeans? |
Unluckily I'm not sure, what exactly you're asking about @marcondesfel
|
My apologies for the lack of clarity @tzoltak. With the margins code below, I can get the 1st difference (the average marginal effects for control vs treatment groups)
However, now I want to go a step further and get the 2nd difference (difference in difference). in Stata, I can do margins, dydx(*) pwcompare. How can I get pairwise comparisons of average marginal effects in R? I tried the code below with package emmeans:
But that does not seem correct. How can do it with the margins package in R? |
Well, there are two answers to your question, @marcondesfel: 1. Technical one: There is no pwcompare-like feature in margins but there is rather easy workaround: simply you must create one variable that describes all the possible situations that are modeled by the interaction (and main effects of variables involved in this interaction). Now you should use this variable as a predictor in your model instead of variables you used to create it. Further you run I suppose that code like this should work for you (assuming you're interested primarily in how x=1 and a=1 differs comparing to x=0 and a=1):
In such a simple situation, i.e. with no additional predictors, you may compute size of the effects on the response scale simply by examining conditional means of the dependent variable in 4 groups defined by crossing The only reason you want to use a model - logistic regression in your case - in this simple situation is to test hypothesis regarding these effects. But to do this you may (and in fact even should) simply look at significance of model coefficients (on the link function scale) and perhaps significance of their linear combinations (in case of svyglm objects see function The only reason this p-levels may change while turning to effects on the response scale is by using approximations necessary to compute variances after nonlinear transformations of random variables - that's why it is better to avoid them (and that's why you can get results of a call to |
Hi @tzoltak, Thanks for the code provided above. But I still cannot seem to get the average marginal effect that I really want. I have the following svyglm model t: To the 2nd difference (the DiD) I tried the code you suggested for the simple model without any covariates (model t2) as below: But the 1st difference and 2nd difference (1st diff before fee bump - 1st diff after fee bump) do not match. I get the following output: I was expecting a number for the AME closer to 0.038 (for yes vs no fee raise in the after feebump period). What am I doing wrong? |
Dear @marcondesfel,
|
Hello there! I'm trying to use I'm not sure if has something to do with the fork and the missing value problems, or something to do with my R session. I'm attaching the session info below. Thanks in advance for your help in this!
|
Dear @dsanchezp18 I don't think it has much to do with my fork specifically. What I have done regarding use of svyglm objects is simply changing the place in which margins is searching for data and information about survey design. Moreover, what margins do internally in general is calling |
I see, @tzoltak , thank you for your insight. It does surprise me that there is only one way to compute average partial effects of survey-weighted glm's, at least to the full extent of my own knowledge. Thanks! |
margins()
S3 method for svyglm object now requires providing design object as an additional argument. This causes trouble if there are missings in some variable included in a model:Importantly, providing survey design object as an argument to
margins()
seems completely unnecessary, as it is included in a svyglm object and it is already restricted only to observations included in estimation - please run:So simple solution should be replacing lines 25-32 in "margins_svyglm.R" with something like:
The text was updated successfully, but these errors were encountered: