0
$\begingroup$

I'm trying to follow these instructions: https://docs.google.com/spreadsheets/d/1Piw0Fk0XCWBJOHhC8NsljvIPywaBmLdNa8QpEV75fHQ/edit#gid=777817907 to do a choice-based conjoint analysis with multiple regression like they did, but with my own data.

With my data, I have 3 attributes where 2 of them have 3 levels and the remaining one has 2 levels.

Initially, this analysis should be done using an MCMC Hierarchical Bayesian Multinomial Logit Model. However, I'm interested in reproducing their steps by using only the simpler version with multiple regression (I saw there's a way to use this MCMC HB model with the "bayesm" package in R but I haven't found a simple way to do it...)

In my data, I also have one column per level instead of one column per attribute like they did. The problem is that, since they only use 2 levels per attribute in their example, they have no problem in encoding everything with 1s and 0s. How would I go about this with 3 levels per attribute?

Also, when it comes to calculating the last table in the third Sheet ("3. Calculation of utilities"), I have no idea on how to begin to do this with more than 2 levels per attribute. How would you do it?

By saving the table from the third Sheet below where it says "Let's put all the data together" into an XLSX file and then running the following in R, you can reproduce their regressions:

library(tidyverse)
library(broom)
library(janitor)
library(readxl)
library(writexl)

df <- read_xlsx("data.xlsx") %>% janitor::clean_names()

final_data <- df %>% 
  pivot_longer(!choice_set, names_to = "respondent_id", values_to = "selection") %>% 
  pivot_wider(names_from = "choice_set", values_from = "selection") %>% 
  janitor::clean_names() %>% 
  mutate(respondent_id = str_sub(respondent_id, start = 2, end = 3),
         respondent_id = ifelse(respondent_id != 10, str_sub(respondent_id, start = 1, end = 1), respondent_id))

fit1 <- lm(your_choice ~ brand + pack_format + fat_content + price_per_litre, data = final_data)

tidy(fit1)

Thank you so much.

$\endgroup$

1 Answer 1

1
$\begingroup$

In my data, I also have one column per level instead of one column per attribute like they did. The problem is that, since they only use 2 levels per attribute in their example, they have no problem in encoding everything with 1s and 0s. How would I go about this with 3 levels per attribute?

You will need to add another dummy-variable. For instance, if there are three brands, you will need two variables for brands. There are already packages to help you with that, e.g. psych or fastDummies.

library(psych)
brands <- 1:3
dummy.code(brands)

Note that you will not need all 3 dummies, only 2. Always one less than the number of levels of the attribute.

Actually, if your data is tidy, you can just put in categorical variables into lm and R will handle the dummy-coding for you.

Also, when it comes to calculating the last table in the third Sheet ("3. Calculation of utilities"), I have no idea on how to begin to do this with more than 2 levels per attribute. How would you do it?

The same way. Just calculate the range (max-min) of the part-worths of one attribute and then proceed as normal.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.