如何找到R中的均方根误差?

时间:2018-12-16 22:01:16

标签: r plot

我正在尝试在ksvm模型上找到均方根误差,并使用airquality数据框绘制结果。这是我到目前为止的内容:

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)

1 个答案:

答案 0 :(得分:1)

该TestAQ数据框中有一堆NA,所以我先将其删除:

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

然后,计算均方根误差是一个非常简单的解析问题,该术语具有非常描述的术语:

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

ggplot调用没有意义,因为svmOutputtest对象是S4,因此无法访问,并且它没有Error插槽,因此无法简单地将@替换为$修复语法错误。该库的拼写为ggplot2。 SO弃用了多部分的问题,因此我将不试图澄清该问题。