Fitting a robust linear regression model with rlm
An outlier in the dataset will move the regression line away from the mainstream. Apart from removing it, we can apply a robust linear regression to fit datasets containing outliers. In this recipe, we will introduce how to apply rlm
to perform robust linear regression to datasets containing outliers.
Getting ready
Prepare the dataset that contains an outlier that may move the regression line away from the mainstream. Here, we use the Quartet
dataset loaded from the previous recipe.
How to do it...
Perform the following steps to fit the robust linear regression model with rlm
:
- Generate a scatter plot of the
x
variable againsty3
:
> plot(Quartet$x, Quartet$y3)
Scatter plot of variables x and y3
- Next, you should import the
MASS
library first. Then, you can apply therlm
function to fit the model, and visualize the fitted line with theabline
function:
> library(MASS) > lmfit = rlm(Quartet$y3~Quartet$x) >...