Intermediate Regression in R
Richie Cotton
Data Evangelist at DataCamp
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
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))
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)
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]
explanatory_data %>%
mutate(
mass_g = case_when(
)
)
explanatory_data %>%
mutate(
mass_g = case_when(
species == "Bream" ~
)
)
explanatory_data %>%
mutate(
mass_g = case_when(
species == "Bream" ~ intercept_bream + slope_bream * length_cm
)
)
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