Bénéfice réalisé dans un magasin selon le nombre d’habitants.
Le coefficient de corrélation (Pearson) entre le bénéfice et la population est de 0.838.
Nombre d’habitants pour lesquels prédire un bénéfice.
(magasins.new <- data.frame(population = c(10, 12.5, 15, 17.5, 20)))
## population
## 1 10.0
## 2 12.5
## 3 15.0
## 4 17.5
## 5 20.0
Calcul du modèle.
\[ profit = \theta_0 + \theta_1 population \]
model <- lm(profit ~ population, data = magasins)
Prédiction.
magasins.new$profit <- predict(model, magasins.new)
plot.magasins()
points(magasins.new, pch = 20, cex = 2, col = rgb(1, 0, 0, .5))
# Courbe du modèle de prédiction
abline(model, col = rgb(1, 0, 0, .5), lw = 3)
Calcul du modèle.
\[ profit = \theta_0 + \theta_1 population + \theta_2 population^2 + \theta_3 population^3 + ... + \theta_n population^n \]
model <- lm(profit ~ poly(population, degree = 5, raw = T), data = magasins)
Prédiction.
plot.magasins()
points(
magasins.new$population,
predict(model, magasins.new),
pch = 20, cex = 2, col = rgb(0, 1, 0, .5)
)
# Courbe du modèle de prédiction
population.test <- seq(min(population), max(population), length.out = 200)
lines(
population.test,
predict(model, data.frame(population = population.test)),
col = rgb(0, 1, 0, .5),
lw = 3
)
Calcul du modèle.
model <- lm(profit ~ poly(population, degree = 20, raw = T), data = magasins)
Prédiction.
plot.magasins()
points(
magasins.new$population,
predict(model, magasins.new),
pch = 20, cex = 2, col = rgb(1, 0, 1, .5)
)
# Courbe du modèle de prédiction
lines(
population.test,
predict(model, data.frame(population = population.test)),
col = rgb(1, 0, 1, .5),
lw = 3
)
Comportements d’achat en fonction de la météo.
## temps temperature vent achete
## 1 soleil froid TRUE TRUE
## 2 soleil chaud TRUE FALSE
## 3 soleil chaud FALSE FALSE
## 4 nuage froid TRUE TRUE
## 5 nuage chaud FALSE TRUE
## 6 nuage froid FALSE TRUE
## 7 pluie froid TRUE FALSE
## 8 pluie froid FALSE TRUE
Calcul du modèle.
model <- glm(achete ~ ., data = achats)
Prédiction.
achats.new <-
data.frame(
temps = c("pluie", "soleil", "soleil"),
temperature = c("froid", "chaud", "froid"),
vent = c(T, F, F)
)
achats.new$achete <- predict(model, newdata = achats.new) > .5
achats.new
## temps temperature vent achete
## 1 pluie froid TRUE FALSE
## 2 soleil chaud FALSE FALSE
## 3 soleil froid FALSE TRUE
Catégories d’iris.
Calcul du modèle.
library(rpart)
model <- rpart(Species ~ ., data = iris, method = "class")
Arbre de décision.
fancyRpartPlot(model, sub = "")
Application du modèle sur les données originales.
plot.iris()
split1 <- 2.5
split2 <- 1.8
lcol <- rgb(0, 0, 0, .5)
abline(v = split1, col = lcol, lw = 3, lt = "dotted")
lines(c(split1, round(max(iris$Petal.Length)) + 1), rep(split2, 2), col = lcol, lw = 3, lt = "dotted")
Calcul du modèle.
model <- kmeans(iris[3:4], 3)
Segmentation originale.
Segmentation du modèle.
plot(
iris$Petal.Length,
iris$Petal.Width,
pch = c(17, 18, 20)[model$cluster],
cex = 2,
col = rgb(.5, .5, .5, .5),
xlab = "Petal Length",
cex.axis = .8,
cex.lab = .8,
ylab = "Petal Width",
main = "iris"
)
points(model$centers, pch = "X", col = "red")
Attention ! L’emplacement initial des centroïds peut avoir un impact sur la segmentation. Il est donc recommandé d’évaluer le modèle à plusieurs reprises.