설명 변수가 2개 이상일 때

R 중급 회귀

Richie Cotton

Data Evangelist at DataCamp

지난 시간 복습

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

길이·높이·질량 산점도(inferno 팔레트).png

R 중급 회귀

종별 패싯

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

종별 패싯: 길이·높이·질량 산점도.png

R 중급 회귀

상호작용 수준 비교

상호작용 없음

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

변수 쌍 간 2차 상호작용

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

세 변수 간 3차 상호작용

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
)
R 중급 회귀

모든 상호작용 포함

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
)
R 중급 회귀

2차 상호작용만 포함

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 설명 변수를 제곱하는 방법은 "Introduction to Regression in R" 2장 "Transforming variables"를 참조하세요.
R 중급 회귀

예측 흐름

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))
R 중급 회귀

예측 시각화

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
  )

길이·높이·질량·종 산점도(예측 포함).png

R 중급 회귀

연습해 봅시다!

R 중급 회귀

Preparing Video For Download...