Загружаем нужные библиотеки:

library("knitr")

library("dplyr") # стратегия Разделяй - Властвуй - Соединяй
library("reshape2") # melt - cast
library("ggplot2") # графики
library("MCMCpack")
library("haven")
library("bayesm")
library("stringr")
help("rhierMnlRwMixture")

Стащено из http://www.r-bloggers.com/lets-do-some-more-hierarchical-bayes-choice-modeling-in-r/: Оценивается мультиномиальный логит:

Стащено из http://www.r-bloggers.com/lets-do-some-more-hierarchical-bayes-choice-modeling-in-r/:

Читаем файл

df <- read_spss("~/Downloads/conjoint_1.sav")

Посмотрим на данные:

glimpse(df)
## Observations: 296
## Variables: 254
## $ id                    (dbl) 93, 105, 124, 127, 141, 158, 161, 169, 1...
## $ version               (dbl) 40, 72, 97, 32, 99, 62, 97, 30, 75, 31, ...
## $ T1_C1_Minutes         (dbl) 4, 1, 4, 3, 4, 6, 4, 6, 3, 3, 4, 7, 5, 3...
## $ T1_C1_Gigabytes       (dbl) 1, 7, 4, 1, 10, 3, 4, 5, 9, 6, 9, 8, 3, ...
## $ T1_C1_Price           (dbl) 4, 5, 3, 4, 5, 5, 3, 2, 5, 4, 5, 2, 2, 4...
## $ T1_C1_Host_provider   (dbl) 4, 3, 1, 3, 3, 4, 1, 4, 3, 1, 2, 1, 4, 3...
## $ T1_C1_Personalization (dbl) 1, 1, 1, 2, 2, 1, 1, 1, 1, 2, 1, 2, 2, 2...
## $ T1_C1_Payment         (dbl) 2, 2, 1, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 2...
## $ T1_C1_Quantity_sim    (dbl) 2, 2, 1, 2, 2, 3, 1, 3, 2, 1, 3, 1, 2, 3...
## $ T1_C2_Minutes         (dbl) 3, 4, 5, 6, 1, 1, 5, 2, 7, 2, 7, 3, 6, 4...
## $ T1_C2_Gigabytes       (dbl) 5, 2, 3, 8, 3, 9, 3, 6, 2, 9, 8, 7, 2, 7...
## $ T1_C2_Price           (dbl) 5, 2, 5, 2, 3, 4, 5, 5, 3, 5, 4, 3, 5, 3...
## $ T1_C2_Host_provider   (dbl) 3, 3, 4, 2, 2, 2, 4, 1, 1, 4, 3, 2, 2, 1...
## $ T1_C2_Personalization (dbl) 1, 1, 2, 1, 1, 2, 2, 2, 1, 1, 1, 2, 1, 1...
## $ T1_C2_Payment         (dbl) 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 1, 2, 1...
## $ T1_C2_Quantity_sim    (dbl) 1, 3, 2, 2, 3, 2, 2, 2, 3, 2, 2, 2, 2, 2...
## $ T1_C3_Minutes         (dbl) 7, 2, 6, 4, 6, 7, 6, 1, 1, 7, 1, 5, 4, 7...
## $ T1_C3_Gigabytes       (dbl) 2, 9, 6, 9, 6, 8, 6, 9, 7, 10, 4, 9, 5, ...
## $ T1_C3_Price           (dbl) 2, 1, 4, 5, 2, 3, 4, 1, 4, 2, 2, 1, 1, 5...
## $ T1_C3_Host_provider   (dbl) 1, 1, 2, 2, 4, 3, 2, 2, 2, 4, 4, 3, 1, 1...
## $ T1_C3_Personalization (dbl) 2, 1, 1, 1, 2, 2, 1, 2, 1, 2, 2, 1, 2, 1...
## $ T1_C3_Payment         (dbl) 1, 2, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1...
## $ T1_C3_Quantity_sim    (dbl) 3, 1, 1, 3, 1, 1, 1, 1, 3, 1, 1, 2, 3, 3...
## $ T1_C4_Minutes         (dbl) 2, 7, 3, 7, 3, 3, 3, 4, 5, 5, 5, 2, 7, 2...
## $ T1_C4_Gigabytes       (dbl) 8, 5, 7, 6, 8, 7, 7, 10, 4, 8, 10, 3, 8,...
## $ T1_C4_Price           (dbl) 1, 4, 1, 3, 1, 2, 1, 3, 2, 1, 3, 4, 3, 2...
## $ T1_C4_Host_provider   (dbl) 1, 2, 3, 1, 3, 1, 3, 3, 2, 3, 1, 4, 3, 4...
## $ T1_C4_Personalization (dbl) 2, 2, 2, 2, 1, 1, 2, 1, 2, 1, 2, 1, 1, 1...
## $ T1_C4_Payment         (dbl) 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1...
## $ T1_C4_Quantity_sim    (dbl) 3, 2, 2, 1, 2, 2, 2, 1, 2, 2, 3, 3, 1, 1...
## $ T1_C5_Minutes         (dbl) 1, 3, 1, 2, 5, 5, 1, 5, 2, 1, 3, 4, 3, 6...
## $ T1_C5_Gigabytes       (dbl) 3, 8, 1, 3, 2, 1, 1, 1, 6, 5, 2, 5, 10, ...
## $ T1_C5_Price           (dbl) 3, 3, 2, 1, 4, 1, 2, 4, 1, 3, 1, 5, 4, 1...
## $ T1_C5_Host_provider   (dbl) 2, 4, 1, 4, 1, 2, 1, 2, 4, 2, 4, 3, 4, 2...
## $ T1_C5_Personalization (dbl) 2, 2, 1, 1, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2...
## $ T1_C5_Payment         (dbl) 1, 1, 2, 1, 1, 2, 2, 2, 1, 2, 2, 1, 1, 2...
## $ T1_C5_Quantity_sim    (dbl) 2, 3, 3, 1, 3, 3, 3, 3, 1, 3, 1, 3, 3, 2...
## $ T1_select             (dbl) 3, 3, 3, 2, 3, 4, 1, 4, 5, 4, 5, 3, 3, 3...
## $ T2_C1_Minutes         (dbl) 2, 2, 7, 7, 7, 6, 7, 3, 4, 4, 2, 4, 7, 1...
## $ T2_C1_Gigabytes       (dbl) 9, 9, 8, 6, 5, 3, 8, 5, 10, 3, 1, 6, 6, ...
## $ T2_C1_Price           (dbl) 4, 3, 4, 4, 1, 4, 4, 5, 4, 3, 1, 2, 1, 4...
## $ T2_C1_Host_provider   (dbl) 1, 2, 4, 2, 4, 1, 4, 3, 1, 3, 2, 2, 1, 3...
## $ T2_C1_Personalization (dbl) 1, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 1, 1...
## $ T2_C1_Payment         (dbl) 2, 1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1...
## $ T2_C1_Quantity_sim    (dbl) 2, 2, 3, 3, 3, 3, 3, 1, 2, 2, 2, 3, 2, 1...
## $ T2_C2_Minutes         (dbl) 4, 6, 2, 6, 1, 7, 2, 2, 1, 1, 7, 1, 6, 3...
## $ T2_C2_Gigabytes       (dbl) 5, 4, 2, 9, 7, 1, 2, 6, 1, 1, 6, 2, 7, 9...
## $ T2_C2_Price           (dbl) 1, 1, 5, 3, 2, 1, 5, 2, 3, 2, 3, 4, 3, 1...
## $ T2_C2_Host_provider   (dbl) 4, 2, 3, 3, 3, 3, 3, 2, 3, 2, 4, 1, 3, 1...
## $ T2_C2_Personalization (dbl) 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2...
## $ T2_C2_Payment         (dbl) 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 2, 2, 2, 1...
## $ T2_C2_Quantity_sim    (dbl) 1, 3, 1, 1, 1, 2, 1, 1, 1, 1, 2, 2, 3, 1...
## $ T2_C3_Minutes         (dbl) 5, 4, 6, 3, 6, 1, 6, 5, 5, 6, 6, 6, 5, 5...
## $ T2_C3_Gigabytes       (dbl) 7, 7, 9, 8, 4, 8, 9, 10, 3, 4, 9, 4, 4, ...
## $ T2_C3_Price           (dbl) 2, 4, 3, 1, 3, 5, 3, 4, 1, 5, 2, 1, 5, 2...
## $ T2_C3_Host_provider   (dbl) 2, 3, 4, 1, 1, 1, 4, 2, 1, 1, 3, 4, 4, 4...
## $ T2_C3_Personalization (dbl) 2, 2, 2, 1, 1, 1, 2, 1, 2, 1, 1, 2, 2, 2...
## $ T2_C3_Payment         (dbl) 2, 2, 2, 2, 1, 2, 2, 1, 2, 1, 1, 1, 1, 2...
## $ T2_C3_Quantity_sim    (dbl) 1, 1, 1, 2, 2, 1, 1, 2, 3, 3, 1, 1, 1, 2...
## $ T2_C4_Minutes         (dbl) 6, 5, 3, 5, 2, 4, 3, 1, 2, 2, 1, 7, 2, 6...
## $ T2_C4_Gigabytes       (dbl) 2, 6, 5, 10, 1, 6, 5, 7, 8, 7, 2, 1, 9, ...
## $ T2_C4_Price           (dbl) 5, 2, 2, 5, 4, 3, 2, 3, 5, 4, 5, 3, 2, 5...
## $ T2_C4_Host_provider   (dbl) 3, 1, 1, 3, 1, 4, 1, 4, 2, 1, 3, 3, 1, 2...
## $ T2_C4_Personalization (dbl) 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 2, 2, 2...
## $ T2_C4_Payment         (dbl) 1, 2, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1...
## $ T2_C4_Quantity_sim    (dbl) 3, 2, 3, 1, 2, 2, 3, 3, 1, 2, 3, 2, 2, 2...
## $ T2_C5_Minutes         (dbl) 3, 7, 4, 1, 3, 2, 4, 7, 6, 3, 3, 3, 1, 2...
## $ T2_C5_Gigabytes       (dbl) 8, 1, 10, 3, 9, 9, 10, 2, 5, 2, 4, 10, 1...
## $ T2_C5_Price           (dbl) 3, 5, 1, 2, 5, 2, 1, 1, 2, 1, 4, 5, 4, 3...
## $ T2_C5_Host_provider   (dbl) 3, 4, 2, 4, 2, 2, 2, 1, 4, 4, 1, 1, 2, 1...
## $ T2_C5_Personalization (dbl) 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 1, 1, 1...
## $ T2_C5_Payment         (dbl) 2, 1, 2, 2, 2, 2, 2, 1, 1, 2, 1, 2, 2, 2...
## $ T2_C5_Quantity_sim    (dbl) 2, 1, 2, 3, 3, 1, 2, 3, 3, 3, 3, 1, 1, 3...
## $ T2_select             (dbl) 3, 5, 5, 5, 1, 5, 4, 1, 1, 3, 1, 3, 1, 3...
## $ T3_C1_Minutes         (dbl) 1, 1, 5, 2, 7, 1, 5, 1, 7, 4, 4, 6, 2, 5...
## $ T3_C1_Gigabytes       (dbl) 6, 3, 9, 2, 9, 5, 9, 8, 9, 2, 10, 5, 3, ...
## $ T3_C1_Price           (dbl) 3, 3, 1, 4, 3, 4, 1, 5, 2, 4, 2, 4, 1, 4...
## $ T3_C1_Host_provider   (dbl) 2, 1, 1, 3, 4, 3, 1, 3, 1, 2, 1, 3, 2, 2...
## $ T3_C1_Personalization (dbl) 1, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1...
## $ T3_C1_Payment         (dbl) 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2...
## $ T3_C1_Quantity_sim    (dbl) 1, 3, 1, 3, 2, 2, 1, 3, 1, 1, 2, 1, 1, 3...
## $ T3_C2_Minutes         (dbl) 5, 6, 7, 7, 6, 5, 7, 3, 6, 2, 2, 5, 4, 1...
## $ T3_C2_Gigabytes       (dbl) 4, 5, 8, 1, 5, 2, 8, 1, 2, 8, 3, 7, 4, 8...
## $ T3_C2_Price           (dbl) 5, 5, 5, 1, 4, 5, 5, 2, 1, 3, 1, 2, 3, 1...
## $ T3_C2_Host_provider   (dbl) 4, 4, 2, 4, 2, 2, 2, 4, 2, 1, 4, 1, 4, 4...
## $ T3_C2_Personalization (dbl) 1, 2, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2, 1, 2...
## $ T3_C2_Payment         (dbl) 1, 2, 2, 1, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1...
## $ T3_C2_Quantity_sim    (dbl) 2, 1, 2, 1, 2, 2, 2, 2, 2, 3, 1, 2, 2, 2...
## $ T3_C3_Minutes         (dbl) 6, 2, 2, 4, 5, 2, 2, 4, 3, 7, 6, 7, 3, 4...
## $ T3_C3_Gigabytes       (dbl) 3, 2, 1, 4, 3, 10, 1, 4, 1, 4, 7, 9, 7, ...
## $ T3_C3_Price           (dbl) 4, 1, 3, 3, 5, 1, 3, 1, 3, 5, 4, 5, 4, 3...
## $ T3_C3_Host_provider   (dbl) 3, 1, 3, 4, 1, 4, 3, 2, 3, 3, 2, 1, 1, 1...
## $ T3_C3_Personalization (dbl) 2, 2, 2, 2, 1, 2, 2, 1, 2, 1, 1, 2, 2, 2...
## $ T3_C3_Payment         (dbl) 2, 2, 1, 1, 2, 1, 1, 2, 2, 2, 2, 1, 1, 1...
## $ T3_C3_Quantity_sim    (dbl) 3, 2, 3, 2, 1, 1, 3, 2, 2, 3, 1, 3, 3, 3...
## $ T3_C4_Minutes         (dbl) 7, 3, 4, 1, 2, 3, 4, 7, 4, 6, 1, 1, 1, 3...
## $ T3_C4_Gigabytes       (dbl) 10, 10, 6, 5, 2, 4, 6, 9, 8, 7, 5, 10, 9...
## $ T3_C4_Price           (dbl) 1, 2, 2, 2, 1, 3, 2, 4, 5, 2, 5, 3, 5, 2...
## $ T3_C4_Host_provider   (dbl) 1, 3, 3, 1, 2, 1, 3, 3, 4, 4, 2, 4, 3, 4...
## $ T3_C4_Personalization (dbl) 2, 1, 2, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1...
## $ T3_C4_Payment         (dbl) 2, 1, 1, 1, 2, 2, 1, 2, 1, 1, 2, 1, 1, 2...
## $ T3_C4_Quantity_sim    (dbl) 1, 2, 2, 3, 1, 1, 2, 3, 3, 2, 2, 3, 3, 2...
## $ T3_C5_Minutes         (dbl) 3, 5, 1, 5, 4, 4, 1, 6, 1, 5, 5, 2, 5, 7...
## $ T3_C5_Gigabytes       (dbl) 1, 8, 4, 7, 8, 7, 4, 3, 10, 5, 8, 2, 5, ...
## $ T3_C5_Price           (dbl) 2, 4, 4, 5, 2, 2, 4, 3, 4, 1, 3, 1, 2, 5...
## $ T3_C5_Host_provider   (dbl) 2, 2, 4, 2, 3, 3, 4, 1, 4, 1, 3, 2, 2, 3...
## $ T3_C5_Personalization (dbl) 1, 1, 1, 1, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1...
## $ T3_C5_Payment         (dbl) 1, 1, 2, 2, 1, 2, 2, 1, 2, 2, 2, 1, 1, 2...
## $ T3_C5_Quantity_sim    (dbl) 3, 3, 3, 1, 3, 3, 3, 1, 1, 1, 3, 2, 1, 1...
## $ T3_select             (dbl) 4, 5, 2, 4, 1, 5, 2, 2, 1, 5, 2, 5, 5, 1...
## $ T4_C1_Minutes         (dbl) 2, 5, 7, 4, 7, 5, 7, 3, 5, 7, 4, 6, 4, 6...
## $ T4_C1_Gigabytes       (dbl) 6, 10, 2, 9, 7, 6, 2, 3, 6, 1, 4, 1, 8, ...
## $ T4_C1_Price           (dbl) 4, 1, 3, 1, 5, 5, 3, 2, 4, 5, 3, 5, 2, 3...
## $ T4_C1_Host_provider   (dbl) 4, 4, 1, 3, 1, 3, 1, 3, 3, 1, 2, 4, 2, 4...
## $ T4_C1_Personalization (dbl) 2, 2, 1, 2, 1, 1, 1, 2, 1, 2, 1, 1, 2, 2...
## $ T4_C1_Payment         (dbl) 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 1, 2, 2, 2...
## $ T4_C1_Quantity_sim    (dbl) 1, 1, 2, 3, 3, 3, 2, 3, 1, 1, 2, 2, 1, 1...
## $ T4_C2_Minutes         (dbl) 6, 3, 6, 3, 5, 2, 6, 7, 6, 1, 3, 3, 2, 4...
## $ T4_C2_Gigabytes       (dbl) 5, 5, 3, 1, 4, 1, 3, 7, 4, 10, 6, 4, 6, ...
## $ T4_C2_Price           (dbl) 5, 2, 1, 5, 1, 3, 1, 1, 3, 3, 1, 1, 1, 1...
## $ T4_C2_Host_provider   (dbl) 2, 1, 3, 4, 4, 2, 3, 2, 2, 3, 3, 2, 4, 3...
## $ T4_C2_Personalization (dbl) 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1...
## $ T4_C2_Payment         (dbl) 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 2...
## $ T4_C2_Quantity_sim    (dbl) 2, 3, 3, 2, 1, 3, 3, 1, 3, 2, 3, 3, 3, 2...
## $ T4_C3_Minutes         (dbl) 1, 1, 2, 5, 1, 6, 2, 5, 2, 4, 5, 1, 7, 2...
## $ T4_C3_Gigabytes       (dbl) 10, 4, 7, 3, 10, 5, 7, 1, 7, 6, 5, 6, 1,...
## $ T4_C3_Price           (dbl) 1, 4, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 3, 4...
## $ T4_C3_Host_provider   (dbl) 3, 3, 2, 2, 3, 4, 2, 1, 4, 4, 1, 2, 3, 1...
## $ T4_C3_Personalization (dbl) 1, 2, 1, 2, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1...
## $ T4_C3_Payment         (dbl) 2, 1, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1...
## $ T4_C3_Quantity_sim    (dbl) 3, 1, 1, 2, 2, 1, 1, 2, 2, 3, 1, 1, 3, 2...
## $ T4_C4_Minutes         (dbl) 5, 6, 1, 1, 2, 7, 1, 2, 7, 5, 7, 4, 3, 1...
## $ T4_C4_Gigabytes       (dbl) 4, 1, 10, 8, 1, 10, 10, 9, 5, 9, 1, 3, 2...
## $ T4_C4_Price           (dbl) 2, 3, 5, 4, 3, 2, 5, 4, 1, 2, 2, 2, 5, 5...
## $ T4_C4_Host_provider   (dbl) 1, 4, 4, 1, 2, 4, 4, 4, 2, 3, 4, 1, 1, 2...
## $ T4_C4_Personalization (dbl) 2, 1, 2, 1, 2, 2, 2, 2, 1, 2, 1, 2, 1, 2...
## $ T4_C4_Payment         (dbl) 2, 2, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 2...
## $ T4_C4_Quantity_sim    (dbl) 3, 3, 3, 1, 3, 2, 3, 1, 3, 2, 2, 1, 2, 1...
## $ T4_C5_Minutes         (dbl) 4, 4, 5, 6, 3, 4, 5, 4, 3, 3, 6, 2, 6, 7...
## $ T4_C5_Gigabytes       (dbl) 9, 8, 5, 6, 6, 9, 5, 5, 3, 3, 3, 8, 10, ...
## $ T4_C5_Price           (dbl) 3, 5, 2, 2, 4, 4, 2, 5, 5, 4, 5, 4, 4, 2...
## $ T4_C5_Host_provider   (dbl) 1, 2, 2, 4, 4, 1, 2, 1, 1, 2, 4, 3, 3, 3...
## $ T4_C5_Personalization (dbl) 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 2, 2, 2, 1...
## $ T4_C5_Payment         (dbl) 1, 2, 2, 2, 2, 1, 2, 1, 1, 1, 2, 1, 2, 1...
## $ T4_C5_Quantity_sim    (dbl) 1, 2, 2, 1, 1, 1, 2, 2, 2, 1, 3, 3, 2, 3...
## $ T4_select             (dbl) 4, 1, 3, 1, 5, 5, 5, 5, 3, 4, 3, 4, 1, 5...
## $ T5_C1_Minutes         (dbl) 7, 5, 5, 6, 5, 3, 5, 2, 2, 5, 2, 3, 4, 1...
## $ T5_C1_Gigabytes       (dbl) 2, 3, 9, 5, 6, 8, 9, 10, 7, 9, 2, 9, 2, ...
## $ T5_C1_Price           (dbl) 1, 5, 5, 2, 5, 2, 5, 1, 3, 5, 3, 2, 1, 5...
## $ T5_C1_Host_provider   (dbl) 3, 3, 2, 3, 3, 2, 2, 3, 1, 1, 3, 3, 3, 3...
## $ T5_C1_Personalization (dbl) 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 2, 2, 2, 2...
## $ T5_C1_Payment         (dbl) 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1...
## $ T5_C1_Quantity_sim    (dbl) 2, 3, 3, 2, 3, 3, 3, 3, 2, 3, 1, 2, 1, 2...
## $ T5_C2_Minutes         (dbl) 6, 7, 1, 3, 6, 1, 1, 4, 1, 2, 3, 5, 6, 7...
## $ T5_C2_Gigabytes       (dbl) 8, 6, 6, 7, 4, 2, 6, 8, 1, 6, 7, 2, 6, 5...
## $ T5_C2_Price           (dbl) 2, 3, 3, 5, 2, 3, 3, 5, 1, 2, 2, 5, 3, 4...
## $ T5_C2_Host_provider   (dbl) 2, 2, 2, 1, 2, 1, 2, 4, 4, 3, 2, 4, 2, 2...
## $ T5_C2_Personalization (dbl) 1, 1, 2, 2, 2, 1, 2, 1, 1, 1, 2, 1, 2, 2...
## $ T5_C2_Payment         (dbl) 1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 1, 1...
## $ T5_C2_Quantity_sim    (dbl) 1, 1, 2, 3, 3, 3, 2, 1, 3, 1, 3, 1, 2, 3...
## $ T5_C3_Minutes         (dbl) 4, 2, 3, 7, 3, 4, 3, 7, 7, 4, 4, 1, 1, 5...
## $ T5_C3_Gigabytes       (dbl) 3, 7, 10, 10, 9, 3, 10, 6, 3, 1, 9, 3, 1...
## $ T5_C3_Price           (dbl) 5, 4, 4, 3, 1, 1, 4, 4, 4, 3, 4, 4, 5, 2...
## $ T5_C3_Host_provider   (dbl) 4, 1, 3, 2, 4, 3, 3, 2, 2, 4, 2, 3, 4, 2...
## $ T5_C3_Personalization (dbl) 2, 2, 1, 1, 1, 1, 1, 2, 2, 1, 2, 1, 1, 2...
## $ T5_C3_Payment         (dbl) 2, 1, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 2, 1...
## $ T5_C3_Quantity_sim    (dbl) 3, 2, 1, 1, 2, 2, 1, 2, 2, 1, 2, 2, 1, 1...
## $ T5_C4_Minutes         (dbl) 2, 4, 7, 2, 7, 6, 7, 5, 4, 6, 5, 2, 5, 4...
## $ T5_C4_Gigabytes       (dbl) 1, 9, 1, 4, 2, 4, 1, 4, 5, 10, 10, 10, 3...
## $ T5_C4_Price           (dbl) 4, 2, 2, 4, 4, 4, 2, 2, 2, 4, 5, 3, 4, 3...
## $ T5_C4_Host_provider   (dbl) 4, 1, 1, 3, 3, 2, 1, 1, 3, 2, 4, 2, 1, 4...
## $ T5_C4_Personalization (dbl) 1, 1, 2, 1, 2, 2, 2, 1, 1, 1, 1, 2, 1, 1...
## $ T5_C4_Payment         (dbl) 1, 2, 1, 1, 1, 1, 1, 2, 2, 2, 1, 2, 2, 2...
## $ T5_C4_Quantity_sim    (dbl) 3, 1, 1, 2, 1, 2, 1, 1, 3, 3, 1, 1, 3, 1...
## $ T5_C5_Minutes         (dbl) 1, 3, 4, 5, 4, 7, 4, 6, 6, 1, 1, 6, 7, 6...
## $ T5_C5_Gigabytes       (dbl) 7, 2, 2, 2, 10, 7, 2, 2, 2, 4, 8, 1, 9, ...
## $ T5_C5_Price           (dbl) 3, 1, 1, 1, 3, 5, 1, 3, 5, 1, 1, 1, 2, 1...
## $ T5_C5_Host_provider   (dbl) 1, 4, 4, 4, 1, 4, 4, 4, 1, 4, 1, 1, 4, 1...
## $ T5_C5_Personalization (dbl) 2, 2, 1, 2, 2, 2, 1, 1, 2, 2, 1, 1, 2, 1...
## $ T5_C5_Payment         (dbl) 2, 1, 2, 2, 2, 1, 2, 2, 1, 1, 1, 1, 2, 2...
## $ T5_C5_Quantity_sim    (dbl) 2, 3, 3, 3, 1, 1, 3, 3, 1, 2, 2, 3, 3, 3...
## $ T5_select             (dbl) 2, 4, 1, 5, 5, 3, 5, 3, 1, 4, 1, 5, 1, 5...
## $ T6_C1_Minutes         (dbl) 1, 1, 3, 4, 6, 5, 3, 1, 5, 3, 6, 1, 3, 7...
## $ T6_C1_Gigabytes       (dbl) 4, 8, 8, 2, 1, 3, 8, 6, 9, 2, 6, 4, 4, 2...
## $ T6_C1_Price           (dbl) 2, 1, 4, 2, 5, 4, 4, 1, 4, 3, 4, 2, 2, 3...
## $ T6_C1_Host_provider   (dbl) 4, 3, 1, 1, 1, 2, 1, 1, 4, 2, 2, 2, 2, 2...
## $ T6_C1_Personalization (dbl) 2, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 1, 1, 2...
## $ T6_C1_Payment         (dbl) 1, 1, 2, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2...
## $ T6_C1_Quantity_sim    (dbl) 1, 2, 3, 1, 1, 1, 3, 3, 2, 2, 2, 3, 3, 2...
## $ T6_C2_Minutes         (dbl) 5, 3, 5, 7, 5, 4, 5, 3, 2, 4, 3, 3, 1, 2...
## $ T6_C2_Gigabytes       (dbl) 8, 1, 4, 8, 7, 2, 4, 9, 10, 3, 1, 7, 1, ...
## $ T6_C2_Price           (dbl) 1, 3, 2, 5, 4, 2, 2, 3, 3, 5, 5, 5, 1, 5...
## $ T6_C2_Host_provider   (dbl) 1, 3, 3, 3, 2, 4, 3, 3, 2, 1, 1, 4, 1, 3...
## $ T6_C2_Personalization (dbl) 1, 2, 2, 2, 2, 1, 2, 1, 1, 1, 1, 2, 2, 2...
## $ T6_C2_Payment         (dbl) 2, 2, 2, 2, 1, 1, 2, 1, 1, 2, 2, 2, 2, 2...
## $ T6_C2_Quantity_sim    (dbl) 3, 1, 2, 2, 2, 3, 2, 1, 1, 2, 1, 1, 2, 3...
## $ T6_C3_Minutes         (dbl) 4, 5, 2, 1, 1, 3, 2, 7, 6, 5, 7, 7, 2, 4...
## $ T6_C3_Gigabytes       (dbl) 6, 9, 7, 1, 5, 9, 7, 5, 4, 8, 9, 8, 5, 1...
## $ T6_C3_Price           (dbl) 3, 4, 1, 1, 3, 5, 1, 5, 1, 2, 1, 3, 4, 4...
## $ T6_C3_Host_provider   (dbl) 3, 4, 4, 2, 4, 3, 4, 2, 3, 2, 3, 3, 3, 2...
## $ T6_C3_Personalization (dbl) 2, 2, 1, 1, 1, 2, 1, 2, 2, 2, 2, 1, 1, 1...
## $ T6_C3_Payment         (dbl) 1, 2, 1, 2, 2, 2, 1, 2, 2, 1, 1, 1, 1, 1...
## $ T6_C3_Quantity_sim    (dbl) 2, 2, 2, 2, 3, 2, 2, 2, 2, 3, 3, 1, 1, 1...
## $ T6_C4_Minutes         (dbl) 7, 7, 7, 6, 2, 2, 7, 2, 4, 7, 2, 5, 6, 5...
## $ T6_C4_Gigabytes       (dbl) 1, 4, 3, 4, 3, 6, 3, 10, 6, 5, 4, 6, 7, ...
## $ T6_C4_Price           (dbl) 5, 2, 3, 4, 2, 1, 3, 2, 5, 4, 2, 4, 5, 1...
## $ T6_C4_Host_provider   (dbl) 3, 2, 2, 1, 3, 1, 2, 2, 3, 4, 1, 4, 4, 4...
## $ T6_C4_Personalization (dbl) 1, 2, 1, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 1...
## $ T6_C4_Payment         (dbl) 2, 2, 1, 2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1...
## $ T6_C4_Quantity_sim    (dbl) 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 2...
## $ T6_C5_Minutes         (dbl) 3, 6, 6, 2, 4, 6, 6, 6, 3, 6, 4, 4, 4, 3...
## $ T6_C5_Gigabytes       (dbl) 10, 2, 5, 7, 8, 1, 5, 8, 8, 7, 10, 5, 8,...
## $ T6_C5_Price           (dbl) 4, 5, 5, 3, 1, 3, 5, 4, 2, 1, 3, 1, 3, 2...
## $ T6_C5_Host_provider   (dbl) 2, 1, 3, 4, 1, 4, 3, 4, 1, 3, 4, 1, 3, 1...
## $ T6_C5_Personalization (dbl) 2, 1, 2, 1, 1, 2, 2, 2, 2, 2, 1, 2, 1, 2...
## $ T6_C5_Payment         (dbl) 1, 1, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 1, 2...
## $ T6_C5_Quantity_sim    (dbl) 2, 1, 1, 3, 2, 2, 1, 2, 3, 1, 3, 2, 2, 1...
## $ T6_select             (dbl) 2, 4, 4, 5, 5, 4, 3, 5, 1, 5, 2, 5, 1, 4...
## $ T7_C1_Minutes         (dbl) 2, 4, 6, 5, 1, 1, 6, 3, 5, 2, 6, 5, 2, 3...
## $ T7_C1_Gigabytes       (dbl) 2, 6, 4, 10, 2, 7, 4, 7, 5, 7, 3, 10, 10...
## $ T7_C1_Price           (dbl) 3, 1, 4, 2, 1, 1, 4, 1, 5, 5, 1, 1, 2, 5...
## $ T7_C1_Host_provider   (dbl) 2, 2, 3, 4, 1, 2, 3, 2, 3, 2, 2, 4, 1, 4...
## $ T7_C1_Personalization (dbl) 1, 1, 1, 1, 2, 1, 1, 1, 2, 2, 1, 2, 2, 1...
## $ T7_C1_Payment         (dbl) 2, 1, 1, 2, 2, 1, 1, 2, 1, 2, 2, 1, 1, 1...
## $ T7_C1_Quantity_sim    (dbl) 3, 3, 3, 3, 1, 1, 3, 2, 2, 3, 1, 2, 2, 2...
## $ T7_C2_Minutes         (dbl) 5, 6, 1, 1, 3, 2, 1, 5, 1, 7, 1, 4, 1, 6...
## $ T7_C2_Gigabytes       (dbl) 3, 10, 9, 9, 1, 10, 9, 4, 3, 2, 5, 8, 3,...
## $ T7_C2_Price           (dbl) 1, 3, 5, 4, 3, 2, 5, 3, 3, 2, 3, 4, 3, 2...
## $ T7_C2_Host_provider   (dbl) 3, 3, 1, 1, 3, 3, 1, 4, 1, 1, 3, 2, 2, 3...
## $ T7_C2_Personalization (dbl) 2, 2, 1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 2, 1...
## $ T7_C2_Payment         (dbl) 1, 2, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1...
## $ T7_C2_Quantity_sim    (dbl) 1, 2, 2, 2, 3, 2, 2, 1, 3, 2, 1, 2, 3, 3...
## $ T7_C3_Minutes         (dbl) 7, 7, 2, 3, 4, 5, 2, 4, 4, 3, 2, 6, 7, 5...
## $ T7_C3_Gigabytes       (dbl) 5, 3, 2, 3, 3, 4, 2, 1, 7, 5, 2, 4, 7, 3...
## $ T7_C3_Price           (dbl) 5, 4, 2, 3, 4, 3, 2, 2, 1, 3, 4, 3, 4, 4...
## $ T7_C3_Host_provider   (dbl) 1, 1, 2, 2, 2, 3, 2, 1, 1, 3, 4, 1, 4, 3...
## $ T7_C3_Personalization (dbl) 2, 1, 2, 2, 1, 2, 2, 2, 2, 1, 1, 2, 1, 1...
## $ T7_C3_Payment         (dbl) 1, 1, 2, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1...
## $ T7_C3_Quantity_sim    (dbl) 3, 1, 3, 3, 3, 3, 3, 3, 1, 3, 2, 3, 2, 1...
## $ T7_C4_Minutes         (dbl) 3, 2, 4, 4, 2, 7, 4, 1, 7, 1, 7, 2, 3, 2...
## $ T7_C4_Gigabytes       (dbl) 7, 5, 6, 6, 7, 8, 6, 2, 6, 4, 7, 1, 8, 6...
## $ T7_C4_Price           (dbl) 4, 5, 1, 1, 5, 4, 1, 4, 2, 4, 5, 2, 1, 1...
## $ T7_C4_Host_provider   (dbl) 2, 2, 1, 3, 4, 4, 1, 3, 2, 4, 1, 2, 1, 1...
## $ T7_C4_Personalization (dbl) 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 2, 1, 2...
## $ T7_C4_Payment         (dbl) 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2...
## $ T7_C4_Quantity_sim    (dbl) 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 3, 3, 1, 1...
## $ T7_C5_Minutes         (dbl) 6, 1, 3, 2, 7, 3, 3, 6, 3, 6, 5, 7, 5, 1...
## $ T7_C5_Gigabytes       (dbl) 9, 7, 5, 5, 10, 5, 5, 3, 1, 10, 8, 3, 6,...
## $ T7_C5_Price           (dbl) 2, 2, 3, 5, 2, 5, 3, 5, 4, 1, 2, 5, 5, 3...
## $ T7_C5_Host_provider   (dbl) 4, 4, 4, 2, 2, 1, 4, 3, 4, 1, 3, 3, 3, 2...
## $ T7_C5_Personalization (dbl) 1, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 1, 2, 2...
## $ T7_C5_Payment         (dbl) 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 2, 2...
## $ T7_C5_Quantity_sim    (dbl) 2, 3, 1, 1, 2, 2, 1, 1, 3, 2, 2, 1, 3, 3...
## $ T7_select             (dbl) 5, 1, 3, 1, 5, 2, 3, 4, 3, 5, 2, 1, 4, 2...

