r - 为 gbm 交互强度实现零分布

标签 r machine-learning gbm

我正在尝试使用 Friedman 和 Popescu 2008 https://projecteuclid.org/euclid.aoas/1223908046 中描述的方法确定 gbm 模型中的哪些交互作用是重要的。 .我的 gbm 是一个有 9 个不同类别的分类模型。
我正在努力将第 8.3 节翻译成代码以在 R 中运行。
我认为整个过程是:

  • 用 max.depth = 1
  • 训练模型的一个版本
  • 模拟来自该模型的响应数据
  • 在此数据上训练一个新模型,最大深度与真实模型相同
  • 获取此模型的交互强度
  • 重复步骤 1-4 以创建交互强度的零分布

  • 我发现最令人困惑的部分是实现方程 48 和 49。(您必须查看链接的文章,因为我无法在此处重现它们)
    这是我认为我理解的,但如果我错了,请纠正我:
    y_i 是一个新的响应向量,我们将使用它来训练一个新模型,该模型将提供交互统计的零分布。
    F_A(x_i) 是使用 max.depth = 1 训练的 gbm 模型版本的预测
    b_i 是基于加法模型 F_A(x_i) 预测的 0 到 1 之间的概率
    问题
  • 什么是下标i?它是 bootstrap 中的迭代次数吗?
  • 每个人工数据集与其他数据集有何不同?
  • 我们是否将 Pr(b_i = 1) 代入方程 48?
  • 这如何通过多项分类来完成?
  • 如何在 R 中实现这一点?最好使用 gbm 包。

  • 欢迎任何想法或引用!

    最佳答案

    总体而言,该过程是中和 y 中的交互效应的一种优雅方式。通过置换/重新分配建模对交互的额外贡献。额外的贡献可以通过完整模型和加法模型之间的边际来捕获。

    1. What is subscript i? Is it the number of iterations in the bootstrap?

    它是样本的索引。有N每次迭代中的样本。
    1. How is each artificial data set different from the others?

    预测变量 X跨数据集相同。响应值 Y~由于 equation 47 中边距的随机排列而不同equation 48 中的随机实现(仅适用于分类结果) .
    1. Are we subbing the Pr(b_i = 1) into equation 48?

    是的,如果结局Y是二进制的。
    1. How can this be done with multinomial classification?

    一种方法是在每个类别的对数赔率中随机排列边距。随后根据来自加性模型的概率随机实现。
    1. How would one implement this in R? Preferably using the gbm package.

    我试图按照您的整体流程实现它。
    首先,模拟训练数据集{X1,X2,Y}尺寸N =200 其中 Y具有由 Y1 确定的概率实现的三类( Y2Y3X1 ) , X2 .互动部分X1 * X2Y1 ,而添加部分在 Y2 , Y3 .
    set.seed(1)
    N <- 200
    X1 <- rnorm(N) # 2 predictors
    X2 <- rnorm(N)
    
    #log-odds for 3 categories
    Y1 <- 2*X1*X2 + rnorm(N, sd=1/10) # interaction
    Y2 <- X1^2 + rnorm(N, sd=1/10) #additive
    Y3 <- X2^2 + rnorm(N, sd=1/10) #additive
    Y <- rep(NA, N) # Multinomial outcome with 3 categories
    for (i in 1:N)
      {
        prob <- 1 / (1 + exp(-c(Y1[i],Y2[i],Y3[i]))) #logistic regression
        Y[i] <- which.max(rmultinom(1, 10000, prob=prob)) #realisation from prob
      }
    Y <- factor(Y)
    levels(Y) <- c('Y1','Y2','Y3')
    table(Y)
    #Y1 Y2 Y3 
    #38 75 87
    dat = data.frame(Y, X1, X2)
    head(dat)
    # Y         X1         X2
    # 2 -0.6264538  0.4094018
    # 3  0.1836433  1.6888733
    # 3 -0.8356286  1.5865884
    # 2  1.5952808 -0.3309078
    # 3  0.3295078 -2.2852355
    # 3 -0.8204684  2.4976616
    
  • 使用 max.depth 训练完整模型和加法模型= 2 和 1 分别。
  • library(gbm)
    n.trees <- 100
    F_full <- gbm(Y ~ ., data=dat, distribution='multinomial', n.trees=n.trees, cv.folds=3,
                interaction.depth=2) # consider interactions
    F_additive <- gbm(Y ~ ., data=dat, distribution='multinomial', n.trees=n.trees, cv.folds=3,
                interaction.depth=1) # ignore interactions
    
    #use improved prediction as interaction strength
    interaction_strength_original <- min(F_additive$cv.error) - min(F_full$cv.error)
    > 0.1937891
    
  • 模拟来自该模型的响应数据。
  • #randomly permute margins (residuals) of log-odds to remove any interaction effects
    margin <- predict(F_full, n.trees=gbm.perf(F_full, plot.it=FALSE), type='link')[,,1] -
             predict(F_additive, n.trees=gbm.perf(F_additive, plot.it=FALSE), type='link')[,,1]
    margin <- apply(margin, 2, sample) #independent permutation for each category (Y1, Y2, Y3)
    
    Y_art <- rep(NA, N) #response values of an artificial dataset
    for (i in 1:N)
      {
        prob <- predict(F_additive, n.trees=gbm.perf(F_additive, plot.it=FALSE), type='link',
                      newdata=dat[i,])
        prob <- prob + margin[i,] # equation (47)
        prob <- 1 / (1 + exp(-prob))
        Y_art[i] <- which.max(rmultinom(1, 1000, prob=prob)) #Similar to random realisation in equation (49)
      }
    Y_art <- factor(Y_art)
    levels(Y_art) = c('Y1','Y2','Y3')
    
    table(Y_art)
    #Y1 Y2 Y3 
    #21 88 91
    
  • 使用 max.depth 在此人工数据上训练新模型(2) 同实物模型
  • F_full_art = gbm(Y_art ~ ., distribution='multinomial', n.trees=n.trees, cv.folds=3,
                data=data.frame(Y_art, X1, X2),
                interaction.depth=2)
    F_additive_art = gbm(Y_art ~ ., distribution='multinomial', n.trees=n.trees, cv.folds=3,
                data=data.frame(Y_art, X1, X2),
                interaction.depth=1)
    
  • 获取此模型的交互强度
  • interaction_strength_art = min(F_additive_art$cv.error) - min(F_full_art$cv.error)
    > 0.01323959 # much smaller than interaction_strength_original in step 1.
    
  • 重复步骤 2-4 以创建交互强度的零分布。正如预期的那样,中和数据集中的交互效应比原始训练数据集中的低得多(-0.0527 到 0.0421)(0.1938)。
  • interaction_strength_art <- NULL
    for (j in 1:10)
      { 
        print(j)
        interaction_strength_art <- c(interaction_strength_art, step_2_to_4())
      }
    
    summary(interaction_strength_art)
    #     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
    #-0.052648 -0.019415  0.001124 -0.004310  0.012759  0.042058
    
    interaction_strength_original
    > 0.1937891
    

    关于r - 为 gbm 交互强度实现零分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54464804/

    相关文章:

    r - 在gbm多项式分布中,如何使用predict得到分类输出?

    r - 为什么R中有两个赋值运算符 `<-`和 `->`?

    r - 创建本地 R 包存储库

    r - 非等连接一步添加 data.table 中范围表的所有列

    machine-learning - 如何更新反卷积层的权重?

    python - 如何使用 Opencv 和 Python 检测图像中的白色区域?

    r - 在 R 中增强多类分类树

    r - 数据集上的cbind无效下标类型 'list'

    python - 使用 Keras Sequential 模型实现快捷方式

    python - 尽管设置了种子,仍无法重现 H2O GBM 预测