Aan de slagGa gratis aan de slag

A tentative model

You are handed a data set with measures of the gravitational force between two bodies at different distances and are challenged to build a simple model to predict such force given a specific distance. Initially, you want to stick to simple linear regression. The data consist of 120 pairs of distance and force, and is loaded for you as newton.

Deze oefening maakt deel uit van de cursus

Feature Engineering in R

Cursus bekijken

Oefeninstructies

  • Build a linear model for the newton data using the linear model from base R function and assign it to lr_force.
  • Create a new data frame df by binding the prediction values to the original newton data.
  • Generate a scatterplot of force versus distance using ggplot().
  • Add a regression line to the scatterplot with the fitted values.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Build a linear model for the newton the data and assign it to lr_force
lr_force <- ___(force ~ distance, data = ___)

# Create a new data frame by binding the prediction values to the original data
df <- newton %>% ___(lr_pred = predict(lr_force))

# Generate a scatterplot of force vs. distance
df %>%
  ggplot(aes(x = distance, y = force)) +
  geom____() +
# Add a regression line with the fitted values
  geom_line(aes(y = ___), color = "blue", lwd = .75) +
  ggtitle("Linear regression of force vs. distance") +
  theme_classic()
Code bewerken en uitvoeren