Исходим из предположения, что каждому индивиду предлагалось одно и то же количество карточек. Также предполагаем, что на каждой карточке было одно и то же количество альтернатив.

Разрезаем исходную матрицу на две, \(y\) и \(X\):

n_persons <- nrow(df)
n_cards <- 7
n_alternatives <- 5

person_ids <- df$id


df_X <- dplyr::select(df, -ends_with("select"))
df_y <- dplyr::select(df, ends_with("select"))
head(df_y)
## Source: local data frame [6 x 7]
## 
##   T1_select T2_select T3_select T4_select T5_select T6_select T7_select
##       (dbl)     (dbl)     (dbl)     (dbl)     (dbl)     (dbl)     (dbl)
## 1         3         3         4         4         2         2         5
## 2         3         5         5         1         4         4         1
## 3         3         5         2         3         1         4         3
## 4         2         5         4         1         5         5         1
## 5         3         1         1         5         5         5         5
## 6         4         5         5         5         3         4         2

Превращаем матрицу \(X\) из широкой в длинную:

df_X_melted <- melt(df_X, id.vars = c("id", "version"))
head(df_X_melted)
##    id version      variable value
## 1  93      40 T1_C1_Minutes     4
## 2 105      72 T1_C1_Minutes     1
## 3 124      97 T1_C1_Minutes     4
## 4 127      32 T1_C1_Minutes     3
## 5 141      99 T1_C1_Minutes     4
## 6 158      62 T1_C1_Minutes     6

