Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform random forest/cross validation in R

Tags:

I'm unable to find a way of performing cross validation on a regression random forest model that I'm trying to produce.

So I have a dataset containing 1664 explanatory variables (different chemical properties), with one response variable (retention time). I'm trying to produce a regression random forest model in order to be able to predict the chemical properties of something given its retention time.

ID  RT (seconds)    1_MW    2_AMW   3_Sv    4_Se 4281    38  145.29  5.01    14.76   28.37 4952    40  132.19  6.29    11  21.28 4823    41  176.21  7.34    12.9    24.92 3840    41  174.24  6.7 13.99   26.48 3665    42  240.34  9.24    15.2    27.08 3591    42  161.23  6.2 13.71   26.27 3659    42  146.22  6.09    12.6    24.16 

This is an example of the table that I have. I want to basically plot RT against 1_MW, etc (up to 1664 variables), so I can find which of these variables are of importance and which aren't.

I do:-

r = randomForest(RT..seconds.~., data = cadets, importance =TRUE, do.trace = 100) varImpPlot(r) 

which tells me which variables are of importance and what not, which is great. However, I want to be able to partition my dataset so that I can perform cross validation on it. I found an online tutorial that explained how to do it, but for a classification model rather than regression.

I understand you do:-

k = 10 n = floor(nrow(cadets)/k) i = 1 s1 = ((i-1) * n+1) s2 = (i * n) subset = s1:s2 

to define how many cross folds you want to do, and the size of each fold, and to set the starting and end value of the subset. However, I don't know what to do here on after. I was told to loop through but I honestly have no idea how to do this. Nor do I know how to then plot the validation set and the test set onto the same graph to depict the level of accuracy/error.

If you could please help me with this I'd be ever so grateful, thanks!

like image 858
user2062207 Avatar asked Nov 04 '13 01:11

user2062207


People also ask

Does random forest need cross-validation?

In random forests, there is no need for cross-validation or a separate test set to get an unbiased estimate of the test set error. It is estimated internally, during the run, as follows: Each tree is constructed using a different bootstrap sample from the original data.

What package is randomForest in R?

The R package "randomForest" is used to create random forests.


2 Answers

From the source:

The out-of-bag (oob) error estimate

In random forests, there is no need for cross-validation or a separate test set to get an unbiased estimate of the test set error. It is estimated internally , during the run...

In particular, predict.randomForest returns the out-of-bag prediction if newdata is not given.

like image 128
topchef Avatar answered Sep 19 '22 08:09

topchef


As topchef pointed out, cross-validation isn't necessary as a guard against over-fitting. This is a nice feature of the random forest algorithm.

It sounds like your goal is feature selection, cross-validation is still useful for this purpose. Take a look at the rfcv() function within the randomForest package. Documentation specifies input of a data frame & vector, so I'll start by creating those with your data.

set.seed(42) x <- cadets x$RT..seconds. <- NULL y <- cadets$RT..seconds.  rf.cv <- rfcv(x, y, cv.fold=10)  with(rf.cv, plot(n.var, error.cv)) 
like image 29
Lenwood Avatar answered Sep 21 '22 08:09

Lenwood