I have a logit model with 4 independent variables:
logit <- glm(y ~ x1 + x2 + x3 + x4, family = binomial(), data = df)
All variables in the model are dichotomous (0,1).
I want to get the predicted probabilities for when x1=1 vs x1=0, while holding all other variables in the model constant, using the following code:
mean(predict(logit,transform(df,x1=1),type='response'))
mean(predict(logit,transform(df,x1=0),type='response'))
Is this the correct way to do this? I'm asking because I tried a few different logit models and compared the difference between the two values produced by the code above with the number the margins command produces:
summary(margins(logit, variables="treatment"))
For most models I tried, the difference between the two values equals the number produced by the margins command above, but for one model specification the numbers were slightly different.
Finally, when I use weights in my logit model, do I have to do anything differently when I calculate the predicted probabilities than when I don't include any weights?
Your approach seems reasonable. The only thing I’ll note is that there are many packages out there which will do this automatically for you, and can estimate standard errors. One example is the
marginaleffectspackage. (Disclaimer: I am the author.)FYI, there is a
wtsargument to handle weights in thepredictions()function.Edit: