문제

I'm new to R and have been trying to use the following code (thanks to stackoverflow) for cross validating MARS regression. I'm getting error when I execute the code.

In addition to the above question, is there a way to print all the results from the cross validation?

I would appreciate if anyone could help.

library(earth)
library(pls)

set.seed(1)

k <- 10;
result <- 0;
folds <- cvsegments(nrow(trees), k);

for (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 exactly one prediction per case (row) in each run, you can easily collect the predictions in a vector (or matrix, for 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))

for (fold in 1 : k){
  currentFold <- folds[[fold]]
  fit = earth(Volume ~ ., data=trees[-currentFold,])
  result [currentFold] <- predict(fit, trees[currentFold,]);
}

You can then look at the results at your leisure:

> plot (trees$Volume, result)  

cv-results calibration plot

> 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

다른 팁

A way to collect the result of each iteration of fold is to use a list:

library(earth)
library(pls)

set.seed(1)

k <- 10;
resulti <- 0
result <- vector("list", k);
folds <- cvsegments(nrow(trees), k);

for (fold in 1 : k){
  currentFold <- folds[fold][[1]];
  fit = earth(Volume ~ ., data=trees[-currentFold,])
  pred = predict(fit, trees[currentFold,]);
  result[[fold]] <- resulti + table(true=trees[currentFold,3], pred=pred)
}

This corrects the error, though I'm not sure if this produces what you intend.

Edit to obtain the table as requested, we can use melt in reshape2:

For the first result:

require(reshape2)

df.1 <- melt(result[[1]])
df.1[df.1$value == 1, ]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top