J'ai un problème pour contrôler les types d'objets alimentant la fonction de prédiction. Voici ma fonction simplifiée qui génère l'objet glm
.glm `predict()` erreur: aucune méthode applicable pour 'predict' appliquée à un objet de la classe "list"
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"
Comment accéder à la colonne fits[,1]
pour que je puisse utiliser la fonction de prédire pour obtenir même résultat que je l'ai fait avec myFit
?
Essayez 'fits <- lapply (1: 2, fitOneSample, données, sampleSet)', puis 'probability2 < - predict (correspond à [[1]], newdata = data) '. – cryo111
Merci @ cryo111. Cela a fonctionné parfaitement. –