Perform the following steps to measure the performance of the regression model:
- Load the Quartet dataset from the car package:
> library(car) > data(Quartet)
- 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
- You can retrieve predicted values by using the predict function:
> predicted= predict(lmfit, newdata=Quartet[c("x")])
- Now, you can calculate the root mean square error:
> actual = Quartet$y3 > rmse = (mean((predicted - actual)^2))^0.5 > rmse Output [1] 1.118286
- You can calculate the relative square error:
> mu = mean(actual) > rse = mean((predicted - actual)^2) / mean((mu - actual)^2) > rse Output [1] 0.333676
- Also, you can use R-Square as a measurement:
> rsquare = 1 - rse > rsquare Output [1] 0.666324
- 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
- You can then retrieve the predicted value using the predict function:
> predicted = predict(rlmfit, newdata=Quartet[c("x")])
- 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
- 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
- Now, you can calculate the R-Square value:
> rsquare = 1 - rse > rsquare Output [1] 0.5634933