简体   繁体   English

在 R 中将先验信息纳入岭回归 (RAPM)

[英]Incorporating Prior Information in a Ridge Regression (RAPM) in R

I am currently using R's glmnet package to run a weighted ridge regression on hockey data.我目前正在使用 R 的glmnet package 对曲棍球数据进行加权岭回归。 I have a sparse matrix with dummy variables denoting whether a player is on the ice playing offense or defense for a given shift, in addition to a few other predictors such as home ice advantage.我有一个带有虚拟变量的稀疏矩阵,除了一些其他预测变量(例如主场冰优势)之外,该矩阵表示球员在给定轮换中是在冰上进攻还是防守。 I have a vector of weights which is the length of each shift.我有一个权重向量,它是每个班次的长度。 My target variable is a vector of shot rates that occur in a given shift.我的目标变量是在给定班次中发生的射速向量。

The glmnet call is as follows: glmnet调用如下:

glmnet(y = shot_rates, x = dummy_matrix, weights = shift_length, lambda = previously_obtained_lambda)

(The Lambda is obtained through cross validation on the same data set which is also done using glmnet .) (Lambda 是通过对同一数据集的交叉验证获得的,这也是使用glmnet完成的。)

As of right now the distribution is entirely Gaussian and every predictor variable is biased towards a mean of zero.截至目前,分布完全是高斯分布,每个预测变量都偏向于均值为零。 I am looking to incorporate prior information (prior means) for each dummy variable and possibly set separate lambda values for each of them but I am not sure how I go about doing that.我希望为每个虚拟变量合并先验信息(先验方法),并可能为每个虚拟变量设置单独的 lambda 值,但我不确定我是如何做到的。 I believe I can use penalty.factors to adjust the lambdas for each variable so we can put that aside for now and focus on the prior means.我相信我可以使用penalty.factors 来调整每个变量的lambdas,这样我们就可以暂时把它放在一边,专注于之前的方法。

I have looked into using the bayesglm package and implementing prior.means but my issues with it are two-fold: it is slow as it is and it does not accept sparse matrices which makes things significantly slower.我已经研究过使用bayesglm package 并实现prior.means,但我的问题有两个:它很慢,它不接受稀疏矩阵,这使得事情变得非常慢。 For reference, my matrix of dummy variables contains roughly 600,000 rows and roughly 2,000 columns.作为参考,我的虚拟变量矩阵包含大约 600,000 行和大约 2,000 列。

How might I go about efficiently incorporating prior means into my analysis?我如何有效地将先前的手段纳入我的分析? Thanks in advance for any suggestions.在此先感谢您的任何建议。

Okay, so based on the comment it seems like in principle a Bayesian approach is okay with you, and it's the only way I know to have regularizing priors not centred at 0. You also mentioned that speed was an issue, which is why I would recommend fitting a model using Stan, which is generally much faster than other Bayesian methods.好的,所以根据评论,原则上贝叶斯方法似乎对你来说是可以的,这是我所知道的唯一让先验不以 0 为中心的正则化方法。你还提到速度是一个问题,这就是为什么我会建议使用 Stan 拟合 model,这通常比其他贝叶斯方法快得多。 Also, brms and Stan have absolutely wonderful documentation that is much more useful than what you normally find for other statistical packages in R.此外,brms 和 Stan 拥有绝对精彩的文档,比您通常在 R 中找到的其他统计软件包有用得多。

brms is a very useful package that allows the fitting of Stan models in an lme4 -like syntax. brms是一个非常有用的 package ,它允许以lme4的语法拟合 Stan 模型。

In brms , priors can be specified for the intercept and each independent variable like this:brms中,可以为截距和每个自变量指定先验,如下所示:

model_priors <- c(
  prior(normal(0, 5), class = "Intercept"),
  prior(normal(0, 1), class = "b")
)

This code puts a prior of a normal distribution with a mean of 0 with a sd of 5 on the incercept, as well as a prior with a mean of 0 and sd of 1 on each beta coefficient.这段代码将一个正态分布的先验放在了一个平均值为 0 和 sd 为 5 的 incercept 上,以及一个平均值为 0 和 sd 为 1 的每个 beta 系数上的先验。 If you want each beta coefficient to have different its own prior, you can specify it like this:如果您希望每个 beta 系数具有不同的先验,您可以像这样指定它:

model_priors <- c(
  prior(normal(0, 5), class = "Intercept"),
  prior(normal(0.5, 1), class = "b", coef = "first_predictor"),
  prior(normal(-0.5, 1), class = "b", coef = "second_predictor")
)

This code changes sets specific priors for each of two hypothetical beta coefficients, notice how I have made it so they are no longer centred at 0.此代码更改为两个假设的 beta 系数中的每一个设置了特定的先验,请注意我是如何做到的,因此它们不再以 0 为中心。

You could then incorporate these priors into the model something like this然后,您可以将这些先验合并到 model 中,如下所示

modelfit <- brm(
  formula = outcome_variable ~ first_predictor + second_predictor, 
  data = df,
  prior = model_priors,
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM