Více než 2 vysvětlující proměnné

Intermediate Regression in R

Richie Cotton

Data Evangelist at DataCamp

Z minulé lekce

ggplot(
  fish,
  aes(length_cm, height_cm, color = mass_g)
) +
  geom_point() +
  scale_color_viridis_c(option = "inferno")

Bodový graf délky, výšky a hmotnosti ryb s paletou inferno

Intermediate Regression in R

Rozdělení podle druhu

ggplot(
  fish,
  aes(length_cm, height_cm, color = mass_g)
) +
  geom_point() +
  scale_color_viridis_c(option = "inferno") +
  facet_wrap(vars(species))

Bodový graf délky, výšky a hmotnosti ryb rozdělený podle druhu

Intermediate Regression in R

Různé úrovně interakcí

Bez interakcí

lm(mass_g ~ length_cm + height_cm + species + 0, data = fish)

Interakce 2. řádu mezi dvojicemi proměnných

lm(
  mass_g ~ length_cm + height_cm + species + length_cm:height_cm + length_cm:species + height_cm:species + 0, 
  data = fish
)

Interakce 3. řádu mezi všemi třemi proměnnými

lm(
  mass_g ~ length_cm + height_cm + species + length_cm:height_cm + length_cm:species + height_cm:species + length_cm:height_cm:species + 0, 
  data = fish
)
Intermediate Regression in R

Všechny interakce

lm(
  mass_g ~ length_cm + height_cm + species + length_cm:height_cm + length_cm:species + height_cm:species + length_cm:height_cm:species + 0, 
  data = fish
)
lm(
  mass_g ~ length_cm * height_cm * species + 0, 
  data = fish
)
Intermediate Regression in R

Pouze interakce 2. řádu

lm(
  mass_g ~ length_cm + height_cm + species + length_cm:height_cm + length_cm:species + height_cm:species + 0, 
  data = fish
)
lm(
  mass_g ~ (length_cm + height_cm + species) ^ 2 + 0, 
  data = fish
)
lm(
  mass_g ~ I(length_cm) ^ 2 + height_cm + species + 0, 
  data = fish
)
1 Mocnění vysvětlujících proměnných viz „Introduction to Regression in R", kapitola 2, „Transforming variables"
Intermediate Regression in R

Postup predikce

mdl_mass_vs_all <- lm(mass_g ~ length_cm * height_cm * species * 0, data = fish)

explanatory_data <- expand_grid(
  length_cm = seq(5, 60, 6),
  height_cm = seq(2, 20, 2),
  species = unique(fish$species)
)

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

Vizualizace predikcí

ggplot(
  fish,
  aes(length_cm, height_cm, color = mass_g)
) +
  geom_point() +
  scale_color_viridis_c(option = "inferno") +
  facet_wrap(vars(species)) +
  geom_point(
    data = prediction_data, 
    size = 3, shape = 15
  )

Bodový graf délky, výšky, hmotnosti a druhu ryb s predikcemi

Intermediate Regression in R

Let's practice!

Intermediate Regression in R

Preparing Video For Download...