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’