How to do it...

Perform the following steps to measure the performance of the regression model:

  1. Load the Quartet dataset from the car package:
        > library(car)
        > data(Quartet)
  1. Plot the attribute, y3, against x using the lm function:
        > plot(Quartet$x, Quartet$y3)
        > lmfit = lm(Quartet$y3~Quartet$x)
        > abline(lmfit, col="red")  
The linear regression plot
  1. You can retrieve predicted values by using the predict function:
        > predicted= predict(lmfit, newdata=Quartet[c("x")])  
  1. Now, you can calculate the root mean square error:
        > actual = Quartet$y3
        > rmse = (mean((predicted - actual)^2))^0.5
        > rmse
        Output
        [1] 1.118286  
  1. You can calculate the relative square error:
        > mu = mean(actual)
        > rse = mean((predicted - actual)^2) / mean((mu - actual)^2) 
        > rse
        Output
        [1] 0.333676
  1. Also, you can use R-Square as a measurement:
        > rsquare = 1 - rse
        > rsquare
        Output
        [1] 0.666324  
  1. Then, you can plot attribute, y3, against x using the rlm function from the
    MASS package:
        > library(MASS)
        > plot(Quartet$x, Quartet$y3)
        > rlmfit = rlm(Quartet$y3~Quartet$x)
        > abline(rlmfit, col="red")  
The robust linear regression plot on the Quartet dataset
  1. You can then retrieve the predicted value using the predict function:
        > predicted = predict(rlmfit, newdata=Quartet[c("x")]) 
  1. Next, you can calculate the root mean square error using the distance of the predicted and actual value:
        > actual = Quartet$y3
        > rmse = (mean((predicted - actual)^2))^0.5
        > rmse
        Output
        [1] 1.279045
  1. Calculate the relative square error between the predicted and actual labels:
        > mu = mean(actual)
        > rse =mean((predicted - actual)^2) / mean((mu - actual)^2) 
        > rse
        Output
        [1] 0.4365067
  1. Now, you can calculate the R-Square value:
        > rsquare = 1 - rse
        > rsquare
        Output
        [1] 0.5634933  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset