为rstanarm中的多个预测变量设置先验?

时间:2016-11-01 15:02:38

标签: r rstan

对于如何为以下模型设置多个预测变量的先验,我有点困惑:

require(rstanarm)

wi_prior <- normal(0, sd(train$attendance))
SEED     <- 101

fmla <- attendance ~ (1 + W + W1 + W2 + W3 + DivWin1 + DivWin2 + DivWin3 + 
                      WSWin1 | franchID)

baylm <- stan_glmer(fmla, 
                    data = train, 
                    family = "gaussian", 
                    algorithm = "sampling",
                    adapt_delta = .95,
                    prior_intercept = wi_prior, seed = SEED)

根据要求,这是火车上的第一次观察。

train <- structure(list(franchID = structure(25L, .Label = c("ANA", "ARI", 
                                                             "ATL", "BAL", "BOS", "CHC", "CHW", "CIN", "CLE", "COL", "DET", 
                                                             "FLA", "HOU", "KCR", "LAD", "MIL", "MIN", "NYM", "NYY", "OAK", 
                                                             "PHI", "PIT", "SDP", "SEA", "SFG", "STL", "TBD", "TEX", "TOR", 
                                                             "WSN"), class = "factor"), yearID = 1999L, name = "San Francisco Giants", 
                        park = "3Com Park", attendance = 2078399L, W = 86L, W1 = 89L, 
                        W2 = 90L, W3 = 68L, WCWin1 = FALSE, WCWin2 = FALSE, WCWin3 = FALSE, 
                        DivWin1 = FALSE, DivWin2 = TRUE, DivWin3 = FALSE, LgWin1 = FALSE, 
                        LgWin2 = FALSE, LgWin3 = FALSE, WSWin1 = FALSE, WSWin2 = FALSE, 
                        WSWin3 = FALSE), .Names = c("franchID", "yearID", "name", 
                                                    "park", "attendance", "W", "W1", "W2", "W3", "WCWin1", "WCWin2", 
                                                    "WCWin3", "DivWin1", "DivWin2", "DivWin3", "LgWin1", "LgWin2", 
                                                    "LgWin3", "WSWin1", "WSWin2", "WSWin3"), row.names = c(NA, -1L
                                                    ), class = "data.frame")

1 个答案:

答案 0 :(得分:2)

您可以通过将长度为K的向量传递给先验支持的分布之一来为K个预测变量指定系数的先验。例如,如果K = 4,则可以

wi_prior2 <- normal(location = c(0, 1, -2, 5))

您还可以传递比例矢量和/或与normal不同的系列。然后,您可以使用stan_glmer致电prior = wi_prior2。如果你这样做

wi_prior2 <- normal(location = 0)

然后相同的先验将用于所有K个公共系数。

但是,在您的情况下,我怀疑fmla是错误的。您通常还希望在lme4样式的括号表达式之外包含大多数(如果不是全部)预测变量,以允许franchID的所有级别的共同效果。因此,fmla将成为

fmla <- attendance ~ W + W1 + W2 + W3 + DivWin1 + DivWin2 + DivWin3 + 
        WSWin1 + (1 + W + W1 + W2 + W3 + DivWin1 + DivWin2 + DivWin3 + 
                  WSWin1 | franchID)

如果您只在括号中包含该部分,那么您假设这些变量的系数在总体中恰好为零,并且仅在由franchID的级别定义的子群中偏离零。因此,没有机会将先前的分布放在它们的系数上。

与公共系数的分组偏差之前的条件是条件多变量法线,其中平均向量为零,并且有些复杂但未知的协方差结构。这在help(priors, package = "rstanarm")中有更详细的解释。