使用”margins”命令获取结果变量的预测值。

huangapple go评论127阅读模式
英文:

Obtaining predicted values of the outcome variable using margins command

问题

以下是翻译好的部分:

**我已经拟合了一个多项逻辑回归模型,如下所示。 使用margins命令,我想要在将预测变量设置为特定值的情况下获得我的结果变量的预测值。 换句话说,我想要复制下面的Stata代码。 ChatGPT给了我下面粘贴的代码,但返回了一个错误:“在find_terms_in_model.default(model,variables = variables)中的某些值不在模型项中。”

我的多项逻辑回归代码

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)

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)

我的Stata代码

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 )

请注意,这些代码已经被我翻译成中文,只包括代码部分,没有其他内容。

英文:

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 &lt;- 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 &lt;- margins(model, variables = &quot;obesity:race:rabplace_5&quot;,
                         at = list(age_100 = seq(0, 25, 1)), atMethod = &quot;mean&quot;,
                         method = &quot;probs&quot;, force = TRUE, noSe = TRUE,
                         save = &quot;tran_point_F&quot;, 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 ) 

答案1

得分: 2

margins包仅计算斜率,而不是预测值。此外,该包已不再积极维护和开发(可能只有关键错误修复)。

您可以改用marginaleffects包,这是我开发的一个更新的包,作为margins的更灵活的继任者(请注意:存在利益冲突)。

请参阅此文档以与Stata进行语法比较:<https://vincentarelbundock.github.io/marginaleffects/articles/alternative_software.html>

您可能需要的代码可能类似于以下内容:

library(nnet)
library(marginaleffects)

m &lt;- multinom(Species ~ Petal.Length + Petal.Width, data = iris, trace = FALSE)

avg_predictions(m, newdata = datagrid(Petal.Length = c(1, 4, 6)))
# 
#       Group Estimate Std. Error    z Pr(&gt;|z|)    S  2.5 % 97.5 % Petal.Width Petal.Length
#  setosa        0.333     0.0479 6.95  &lt; 0.001 38.0 0.2389  0.427         1.2            1
#  versicolor    0.382     0.1195 3.20  0.00139  9.5 0.1477  0.616         1.2            4
#  virginica     0.285     0.1192 2.39  0.01672  5.9 0.0516  0.519         1.2            6
# 
# Columns: rowid, group, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, Species, Petal.Width, Petal.Length

如果您需要进一步的帮助,请告诉我。

英文:

The margins package 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 marginaleffects package instead, which is a newer package I developed as a more flexible successor to margins (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:

library(nnet)
library(marginaleffects)

m &lt;- multinom(Species ~ Petal.Length + Petal.Width, data = iris, trace = FALSE)

avg_predictions(m, newdata = datagrid(Petal.Length = c(1, 4, 6)))
# 
#       Group Estimate Std. Error    z Pr(&gt;|z|)    S  2.5 % 97.5 % Petal.Width Petal.Length
#  setosa        0.333     0.0479 6.95  &lt; 0.001 38.0 0.2389  0.427         1.2            1
#  versicolor    0.382     0.1195 3.20  0.00139  9.5 0.1477  0.616         1.2            4
#  virginica     0.285     0.1192 2.39  0.01672  5.9 0.0516  0.519         1.2            6
# 
# Columns: rowid, group, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, Species, Petal.Width, Petal.Length

huangapple
  • 本文由 发表于 2023年6月2日 13:49:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定