Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random Forest in R - Applying to testing/validation set

I am a beginner to using Random Forest. I am trying to train a random forest model then apply it to a testing dataset but am having problems getting two datasets that are the same length. I have trained a good model but need to see how it performs on my test data. Please see my code below. Any tips would be appreciated.

#Import Data
url <- "http://groupware.les.inf.puc-rio.br/static/WLE/WearableComputing_weight_lifting_exercises_biceps_curl_variations.csv"
df <- read.csv(url, header = TRUE, na.strings=c("NA","#DIV/0!",""))

#Remove columns containing ALL NA values
df <- df[,colSums(is.na(df)) == 0]

#Remove all irrelevant columns that you will not need as predictors 
df <- subset(df, select = -c(1:7))

#Create training and testing datasets
library(caret)
inTrain <- createDataPartition(y = df$classe,
                               p=0.7, list = FALSE)
training <- df[inTrain,]
testing <- df[-inTrain,]

set.seed(2020)

rfmodel <- randomForest(classe ~ ., data = training, method="rf", ntree=100, importance = TRUE)
print(rfmodel) #Error rate of 0.17% = good!

#validating that this method works on training set
prediction_train <- predict(rfmodel, data = training, type = "class")
table(prediction_train, training$classe)

#Cannot figure out what is going wrong here
prediction_test <- predict(rfmodel, data = testing)
length(prediction_test) #27472
length(testing$classe) #11770
table(prediction_test, testing$classe) #ERROR (see below)
#Error in table(prediction_test, testing$classe) : all arguments must have the same length

Packages I am using:

version$version.string [1] "R version 3.5.3 (2019-03-11)" packageVersion("caret", lib.loc = NULL) [1] ‘6.0.85’ packageVersion("rattle", lib.loc = NULL) [1] ‘5.3.0’ packageVersion("randomForest", lib.loc = NULL) [1] ‘4.6.14’ packageVersion("randomForestExplainer", lib.loc = NULL) [1] ‘0.10.0’

like image 841
Angela C Avatar asked Jan 22 '26 12:01

Angela C


1 Answers

The problem was in the data = when doing the testing. Cheers.

rfmodel <- randomForest(training$classe ~ ., data = training[,-51], method="rf", ntree=100, importance = TRUE)
prediction_test <- predict(rfmodel, testing[,-51])
table(prediction_test, testing$classe) 

prediction_test    A    B    C    D    E
              A 3346    3    0    0    0
              B    1 2274    4    0    0
              C    0    0 2049   15    0
              D    0    0    0 1913    0
              E    0    0    0    1 2164


like image 103
programandoconro Avatar answered Jan 25 '26 04:01

programandoconro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!