Modele mnl

Author

v4_24mai

tinytex::install_tinytex(force = TRUE)
tlmgr --repository http://www.preining.info/tlgpg/ install tlgpg
tlmgr option repository "https://ctan.mines-albi.fr/systems/texlive/tlnet"
tlmgr update --list
tinytex::is_tinytex()
[1] TRUE
library(mlogit)
Warning: le package 'mlogit' a été compilé avec la version R 4.3.3
Le chargement a nécessité le package : dfidx
Warning: le package 'dfidx' a été compilé avec la version R 4.3.3

Attachement du package : 'dfidx'
L'objet suivant est masqué depuis 'package:stats':

    filter
library(dplyr)
Warning: le package 'dplyr' a été compilé avec la version R 4.3.3

Attachement du package : 'dplyr'
Les objets suivants sont masqués depuis 'package:stats':

    filter, lag
Les objets suivants sont masqués depuis 'package:base':

    intersect, setdiff, setequal, union
library(tidyr)
 
# Charger les données
data("Yogurt", package = "Ecdat")
df <- Yogurt
 
# Modifier les noms des colonnes pour simplifier et éviter les duplications
names(df) <- gsub("^feat\\.", "feat_", names(df))
names(df) <- gsub("^price\\.", "price_", names(df))
 
# Réorganiser les données pour mlogit
df_long <- df %>%
  pivot_longer(cols = starts_with("price_"), names_to = "Brand", values_to = "Price") %>%
  mutate(Brand = gsub("price_", "", Brand)) %>%
  pivot_longer(cols = starts_with("feat_"), names_to = "Feat_Brand", values_to = "Feat") %>%
  mutate(Feat_Brand = gsub("feat_", "", Feat_Brand)) %>%
  filter(Brand == Feat_Brand) %>%
  select(-Feat_Brand) %>%
  arrange(id) %>%
  mutate(Price = Price / 100)  # Diviser la variable Price par 100
 
# Ajouter une variable de choix binaire pour chaque marque
df_long <- df_long %>%
  mutate(choice = ifelse(choice == Brand, 1, 0))
 
 
df_mlogit <- mlogit.data(df_long, choice = "choice", shape = "long", alt.var = "Brand", id.var = "id", reflevel = "hiland")
 
model <- mlogit(choice ~ Price + Feat, data = df_mlogit)
 
summary(model)

Call:
mlogit(formula = choice ~ Price + Feat, data = df_mlogit, method = "nr")

Frequencies of alternatives:choice
  dannon   hiland   weight  yoplait 
0.402156 0.029436 0.229270 0.339138 

nr method
6 iterations, 0h:0m:0s 
g'(-H)^-1g = 0.000763 
successive function values within tolerance limits 

Coefficients :
                      Estimate Std. Error  z-value  Pr(>|z|)    
(Intercept):hiland   -3.715595   0.145419 -25.5510 < 2.2e-16 ***
(Intercept):weight   -0.641184   0.054498 -11.7652 < 2.2e-16 ***
(Intercept):yoplait   0.734571   0.080644   9.1088 < 2.2e-16 ***
Price               -36.658445   2.436607 -15.0449 < 2.2e-16 ***
Feat                  0.491433   0.120063   4.0931 4.256e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Log-Likelihood: -2656.9
McFadden R^2:  0.062142 
Likelihood ratio test : chisq = 352.09 (p.value = < 2.22e-16)
# Installer les packages nécessaires

# Charger les packages
library(tidyverse)
Warning: le package 'tidyverse' a été compilé avec la version R 4.3.3
Warning: le package 'ggplot2' a été compilé avec la version R 4.3.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.0     ✔ readr     2.1.5
✔ ggplot2   3.5.1     ✔ stringr   1.5.1
✔ lubridate 1.9.3     ✔ tibble    3.2.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks dfidx::filter(), stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(mlogit)
library(knitr)

# Charger les données
data("Yogurt", package = "Ecdat")
df <- Yogurt

# Modifier les noms des colonnes pour simplifier et éviter les duplications
names(df) <- gsub("^feat\\.", "feat_", names(df))
names(df) <- gsub("^price\\.", "price_", names(df))

# Réorganiser les données pour mlogit
df_long <- df %>%
  pivot_longer(cols = starts_with("price_"), names_to = "Brand", values_to = "Price") %>%
  mutate(Brand = gsub("price_", "", Brand)) %>%
  pivot_longer(cols = starts_with("feat_"), names_to = "Feat_Brand", values_to = "Feat") %>%
  mutate(Feat_Brand = gsub("feat_", "", Feat_Brand)) %>%
  filter(Brand == Feat_Brand) %>%
  select(-Feat_Brand) %>%
  arrange(id) %>%
  mutate(Price = Price / 100)  # Diviser la variable Price par 100

# Ajouter une variable de choix binaire pour chaque marque
df_long <- df_long %>%
  mutate(choice = ifelse(choice == Brand, 1, 0))

# Créer un objet mlogit
df_mlogit <- mlogit.data(df_long, choice = "choice", shape = "long", alt.var = "Brand", id.var = "id", reflevel = "hiland")

# Créer le modèle
model <- mlogit(choice ~ Price + Feat, data = df_mlogit)

# Résumé du modèle
model_summary <- summary(model)

# Afficher le résumé dans un tableau
kable(model_summary$CoefTable, "markdown")
Estimate Std. Error z-value Pr(>|z|)
(Intercept):hiland -3.7155950 0.1454190 -25.550959 0.00e+00
(Intercept):weight -0.6411843 0.0544983 -11.765222 0.00e+00
(Intercept):yoplait 0.7345712 0.0806442 9.108791 0.00e+00
Price -36.6584451 2.4366066 -15.044876 0.00e+00
Feat 0.4914334 0.1200630 4.093129 4.26e-05