Manually engineering a feature
After doing some research with your team, you recall that the gravitational force of attraction between two bodies obeys Newton's formula:
$$F = G\frac{m_1m_2}{r^2}$$.
You can't use the formula directly because the masses are unknown, but you can fit a regression model of force
as a function of inv_square_distance
. The augmented dataset df
you built in the previous exercise has been loaded for you.
This exercise is part of the course
Feature Engineering in R
Exercise instructions
- Create a new variable
inv_square_distance
defined as the reciprocal of the squared distance and add it to thedf
data frame. - Build a simple regression model using
lm()
offorce
versusinv_square_distance
and save it aslr_force_2
. - Bind your predictions to
df_inverse
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a new variable inv_square_distance
df_inverse <- df %>% ___(inv_square_distance = 1/distance^2)
# Build a simple regression model
lr_force_2 <- lm(force ~ ___, data = df_inverse)
# Bind your predictions to df_inverse
df_inverse <- df_inverse %>% ___(lr2_pred = predict(lr_force_2))
df_inverse %>% ggplot(aes(x = distance, y = force)) +
geom_point() +
geom_line(aes(y = lr2_pred), col = "blue", lwd = .75) +
ggtitle("Linear regression of force vs. inv_square_distance") +
theme_classic()