cross validation using pls function in R -
i'm new r , have been trying use following code (thanks stackoverflow) cross validating mars regression. i'm getting error when execute code.
in addition above question, there way print results cross validation?
i appreciate if help.
library(earth) library(pls) set.seed(1) k <- 10; result <- 0; folds <- cvsegments(nrow(trees), k); (fold in 1 : k){ currentfold <- folds[fold][[1]]; fit = earth(volume ~ ., data=trees[-currentfold,]) pred = predict(fit, trees[currentfold,]); result <- result + table(true=trees[currentfold,3], pred=pred) }
because k-fold cross validation gets 1 prediction per case (row) in each run, can collect predictions in vector (or matrix, more iterations/repetitions and/or multiple predicted values per case):
library(earth) library(pls) set.seed(1) k <- 10; folds <- cvsegments(nrow(trees), k); result <- rep (na, nrow (trees)) (fold in 1 : k){ currentfold <- folds[[fold]] fit = earth(volume ~ ., data=trees[-currentfold,]) result [currentfold] <- predict(fit, trees[currentfold,]); }
you can @ results @ leisure:
> plot (trees$volume, result)
> head (cbind (trees, pred.vol = result)) girth height volume pred.vol 1 8.3 70 10.3 9.701729 2 8.6 65 10.3 10.627089 3 8.8 63 10.2 10.737521 4 10.5 72 16.4 16.313330 5 10.7 81 18.8 21.297516 6 10.8 83 19.7 22.408600
Comments
Post a Comment