Разбиваем название переменной на три составляющих:

df_X_sep <- tidyr::separate(df_X_melted, variable, into = c("card", "alternative", "variable"), sep = "_")
## Warning: Too many values at 20720 locations: 889, 890, 891, 892, 893, 894,
## 895, 896, 897, 898, 899, 900, 901, 902, 903, 904, 905, 906, 907, 908, ...
# warning возникает из за разбивки "T1_C4_Host_provider". "provider" теряется
# пока забьём
head(df_X_sep)
##    id version card alternative variable value
## 1  93      40   T1          C1  Minutes     4
## 2 105      72   T1          C1  Minutes     1
## 3 124      97   T1          C1  Minutes     4
## 4 127      32   T1          C1  Minutes     3
## 5 141      99   T1          C1  Minutes     4
## 6 158      62   T1          C1  Minutes     6

Переделываем табличку с данными в список:

# list lgtdata should contain for each person:
# y numeric vector (n_card x 1)
# X numeric matrix (n_card * n_alternatives) x (n_variables):
# card alternative var1 var2
#  1 1 xx zz
#  1 2 xx zz
#  2 1 xx zz
#  2 2 xx zz

choice_data <- list()
for (person_no in 1:n_persons) {
  person_id <- person_ids[person_no]
  
  person_y <- unlist(df_y[person_no, ])
  names(person_y) <- NULL
  # unlist нужен чтобы превратить tbl_df размера (1 x n_cards) в простой вектор
  
  person_X_melted <- df_X_sep %>% dplyr::filter(id == person_id) %>% 
    dplyr::select(-id, -version)
  
  person_X_df <- dcast(person_X_melted, card + alternative  ~ variable, 
                       value.var = "value")
  person_X <- dplyr::select(person_X_df, -card, -alternative) %>% as.matrix()

  choice_data[[person_no]] <- list(y = person_y, X = person_X)
}

