【发布时间】:2023-04-03 16:05:02
【问题描述】:
我正在运行这个函数来进行 n 次交叉验证。错误分类率不会随折叠而变化,例如如果我跑 10 或 50。我也会收到警告:
“警告信息:
'newdata' 有 19 行,但找到的变量有 189 行"
如果我运行代码而不是函数的一部分,它就是我想要的 -> 例如对于 folds==1,它提取 10%,在 90% 的数据上运行模型,并预测另外 10%。 有没有人知道为什么它没有显示变量和折叠次数的变化?
library("MASS")
data(birthwt)
data=birthwt
n.folds=10
jim = function(x,y,n.folds,data){
for(i in 1:n.folds){
folds <- cut(seq(1,nrow(data)),breaks=n.folds,labels=FALSE)
testIndexes <- which(folds==i,arr.ind=TRUE)
testData <- data[testIndexes, ]
trainData <- data[-testIndexes, ]
glm.train <- glm(y ~ x, family = binomial, data=trainData)
predictions=predict(glm.train, newdata =testData, type='response')
pred.class=ifelse(predictions< 0, 0, 1)
}
rate=sum(pred.class!= y) / length(y)
print(head(rate))
}
jim(birthwt$smoke, birthwt$low, 10, birthwt)
【问题讨论】:
-
谢谢你 - 预测应该是 (
-
我希望 pred.class 作为一个向量,其中包含每个折叠的所有预测。在这个函数中,我只是得到了 19,它应该是 189。然后我使用这个长度为 189 的向量生成速率。
标签: r regression glm cross-validation predict