【发布时间】:2017-01-10 23:39:22
【问题描述】:
我在控制输入预测函数的对象类型时遇到问题。这是生成glm 对象的简化函数。
fitOneSample <- function(x,data,sampleSet)
{
#how big of a set are we going to analyze? Pick a number between 5,000 & 30,000, then select that many rows to study
sampleIndices <- 1:5000
#now randomly pick which columns to study
colIndices <- 1:10
xnames <- paste(names(data[,colIndices]),sep = "")
formula <- as.formula(paste("target ~ ", paste(xnames,collapse = "+")))
glm(formula,family=binomial(link=logit),data[sampleIndices,])
}
myFit <- fitOneSample(1,data,sampleSet)
fits <- sapply(1:2,fitOneSample,data,sampleSet)
all.equal(myFit,fits[,1]) #different object types
#this works
probability <- predict(myFit,newdata = data)
#this doesn't
probability2 <- predict(fits[,1],newdata = data)
# Error in UseMethod("predict") :
# no applicable method for 'predict' applied to an object of class "list"
如何访问 fits[,1] 中的列,以便使用 predict 函数获得与 myFit 相同的结果?
【问题讨论】:
-
尝试
fits <- lapply(1:2,fitOneSample,data,sampleSet),然后尝试probability2 <- predict(fits[[1]],newdata = data)。 -
谢谢@cryo111。效果很好。
标签: r regression glm predict