Reputation: 380
I tried to predict several models using a previous function, but but I'm getting "Error in eval(expr, envir, enclos) : object 'var.1' not found". That's is weird because var.1 is not in the dataframe. The code is:
library(randomForest)
library(ada)
library(class)
library(e1071)
library(rpart)
library(car)
library(nnet)
library(kknn)
Consenso <- function(DF,VAR.DEP){ #entries are a dataframe and a dependent var
N1 <- sample(DF,dim(DF)[1],replace=TRUE)
N2 <- sample(DF,dim(DF)[1],replace=TRUE)
N3 <- sample(DF,dim(DF)[1],replace=TRUE)
N4 <- sample(DF,dim(DF)[1],replace=TRUE)
N5 <- sample(DF,dim(DF)[1],replace=TRUE)
mod.kknn <- train.kknn(VAR.DEP ~.,data=N1,kmax=trunc(sqrt(dim(DF)[1])))
mod.net <- nnet(VAR.DEP ~ ., data = N2, size = 10, rang = 0.1,
decay = 5e-04, maxit = 400, trace = FALSE, MaxNWts = 20000)
mod.tree <- rpart(VAR.DEP ~ ., data = N3)
mod.sv <- svm(VAR.DEP ~ ., data = N4, kernel = "linear")
mod.rf <-randomForest(VAR.DEP ~.,data= N5,ntree=400)
lista.mod <- list("Modelo.kknn"=mod.kknn,"Modelo.Redes"=mod.net,"Modelo.Arboles"=mod.tree,"Modelo.SV"=mod.sv,"Modelo.RF"=mod.rf)
return(lista.mod)
}
#Using the function
pbiris <- Consenso(iris,iris$Species)
#Make a prediction
predict(pbiris$Modelo.kknn,iris)
#Error in eval(expr, envir, enclos) : object 'Sepal.Width.1' not found
#Var Sepal.Width.1 is not in the dataframe.
What am I doing wrong?
Any suggestions? Thanks.
Upvotes: 3
Views: 7150
Reputation: 52687
Hard to tell for sure given your code is not exactly minimally reproducible, but almost certainly the problem is from:
sample(DF, dim(DF)[1], rep=T)
The problem is you are sampling the columns of the data frame, not the rows. Consider:
DF <- data.frame(a=1:4, b=5:8)
sample(DF, dim(DF)[1], rep=T)
Produces:
b b.1 b.2 a
1 5 5 5 1
2 6 6 6 2
3 7 7 7 3
4 8 8 8 4
Since our data frame has fewer columns than rows, if you try to sample columns as many times as there are rows it will re-sample the same columns several times. In our test we re-sampled column b
three times, so we get b
, b.1
, and b.2
. Of course, your original data frame does not have the extra columns so your predict
call fails.
Remember, data.frame
objects are lists, so you just sampled the elements of the list. To sample rows:
DF[sample(nrow(DF)), ]
Upvotes: 3