如何从bsts R包中提取包含概率

How to extract inclusion probabilities from bsts R package

bsts R 包(贝叶斯结构时间序列)计算模型中每个预测变量的包含概率,可以通过 PlotBstsPredictors 函数绘制。如何将这些提取为给定 bsts 模型的值?

GetInclusionProbabilities <- function(bsts.object) {
  # Pulls code from
  # - BoomSpikeSlab::PlotMarginalInclusionProbabilities
  # - bsts::PlotBstsCoefficients
  burn <- SuggestBurn(0.1, bsts.object)
  beta <- bsts.object$coefficients
  beta <- beta[-(1:burn), , drop = FALSE]
  inclusion.prob <- colMeans(beta != 0)
  index <- order(inclusion.prob)
  inclusion.prob <- inclusion.prob[index]
  # End from BoomSpikeSlab/bsts.
  return(data.frame(predictor = names(inclusion.prob),
                    inclusion.prob = inclusion.prob))
}

这也可以通过传递 impact$model$bsts.modelCausalImpact 一起使用。