使用Rstanarm计算二项式Logit中的边际效应 [英] Calculating marginal effects in binomial logit using rstanarm

查看:278
本文介绍了使用Rstanarm计算二项式Logit中的边际效应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此帖子,我正在尝试获得边际效应: http://andrewgelman.com/2016/01/14/rstanarm-and-more/

I am trying to get the marginal effects, according to this post: http://andrewgelman.com/2016/01/14/rstanarm-and-more/

td <- readRDS("some data")

CHAINS <- 1
CORES <- 1
SEED <- 42
ITERATIONS <- 2000
MAX_TREEDEPTH <- 9

md <- td[,.(y,x1,x2)] # selection the columns i need. y is binary


glm1 <- stan_glm(y~x1+x2,
                 data = md,
                 family = binomial(link="logit"),
                 prior = NULL,
                 prior_intercept = NULL,
                 chains = CHAINS,
                 cores = CORES,
                 seed = SEED,
                 iter = ITERATIONS,
                 control=list(max_treedepth=MAX_TREEDEPTH)
)

# launch_shinystan(glm1) 


tmp <- posterior_predict(glm1,newdata=md[,.(x1,x2)])

问题

运行此代码后,出现以下错误: 我收到未找到y的错误,这实际上意味着我还需要在newdata中传递y,根据?posterior_predict

After running this code i get the following error: I get an error that y not found, which actually means that i also need to pass y in the newdata, which it shouldn't be the case according to ?posterior_predict

理由

我需要tmp <- posterior_predict(glm1,newdata=md[,.(x1,x2)]),因为根据上面的帖子(据我所知),为了计算x1的边际效应(如果我假设x1是二进制的)将是

I need tmp <- posterior_predict(glm1,newdata=md[,.(x1,x2)]) because according to the post above (as far as i understand), in order to calculate the marginal effect of x1 (if i assume that x1 is binary) would be

temp <- md
temp[,x1:=0]
temp[,x2:=mean(x2)]
number_0 <- posterior_predict(glm1,newdata=temp)

temp <- md
temp[,x1:=1]
temp[,x2:=mean(x2)]
number_1 <- posterior_predict(glm1,newdata=temp)

marginal_effect_x1 <- number_1 - number_0

推荐答案

对于二进制logit模型,连续变量的边际效应是该变量成功概率的导数,根据链式规则,该概率为逻辑密度(在预测变量的某些值处评估,通常是预测变量的观测值)乘以相关变量的系数.在你的情况下,那将是 df <- as.data.frame(glm1) ME <- df$x2 * dlogis(posterior_linpred(glm1)) 由于这取决于预测变量的观测值,因此通常对 AME <- rowMeans(ME) 对于二进制预测变量,您可以通过x1 = 1时的成功概率减去x1 = 0时的成功概率. nd <- md nd$x1 <- 0 p0 <- posterior_linpred(glm1, newdata = nd, transform = TRUE) nd$x1 <- 1 p1 <- posterior_linpred(glm1, newdata = nd, transform = TRUE) ME <- p1 - p0 AME <- rowMeans(ME)

For a binary logit model, the marginal effect of a continuous variable is the derivative of the probability of success with respect to that variable, which by the chain rule is the logistic density (evaluated at some values of the predictors, usually the observed values of the predictors) multiplied by the coefficient of the variable in question. In your case, that would be df <- as.data.frame(glm1) ME <- df$x2 * dlogis(posterior_linpred(glm1)) Since this depends on the observed values of the predictors, it is common to average over the data with AME <- rowMeans(ME) In the case of a binary predictor, you can just subtract the probability of success when x1 = 0 from the probability of success when x1 = 1 via nd <- md nd$x1 <- 0 p0 <- posterior_linpred(glm1, newdata = nd, transform = TRUE) nd$x1 <- 1 p1 <- posterior_linpred(glm1, newdata = nd, transform = TRUE) ME <- p1 - p0 AME <- rowMeans(ME)

这篇关于使用Rstanarm计算二项式Logit中的边际效应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