R 的迴歸分析中級
Richie Cotton
Data Evangelist at DataCamp
本課程預設你已修過[Introduction to Regression in R](https://learn.datacamp.com/courses/introduction-to-regression-in-r)。
「多元迴歸」是含有超過 1 個解釋變數的迴歸模型。
更多解釋變數可帶來「更多洞見」與「更佳預測」。
| mass_g | length_cm | species |
|---|---|---|
| 242.0 | 23.2 | Bream |
| 5.9 | 7.5 | Perch |
| 200.0 | 30.0 | Pike |
| 40.0 | 12.9 | Roach |
mass_g 為反應變數mdl_mass_vs_length <- lm(mass_g ~ length_cm, data = fish)
Call:
lm(formula = mass_g ~ length_cm, data = fish)
Coefficients:
(Intercept) length_cm
-536.2 34.9
mdl_mass_vs_species <- lm(mass_g ~ species + 0, data = fish)
Call:
lm(formula = mass_g ~ species + 0, data = fish)
Coefficients:
speciesBream speciesPerch speciesPike speciesRoach
617.8 382.2 718.7 152.0
mdl_mass_vs_both <- lm(mass_g ~ length_cm + species + 0, data = fish)
Call:
lm(formula = mass_g ~ length_cm + species + 0, data = fish)
Coefficients:
length_cm speciesBream speciesPerch speciesPike speciesRoach
42.57 -672.24 -713.29 -1089.46 -726.78
coefficients(mdl_mass_vs_length)
(Intercept) length_cm
-536.2 34.9
coefficients(mdl_mass_vs_species)
speciesBream speciesPerch speciesPike speciesRoach
617.8 382.2 718.7 152.0
coefficients(mdl_mass_vs_both)
length_cm speciesBream speciesPerch speciesPike speciesRoach
42.57 -672.24 -713.29 -1089.46 -726.78
library(ggplot2)
ggplot(fish, aes(length_cm, mass_g)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE)

ggplot(fish, aes(species, mass_g)) +
geom_boxplot() +
stat_summary(fun.y = mean, shape = 15)

library(moderndive)
ggplot(fish, aes(length_cm, mass_g, color = species)) +
geom_point() +
geom_parallel_slopes(se = FALSE)

R 的迴歸分析中級