예측 함수에 입력되는 객체 유형을 제어하는 데 문제가 있습니다. 여기에 glm
객체를 생성 내 단순화 된 기능입니다.glm`predict()`오류 : "list"클래스의 객체에 적용 할 수있는 'predict'의 적용 가능한 메소드가 없습니다.
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"
어떻게 내가 myFit
와 함께했던 동일한 결과를 얻을 수있는 예측 기능을 사용할 수 있도록 fits[,1]
에 열을 액세스합니까?
하여 코드를 수정할 수 있습니다 맞는 <'시도 - lapply (1 : 2, fitOneSample, 데이터, sampleSet)'다음'probability2을 < - 예측 (맞는 [[1]], newdata = 데이터)'. cryo111 @ – cryo111
감사합니다. 그것은 완벽하게 작동했습니다. –