---
title: "Conjoint design and analysis using R"
date: "2024-08-08"
---
This post is to illustrate how to use R to do basic conjoint analysis.
## Conjoint Analysis
Marketing people used to use Sawtooth to do conjoint analysis.
What is conjoint analysis? Here is the intro from Sawtooth:
"Conjoint analysis methods use statistical analysis to compute mathematical representations of survey respondents’ preferences for product features, simulate how attribute changes affect demand, and even predict market acceptance of new products before they launch.
This powerful research methodology has become very popular with market researchers and economists for the valuable insight that few other research methods can provide."
"Consider the task of purchasing a new vehicle.
When searching for the “right” vehicle, you typically consider a multitude of variables: type, make (brand), model, year, mileage, price, color, accessories packages, etc. You can see how this is a complex choice to make.
How does anyone possibly make such a complicated decision?
You think about what features matter most to you and then make tradeoffs.
For example, one vehicle could be an acceptable make and model, but at a high price point, whereas a second option is not the ideal make and model but has a more palatable price.
The tradeoff in this simple example is whether the make and model is more preferable to a reasonable price, or vice versa.
A conjoint survey captures the relationship between different attributes of a product and how preferable they are both comparatively and when combined.
Because of this, conjoint analysis is the gold standard solution for testing multiple features simultaneously, when limited A/B testing doesn’t go far enough."
"In simple terms, the conjoint survey design algorithm generates a balanced survey design with product profiles, also known as concepts, formulated to have ideal statistical properties (such as level balance and independence of attributes).
Each profile is made up of multiple conjoined product features (hence, conjoint analysis), such as brand, type, engine, and color, each with systematically varied levels.
These product concepts are then included in a series of questions, usually 8-20, called choice tasks, that make up the conjoint analysis portion of the survey."
## Example
The example uses the tea data that ships with the `conjoint` package
[@bak-bartlomowicz-2012]. A hundred respondents rate 13 tea profiles on a
scale from 0 to 10. Each profile combines four attributes: price (low, medium,
high), variety (black, green, red), kind (bags, granulated, leafy) and aroma
(yes, no).
```{r}
#| message: false
#| warning: false
library(tidyverse)
library(conjoint)
data(tea) # tprof: the 13 profiles; tprefm: 100 x 13 ratings; tlevn: level names
levels_list <- list(price = c("low", "medium", "high"),
variety = c("black", "green", "red"),
kind = c("bags", "granulated", "leafy"),
aroma = c("yes", "no"))
profiles <- tprof
profiles[] <- Map(function(x, l) factor(l[x], levels = l), tprof, levels_list)
tea_long <- tibble(respondent = rep(1:100, each = 13),
profile = rep(1:13, times = 100),
rating = as.vector(t(tprefm))) |>
bind_cols(profiles[rep(1:13, times = 100), ])
tea_long
```
The data are 1,300 rows, 100 respondents times 13 profiles.
## Conjoint design of experiment
With three, three, three and two levels, the full factorial has
$3 \times 3 \times 3 \times 2 = 54$ profiles. Asking every respondent to rate 54
teas is too much, so a conjoint study shows a fraction of them. The question is
which fraction. A good design lets us estimate the effect of every attribute
level, with little correlation between the attributes.
`AlgDesign` searches for such designs. `optFederov()` picks the subset of the
full factorial that maximizes the determinant of the information matrix for a
main-effects model, the D-optimality criterion. A main-effects model here has 8
parameters, an intercept plus $2 + 2 + 2 + 1$ level contrasts, so a design needs
at least 8 profiles. We search for designs of several sizes and report their
D-efficiency [@kuhfeld-1994] and whether every level appears equally often.
```{r}
#| message: false
#| warning: false
library(AlgDesign)
full <- expand.grid(lapply(levels_list, factor))
set.seed(123)
designs <- map(c(9, 12, 13, 18, 27, 54),
\(n) optFederov(~ ., data = full, nTrials = n, criterion = "D"))
tibble(trials = c(9, 12, 13, 18, 27, 54),
D_efficiency = map_dbl(designs, "Dea"),
balanced = map_lgl(designs, \(d) all(map_lgl(d$design,
\(f) length(unique(table(f))) == 1))))
```
D-efficiency measures how much information a design carries per profile,
relative to an ideal orthogonal design. A design with efficiency 0.8 needs about
25% more observations to give the same standard errors as one with
efficiency 1. Balance requires a number of profiles divisible by 3 and by 2.
Among these sizes, only 12, 18 and 54 are balanced. The 18-profile design reaches
an efficiency of 1: it is an orthogonal main-effects design, a third of the full
factorial with nothing lost for a main-effects model.
The tea study used 13 profiles. That number cannot be balanced, and the
design shows it.
```{r}
map(profiles, table)
X <- model.matrix(~ price + variety + kind + aroma, profiles)[, -1]
round(cor(X), 2)
```
Low price and red variety appear 3 times, while other levels appear 5 times,
and aroma is "no" in 8 of the 13 profiles. The correlations between dummies of
the *same* attribute, such as $-0.62$ for medium and high price, come from the
coding and are expected. What matters is the correlation *between* attributes,
and it stays small: the largest is 0.13, between red variety and granulated
kind. The attributes are close to independent, so each attribute's effect can be
estimated without much contamination from the others.
## Conjoint analysis
The part-worths are the coefficients of a regression of the rating on attribute
dummies. Each respondent rates 13 profiles, so the ratings are clustered by
respondent, and the standard errors have to allow for that.
```{r}
#| message: false
#| warning: false
library(sandwich)
library(lmtest)
partworths_lm <- lm(rating ~ price + variety + kind + aroma, data = tea_long)
coeftest(partworths_lm, vcov = vcovCL(partworths_lm, cluster = ~ respondent))
```
The baseline profile is low-priced black tea in bags with aroma. Relative to it,
red tea lowers the rating by 1.26 points and green tea by 0.58. Granulated tea
lowers it by 1.03, while leafy tea raises it by 0.62. Tea without aroma is rated
0.82 lower. Price matters least: medium and high prices lower the rating by about
0.35, and only the medium-price contrast is significant at 5%.
Conjoint software usually reports part-worths in effect coding, where the
part-worths of each attribute sum to zero. They are the same coefficients,
re-centred within each attribute. The importance of an attribute is the range of
its part-worths as a share of the sum of the ranges.
```{r}
#| fig-width: 8
#| fig-height: 3.5
part_worths <- imap_dfr(levels_list, \(lv, attr) {
b <- c(0, coef(partworths_lm)[paste0(attr, lv[-1])])
tibble(attribute = attr, level = factor(lv, levels = lv), part_worth = b - mean(b))
})
importance <- part_worths |>
group_by(attribute) |>
summarise(range = diff(range(part_worth))) |>
mutate(importance = round(100 * range / sum(range), 1))
importance
ggplot(part_worths, aes(level, part_worth)) +
geom_col(fill = "darkcyan") +
geom_hline(yintercept = 0) +
facet_wrap(~ attribute, scales = "free_x", nrow = 1) +
labs(x = NULL, y = "Part-worth") +
theme_bw()
```
Kind is the most important attribute, with 39.9% of the total range, followed by
variety with 30.8%, aroma with 20.0% and price with 9.3%.
The `conjoint` package computes the same quantities. Its pooled part-worths
match ours exactly. Its importance figures do not, and the reason is worth
knowing. `caImportance()` computes each respondent's importance from that
respondent's own part-worths, and then averages across respondents. Individual
part-worths from 13 ratings are noisy, and noise widens every attribute's range,
so the averaged importance spreads more evenly across attributes than the pooled
figure.
```{r}
#| message: false
#| warning: false
# conjoint's functions switch R to sum-to-zero contrasts globally and do not
# switch back, which would break every later regression; save and restore.
op <- options("contrasts")
invisible(capture.output(pooled <- caUtilities(y = tprefm, x = tprof, z = tlevn)))
invisible(capture.output(imp <- caImportance(y = tprefm, x = tprof)))
options(op)
round(pooled, 3) # intercept, then effect-coded part-worths
setNames(imp, names(levels_list)) # averaged over respondents
```
The averaged importances are 24.8 for price, 32.2 for variety, 27.2 for kind and
15.9 for aroma. Price moves from least important to second. The two figures
answer different questions. The pooled range describes the average respondent.
The averaged importance describes how much each attribute matters within a
respondent, and it is inflated by sampling noise in the individual fits.
## Causal effect in conjoint analysis
@hainmueller-2014 define the average marginal component effect (AMCE): the
average change in the outcome when one attribute moves from one level to
another, averaged over the distribution of the other attributes. It is not to be
confused with the ACME, the average causal mediation effect of @imai-keele-tingley-2010. When
the attribute levels are randomized independently of each other and of the
respondents, the AMCE is identified, and it is estimated by the coefficients of a
regression of the outcome on the attribute dummies. Standard errors are clustered
by respondent. The `cjoint` package implements it.
```{r}
#| message: false
#| warning: false
library(cjoint)
amce_fit <- amce(rating ~ price + variety + kind + aroma, data = as.data.frame(tea_long),
cluster = TRUE, respondent.id = "respondent")
imap_dfr(amce_fit$estimates, \(m, attr)
tibble(attribute = attr, level = colnames(m),
amce = round(m["AMCE", ], 3), se = round(m["Std. Error", ], 3)))
```
The AMCEs are exactly the part-worth coefficients above, with the same clustered
standard errors. The tea profiles were not randomized; they are a fixed
13-profile design. The attributes in it are close to independent, which is why
the regression coefficients still read as average effects of each attribute. The
causal reading also needs the ratings to reflect the attributes shown and nothing
else about the profile, and it holds only for the population of profiles the
design represents.
## Summary
A conjoint study asks respondents to rate or choose among product profiles whose attributes vary by design. The full factorial is usually too long, so we use a fraction chosen for D-efficiency and balance. For the tea attributes, an 18-profile design is fully efficient and balanced. The study's own 13 profiles cannot be balanced, but the correlations between attributes stay below 0.13.
The analysis is a regression of the ratings on attribute dummies, with standard errors clustered by respondent. The coefficients are the part-worths. Kind and variety matter most, and price least. Importance from the pooled part-worths differs from `conjoint`'s average of individual importances, because individual fits are noisy. When attribute levels are independent, the same coefficients are Hainmueller et al.'s average marginal component effects, which `cjoint` reproduces.