I have fit a multinomial logistic model as shown below. Using the margins command, I would like to obtain the predicted values for my outcome variable while setting the predictors to some specific values. In other words, I would like to replicate the Stata code below. Chatgpt gave me code that I pasted below as well but it returns an error of "Error in find_terms_in_model.default(model, variables = variables): Some values in 'variables' are not in the model terms."
my multinomial logist code
library(nnet)
model <- multinom(obesity_E ~ age_100 + I(age_100^2) + obesity + age_100:obesity +
covid:race + age_100:race + education + education:race +
rabplace_5, data = female_98_2020, maxit=1000)
Code suggested by ChatGPT
margins_model <- margins(model, variables = "obesity:race:rabplace_5",
at = list(age_100 = seq(0, 25, 1)), atMethod = "mean",
method = "probs", force = TRUE, noSe = TRUE,
save = "tran_point_F", replace = TRUE)
My Stata code
margins , at (age_100=(0 (1) 25) obesity=(1 (1) 4) race=(0 (1) 3) rabplace_5=(1 (1) 5)) atmeans force nose saving(tran_point_F, replace )
The
marginspackage only computes slopes, not predictions. Also, that package is not being actively maintained and developed anymore (perhaps only critical bug fixes).You can use the
marginaleffectspackage instead, which is a newer package I developed as a more flexible successor tomargins(note: conflict of interest).See this vignette for a syntax comparison with Stata: https://vincentarelbundock.github.io/marginaleffects/articles/alternative_software.html
The code you need will probably look similar to this: