首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用bartMachine R软件包预测的概率是失败概率

使用bartMachine R软件包预测的概率是失败概率
EN

Stack Overflow用户
提问于 2016-10-26 22:23:28
回答 1查看 531关注 0票数 2

如果我使用bartMachine运行用于分类的BART模型,则返回的p_hat_train值对应的是失败概率,而不是BayesTree R包中BART的初始实现中所做的成功概率。

下面是一个模拟二进制响应的示例:

代码语言:javascript
复制
library(bartMachine)
library(BayesTree)
library(logitnorm)

N = 1000
X <- rnorm(N, 0, 1)
p_true <- invlogit(1.5*X)
y <- rbinom(N, 1, p_true)

## bartMachine
fit <- bartMachine(data.frame(X), as.factor(y), num_burn_in = 200,
                   num_iterations_after_burn_in = 500)
p_hat <- fit$p_hat_train

## BayesTree
fit2 <- bart(X, as.factor(y), ntree = 50, ndpost = 500)
p_hat2 <- apply(pnorm(fit2$yhat.train), 2, mean)

par(mfrow = c(2,2))
plot(p_hat, p_true, main = 'p_hat_train with bartMachine')
abline(0, 1, col = 'red')
plot(1 - p_hat, p_true, main = '1 - p_hat_train with bartMachine')
abline(0, 1, col = 'red')
plot(p_hat2, p_true, main = 'pnorm(yhat.train) with BayesTree')
abline(0, 1, col = 'red')

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-26 23:19:42

检查iris示例从?bartMachine中可以看出,bartMachine正在估计一个观察被归类为y变量的第一级的概率,而在您的示例中,这个级别恰好是0。要获得您想要的结果,您需要在将y转换为一个因子时指定级别,即

代码语言:javascript
复制
fit <- bartMachine(data.frame(X), factor(y, levels = c("1", "0")), 
  num_burn_in = 200,
  num_iterations_after_burn_in = 500)

当我们检查build_bart_machine的代码时,我们可以看到发生了什么

代码语言:javascript
复制
if (class(y) == "factor" & length(y_levels) == 2) {
        java_bart_machine = .jnew("bartMachine.bartMachineClassificationMultThread")
        y_remaining = ifelse(y == y_levels[1], 1, 0)
        pred_type = "classification"
    }

查看来自bartMachine的输出(使用您的原始规范)显示了正在发生的事情:

代码语言:javascript
复制
head(cbind(fit$model_matrix_training_data, y))
#             X y_remaining y
# 1 -0.85093975           0 1
# 2  0.20955263           1 0
# 3  0.66489564           0 1
# 4 -0.09574123           1 0
# 5 -1.22480134           1 0
# 6 -0.36176273           1 0
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40273072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档