|
- library(tidyverse)
- library(broom)
-
- data(iris)
-
- # Création du modèle
- fit <- lm(Sepal.Length ~ Petal.Length, data = iris)
-
- # Résultat
- fit
-
- # Résultat détaillé
- fit %>% summary
-
- # Test de corrélation
- cor.test(iris$Sepal.Length, iris$Petal.Length)
-
- # Coefficient de corrélation -> R²
- cor.test(iris$Sepal.Length, iris$Petal.Length)$estimate ^ 2
-
- # Utilisation de broom pour extraire les données du modèle
-
- # Glance -> paramètres du modèle
- fit %>% glance
-
- # Tidy -> résultats pour chaque prédicteur
- fit %>% tidy
-
- # Augment -> détail pour chaque observation
- fit %>% augment
-
- # Diagnostic du modèle
- fit %>% plot
-
- # Prédiction de valeurs
- tibble(Petal.Length = 1:7,
- Sepal.Length = predict(fit, tibble(Petal.Length = Petal.Length))) -> pred
-
- iris %>%
- ggplot() +
- aes(x = Petal.Length, y = Sepal.Length) +
- geom_point() +
- # geom_abline(intercept = fit %>% tidy %>% filter(term == "(Intercept)") %>% pull(estimate),
- # slope = fit %>% tidy %>% filter(term == "Petal.Length") %>% pull(estimate)) +
- geom_point(data = pred, aes(x = Petal.Length, y = Sepal.Length), color = "red")
-
- # Exemple avec gapminder
- library(gapminder)
- data(gapminder)
-
- gapminder %>%
- ggplot() +
- aes(x = year, y = lifeExp, group = country) +
- geom_line()
-
- gapminder %>%
- filter(country == "France") -> france
-
- lm(lifeExp ~ year, data = france) -> fit_fr
-
- fit_fr %>% glance
- fit_fr %>% tidy
-
- france %>%
- ggplot() +
- aes(x = year, y = lifeExp) +
- geom_line()
-
- tibble(year = 2008:2050,
- lifeExp = predict(fit_fr, tibble(year = year))) -> pred
-
- france %>%
- bind_rows(pred) %>%
- ggplot() +
- aes(x = year, y = lifeExp) +
- geom_line()
-
- gapminder %>%
- group_by(continent, country) %>%
- do(rsquared = lm(data = ., lifeExp ~ year) %>% glance %>% pull(r.squared)) %>%
- unnest -> rsq
-
- rsq %>%
- ggplot() +
- aes(x = rsquared) +
- geom_histogram()
-
- rsq %>%
- mutate(country = country %>% fct_reorder(rsquared)) %>%
- ggplot() +
- aes(x = rsquared, y = country, color = continent) +
- geom_point()
|