SLG333
SLG333

Reputation: 79

How do I find the root mean squared error in R?

I'm trying to find the root mean squared error on a ksvm model and also plotting the results using the airquality dataframe. This is what I have so far:

library(kernlab)
library(ggplot2)
AQ <- airquality

set.seed(1)
randIndex <- sample(1:dim(AQ)[1])
cutPoint2_3<- floor(2 * dim(AQ)[1]/3)
cutPoint2_3
TrainAQ <- AQ[randIndex[1:cutPoint2_3],]
TestAQ <- AQ[randIndex[(cutPoint2_3+1) :dim(AQ)[1]],]

svmOutput <- ksvm(Ozone ~., data=TrainAQ, kernel = "rbfdot", 
kpar='automatic',C=5,cross=3, prob.model=TRUE)

#Test the model on the testing dataset, and compute the Root Mean Squared    Error
svmOutputtest <- ksvm(Ozone ~., data=TestAQ, 
                      kernel = "rbfdot",
                      kpar="automatic",
                      C=5,
                      cross=3,
                      prob.model=TRUE)

#root mean squared is ?

#Plot the   results. Use a scatter  plot. Have the  x-axis  represent    temperature, the   y-axis  represent   wind,   the point   size    and color    represent  the error,  as  defined by  the actual  ozone   level minus the  predicted  ozone   level).
ggplot(AQ,aes(x=Temp,y= Wind,color=svmOutput$Error,shape=svmOutput$Error)) +geom_point(size=5)

Upvotes: 2

Views: 1823

Answers (1)

IRTFM
IRTFM

Reputation: 263342

There's a bunch of NA's in that TestAQ dataframe so I would remove them first:

  TestAQ <- TestAQ[complete.cases(TestAQ), ]

Then calculating the Root Mean Squared Error is a simple matter of parsing that verys descripted terminology:

 sqrt( mean( TestAQ$Ozone-predict(svmOutputtest,newdata=TestAQ ))^2) 
[1] 2.182599

The ggplot call makes no sense since that svmOutputtest object is S4 so cannot be accessed and it has no Error slot so simple substitution of @ for $ will not fix the syntax error. The library is spelled ggplot2. Multi-part questions are deprecated on SO, so I'm not going to try to get clarification on that issue.

Upvotes: 1

Related Questions