Ich habe ein Problem, die Objekttypen zu steuern, die in die Vorhersagefunktion einspeisen. Hier ist meine vereinfachte Funktion, die das Objekt glm
generiert.glm `predict()` Fehler: keine anwendbare Methode für 'predict' angewendet auf ein Objekt der Klasse "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"
Wie greife ich auf die Spalte in fits[,1]
, so dass ich die Funktion vorhersagen kann gleiches Ergebnis zu erhalten, die ich mit myFit
tat?
Versuchen 'fits <- lapply (1: 2, fitOneSample, Daten, Sampleset)', dann 'probability2 < - vorhersagen (passt [[1]], newdata = Daten) '. – cryo111
Danke @ Cryo111. Das hat perfekt funktioniert. –