Структура матриц \(y\) и \(X\) для последнего индивида. Для наглядности в матрице \(X\) сохранены номера карточек и альтернатив:

head(person_y)
## [1] 4 2 4 4 2 2
head(person_X_df)
##   card alternative Gigabytes Host Minutes Payment Personalization Price
## 1   T1          C1         4    1       7       1               1     1
## 2   T1          C2         8    2       3       2               2     4
## 3   T1          C3         5    3       2       2               2     2
## 4   T1          C4        10    4       5       1               1     3
## 5   T1          C5         9    3       6       1               2     5
## 6   T2          C1         1    1       1       1               2     4
##   Quantity
## 1        3
## 2        1
## 3        2
## 4        1
## 5        3
## 6        1

Наконец оценим:

# ncomp - количество смешиваемых нормальных распределений (кластеров среди покупателей)
# p - число альтернатив (должно быть постоянно!)
mcmc_pars <- list(R = 2000, keep = 10)
# R - общее количество симуляций
# keep = 10, сохраняем каждую 10ую

out <- rhierMnlRwMixture(Data = list(p = 5, lgtdata = choice_data),
                      Prior = list(ncomp = 1), Mcmc = mcmc_pars)
## Z not specified
## Table of Y values pooled over all units
## ypooled
##   1   2   3   4   5 
## 446 442 396 399 389 
##  
## Starting MCMC Inference for Hierarchical Logit:
##    Normal Mixture with 1 components for first stage prior
##    5  alternatives;  7  variables in X
##    for  296  cross-sectional units
##  
## Prior Parms: 
## nu = 10
## V 
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]   10    0    0    0    0    0    0
## [2,]    0   10    0    0    0    0    0
## [3,]    0    0   10    0    0    0    0
## [4,]    0    0    0   10    0    0    0
## [5,]    0    0    0    0   10    0    0
## [6,]    0    0    0    0    0   10    0
## [7,]    0    0    0    0    0    0   10
## mubar 
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]    0    0    0    0    0    0    0
## Amu 
##      [,1]
## [1,] 0.01
## a 
## [1] 5
##  
## MCMC Parms: 
## s= 0.9  w=  0.1  R=  2000  keep=  10  nprint=  100
## 
## initializing Metropolis candidate densities for  296  units ...
##   completed unit # 50
##   completed unit # 100
##   completed unit # 150
##   completed unit # 200
##   completed unit # 250
##  MCMC Iteration (est time to end - min) 
##  100 (0.0)
##  200 (0.0)
##  300 (0.0)
##  400 (0.1)
##  500 (0.1)
##  600 (0.0)
##  700 (0.0)
##  800 (0.0)
##  900 (0.0)
##  1000 (0.0)
##  1100 (0.0)
##  1200 (0.0)
##  1300 (0.0)
##  1400 (0.0)
##  1500 (0.0)
##  1600 (0.0)
##  1700 (0.0)
##  1800 (0.0)
##  1900 (0.0)
##  2000 (0.0)
##  Total Time Elapsed: 0.05

