【发布时间】:2017-07-11 14:30:51
【问题描述】:
根据这篇文章,我正在尝试获得边际效应: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 not found,这实际上意味着我还需要在newdata 中传递y,根据?posterior_predict 不应该是这种情况
推理
我需要tmp <- posterior_predict(glm1,newdata=md[,.(x1,x2)]),因为根据上面的帖子(据我所知),为了计算 x1 的边际效应(如果我假设 x1 是二进制的)将是
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
【问题讨论】:
-
虽然与您的问题无关,但仅使用 1 条链并不是一个好主意。而且您不必将
max_treedepth从其默认值(rstanarm 中的 15 与 rstan 中的 10)减少;将其设置为较高的值不会在未达到时造成任何伤害。