我试图产生自助信心和间隔'对于使用的回归树的特定拆分
rpart
(生成树)和boot
(引导) - 详细阐述this问题/答案。
示例:
data(iris)
library(rpart)
r1<-rpart(Sepal.Length ~ ., cp = 0.05, data=iris)
plot(r1)
text(r1)
library(boot)
trainData <- iris[-150L, ]
predictData <- iris[150L, ]
rboot <- boot(trainData, function(data, idx) {
bootstrapData <- data[idx, ]
r1 <- rpart(Sepal.Length ~ ., bootstrapData, cp = 0.05)
predict(r1, newdata = predictData)
}, 1000L)
生成分位数,因为rpart
没有CI函数:
quantile(rboot$t, c(0.025, 0.975))
2.5% 97.5%
5.871393 6.766842
那没关系,但是,我怎样才能获得分位数&#39;根据预测值估算每次拆分。例如,&#34; Petal.Length&lt; 3.4&#34;?
的任意一侧的分位数答案 0 :(得分:1)
这是一个解决方案(由非会员提供)。唯一的问题是没有。分裂在引导运行之间波动,可能是小n的函数。
library(boot)
library(rpart)
library(lattice)
data(iris)
names(iris)
iris2 <- iris[,c(1,3)]
r1 <- rpart(Sepal.Length ~ Petal.Length, cp = 0.05, data=iris2)
r1$splits
r1$frame
plot(r1)
text(r1)
n.boot <- 10000
n.split <- 3 #change this according to no. of splits on tree
store_matrix <- array(0,c(n.boot,n.split))` #column 1 will contain split, col 2 split 2, etc
trainData <- iris2
for (i in 1:n.boot) {
iboot <- sample(1:nrow(trainData), replace = TRUE)
bootdata <- trainData[iboot,]
r <- rpart(Sepal.Length ~ Petal.Length, bootdata, cp = 0.05)
r
r$frame
r$split
store_matrix[i,] <- r$splits[1:n.split,4]
}
split.n <- 2 #choose which split to look at
store1 <- store_matrix[,split.n] #select the distribution of split estimates for a specific split
(split_estimate <- r1$splits[split.n,4]) #check its the correct split
[1] 3.4
q1 <- quantile(na.omit(as.numeric(store1)), c(0.025, 0.975))
quantile(na.omit(as.numeric(store1)), c(0.025, 0.975)); as.numeric(q1)[2] - as.numeric(q1)[1]
2.5% 97.5%
1.45 5.65
[1] 4.2