Логарифм правдоподобия???:

plot(out$loglike, type = "l")

График бет усреднённый по индивидам:

burnin <- 50

betas <- out$betadraw

# betas: [n_person x n_variables x chain_lenght]
chain_lenght <- dim(betas)[3]
betas <- betas[, , (burnin + 1):chain_lenght]

betas_mean <- t(apply(betas, c(2, 3), mean))
matplot(betas_mean, type = "l")

Значения бет, усреднённые по индивидам:

beta_estimate_df <- data.frame(varname = colnames(choice_data[[1]]$X),
                                    beta_estimate = apply(betas, c(2), mean))
beta_estimate_df
##           varname beta_estimate
## 1       Gigabytes    0.22545999
## 2            Host   -0.28636579
## 3         Minutes    0.47523959
## 4         Payment   -0.09398692
## 5 Personalization   -0.15583096
## 6           Price   -0.58002422
## 7        Quantity    0.16617111

Тестируем сходимость…

Сохраняем беты для индивидов:

personal_betas <- apply(betas, c(1, 2), mean)
colnames(personal_betas) <- colnames(choice_data[[1]]$X)
df_betas <- cbind(dplyr::select(df, id), personal_betas)
head(df_betas)
##    id Gigabytes        Host     Minutes     Payment Personalization
## 1  93 0.3836733 -0.44900577  1.08770908  0.08221019     -0.08639441
## 2 105 0.2942580 -0.18267362  0.55542007 -0.18840870     -0.52459999
## 3 124 0.2966247 -0.50920370  0.38127583 -0.02868183     -0.42650785
## 4 127 0.3190092  0.35920652 -0.03169329  0.02036271     -0.07515843
## 5 141 0.4464321  0.09129706  0.65850741  0.20779608     -0.12979423
## 6 158 0.4189896 -0.80246568  0.12262250 -0.05548735     -0.62680929
##        Price    Quantity
## 1 -0.9217028  0.19978635
## 2 -0.6013333 -0.01531799
## 3  0.0763326  0.05577725
## 4 -1.0870413  1.09119671
## 5 -0.4980147 -0.49107021
## 6 -0.8038376  0.28709210
write.csv(df_betas, file = "person_betas.csv", row.names = FALSE)

Пример из хелпа