LoslegenKostenlos loslegen

In-sample RMSE for linear regression on diamonds

As you saw in the video, included in the course is the diamonds dataset, which is a classic dataset from the ggplot2 package. The dataset contains physical attributes of diamonds as well as the price they sold for. One interesting modeling challenge is predicting diamond price based on their attributes using something like a linear regression.

Recall that to fit a linear regression, you use the lm() function in the following format:

mod <- lm(y ~ x, my_data)

To make predictions using mod on the original data, you call the predict() function:

pred <- predict(mod, my_data)

Diese Übung ist Teil des Kurses

Machine Learning with caret in R

Kurs anzeigen

Anleitung zur Übung

  • Fit a linear model on the diamonds dataset predicting price using all other variables as predictors (i.e. price ~ .). Save the result to model.
  • Make predictions using model on the full original dataset and save the result to p.
  • Compute errors using the formula \(errors = predicted - actual\). Save the result to error.
  • Compute RMSE using the formula you learned in the video and print it to the console.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

# Fit lm model: model


# Predict on full data: p


# Compute errors: error


# Calculate RMSE
Code bearbeiten und ausführen