Making predictions with interactions

Intermediate Regression in R

Richie Cotton

Data Evangelist at DataCamp

The model with the interaction

mdl_mass_vs_both_inter <- lm(mass_g ~ species + species:length_cm + 0, data = fish)
Call:
lm(formula = mass_g ~ species + species:length_cm + 0, data = fish)

Coefficients:
          speciesBream            speciesPerch             speciesPike            speciesRoach  
              -1035.35                 -619.18                -1540.82                 -329.38  
speciesBream:length_cm  speciesPerch:length_cm   speciesPike:length_cm  speciesRoach:length_cm  
                 54.55                   38.91                   53.19                   23.32
Intermediate Regression in R

The prediction flow, again

library(dplyr)
library(tidyr)
explanatory_data <- expand_grid(
  length_cm = seq(5, 60, 5),
  species = unique(fish$species)
)

prediction_data <- explanatory_data %>% 
  mutate(mass_g = predict(mdl_mass_vs_both_inter, explanatory_data))
Intermediate Regression in R

Visualizing the predictions

ggplot(fish, aes(length_cm, mass_g, color = species)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  geom_point(data = prediction_data, size = 3, shape = 15)

scatter-fish-mass-vs-length-by-species-inter-with-preds.png

Intermediate Regression in R

Manually calculating the predictions

coeffs <- coefficients(mdl_mass_vs_both_inter)
          speciesBream           speciesPerch            speciesPike           speciesRoach 
           -1035.34757             -619.17511            -1540.82427             -329.37621 
speciesBream:length_cm speciesPerch:length_cm  speciesPike:length_cm speciesRoach:length_cm 
              54.54998               38.91147               53.19487               23.31926 
intercept_bream <- coeffs[1]
intercept_perch <- coeffs[2]
intercept_pike <- coeffs[3]
intercept_roach <- coeffs[4]
slope_bream <- coeffs[5]
slope_perch <- coeffs[6]
slope_pike <- coeffs[7]
slope_roach <- coeffs[8]
Intermediate Regression in R

Manually calculating the predictions

explanatory_data %>% 
  mutate(
    mass_g = case_when(



    )
  )
Intermediate Regression in R

Manually calculating the predictions

explanatory_data %>% 
  mutate(
    mass_g = case_when(
      species == "Bream" ~ 



    )
  )
Intermediate Regression in R

Manually calculating the predictions

explanatory_data %>% 
  mutate(
    mass_g = case_when(
      species == "Bream" ~ intercept_bream + slope_bream * length_cm



    )
  )
Intermediate Regression in R

Manually calculating the predictions

explanatory_data %>% 
  mutate(
    mass_g = case_when(
      species == "Bream" ~ intercept_bream + slope_bream * length_cm,
      species == "Perch" ~ intercept_perch + slope_perch * length_cm,
      species == "Pike" ~ intercept_pike + slope_pike * length_cm,
      species == "Roach" ~ intercept_roach + slope_roach * length_cm
    )
  )
Intermediate Regression in R

Let's practice!

Intermediate Regression in R

Preparing Video For Download...