【问题标题】:Extract inclusion probabilities and positive probabilities from BoomSpikeSlab model从 BoomSpikeSlab 模型中提取包含概率和正概率
【发布时间】:2018-07-24 08:16:50
【问题描述】:

BoomSpikeSlab 模型的默认 plot 函数是每个预测变量包含概率的条形图,并根据其为正的概率着色:

set.seed(0)
simulate.lm.spike <- function(n=100, p=10, ngood=3, niter=1000, sigma=1) {
  x <- cbind(matrix(rnorm(n * (p - 1)), nrow=n))
  beta <- c(rnorm(ngood), rep(0, p - ngood))
  y <- rnorm(n, beta[1] + x %*% beta[-1], sigma)
  draws <- lm.spike(y ~ x, niter=niter)
  return(invisible(draws))
}

model <- simulate.lm.spike(n=1000, p=50, sigma=.3)
plot(model, inclusion.threshold=.01)

如何提取此图背后的数据,即具有每个预测变量的包含概率和为正概率的数据框?

【问题讨论】:

    标签: r statistics


    【解决方案1】:

    适配PlotMarginalInclusionProbabilities函数:

    GetMarginalInclusionProbabilities = function(
        model,
        burn = 0,
        inclusion.threshold = 0,
        unit.scale = TRUE,
        number.of.variables = NULL) {
      beta <- model$beta
      if (burn > 0) {
        beta <- beta[-(1:burn), , drop = FALSE]
      }
      inclusion.prob <- colMeans(beta != 0)
      index <- order(inclusion.prob)
      beta <- beta[, index, drop = FALSE]
      inclusion.prob <- inclusion.prob[index]
    
      compute.positive.prob <- function(x) {
        ## Compute the probability that x is positive, given that it is
        ## nonzero.  If all(x == 0) then zero is returned.
        x <- x[x != 0]
        if (length(x) == 0) {
          return(0)
        }
        return(mean(x > 0))
      }
      positive.prob <- apply(beta, 2, compute.positive.prob)
    
      res <- data.frame(predictor = names(inclusion.prob),
                        inclusion.prob = inclusion.prob,
                        positive.prob = positive.prob)
    
      return(res[order(-res$inclusion.prob), ])
    }
    

    例子:

    GetMarginalInclusionProbabilities(model)  
    #               predictor inclusion.prob positive.prob
    # (Intercept) (Intercept)          1.000             1
    # x1                   x1          1.000             0
    # x2                   x2          0.999             1
    # x15                 x15          0.014             1
    # x43                 x43          0.002             1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-16
      相关资源
      最近更新 更多