Created
December 14, 2016 17:59
-
-
Save gDanix/529301142003dfcb34c16951a1d524c6 to your computer and use it in GitHub Desktop.
Código en R problemático con la definición de 'train'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library (randomForest) | |
acierto <- function(bag.datos){ | |
return (sum (sapply(1:length(bag.datos$y), function(x){ | |
if (is.na(bag.datos$predicted[x])){ | |
0 | |
} | |
else if (as.numeric(bag.datos$y[x])==as.numeric(bag.datos$predicted[x])){ | |
1 | |
} | |
else{ | |
0 | |
} | |
}))/length(bag.datos$y)) | |
} | |
Graphical_RF <- function(datos, formula, num_trees){ | |
compute1Prediction <- function(n, data, formula) { | |
train <- sample (1:nrow(data), as.integer(nrow(data)*0.9)) | |
test <- data [-train ,] | |
# Ahora mismo, randomForest() da error: objeto "train" no encontrado | |
# Si se descomenta la siguiente línea (que declara train como una variable global), entonces todo funciona | |
#assign("train", sample (1:nrow(data), as.integer(nrow(data)*0.9)), envir = .GlobalEnv) | |
model <- randomForest(formula, data = data, subset = train, ntree =n) | |
pred <- predict (model ,newdata =test) | |
acierto(as.data.frame(cbind(predicted = pred, y=test[,ncol(test)]))) | |
} | |
plot(1:num_trees, sapply(1:num_trees, compute1Prediction, datos, formula), type = "b", xlab = "Número de árboles", ylab = "Precisión") | |
} | |
Graphical_RF(iris,Species~.,100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment