---
title: "Analyze and Visualize Important QOIs"
output: rmarkdown::html_vignette
description: >
  How to estimate and correct marginal means (MMs) or average marginal component effects (AMCEs), including predicting IRR if necessary. Visualize these and other QOIs of interest.
vignette: >
  %\VignetteIndexEntry{Analyze and Visualize Important QOIs}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---
```{r setup, include=FALSE}
library(projoint)
library(dplyr)
library(ggplot2)
library(patchwork)
```
## 🎯 Estimate Corrected MMs or AMCEs
In conjoint analysis, default MMs and AMCEs can be **biased** due to measurement error from intra-respondent variability.
**projoint** corrects for this bias automatically.
The following instructions apply to choice-level data. What if you have profile-level data?ⓘ Our FAQ Page has instructions to estimate and visualize profile-level QOIs.
---
## 📦 Prepare Example Data
> **Outcome naming & order (important)**
> 
> - List `.outcomes` in the **order questions were asked**.  
> - If you have a repeated task, its outcome must be the **last element**.  
> - For base tasks (all but last), the function reads the **digits** in each name as the task id (e.g., `"choice4"`, `"Q4"`, `"task04"` → task 4).  
> - The **repeated base task** is inferred from the **first base outcome’s digits**. The repeated outcome itself **need not** contain digits—only its position (last) matters.  
> - Outcome strings should end with your choice labels; by default we parse the **last character** and expect `"A"`/`"B"`. If your survey uses `"1"`/`"2"` (or other endings), set `.choice_labels` accordingly.
**Examples**
```{r}
# Standard order; repeated = task 1
data("exampleData1")
outcomes <- c(paste0("choice", 1:8), "choice1_repeated_flipped")
out1 <- reshape_projoint(exampleData1, outcomes)
```
---
## 🛠️ Why Use IDs (e.g., `att1`, `level1`)?
Before estimating quantities, it’s important to understand how attribute and level IDs work inside projoint.
We recommend working with **attribute IDs** rather than actual text labels because:
- Safer against special characters, languages, or typos
- Allows **multiple attributes** to have identical labels (e.g., "High" for both "Teaching Quality" and "Research Quality")
Check attribute-level mappings:
```{r}
out1$labels
```
You can also save these labels for easier editing:
```r
save_labels(out1, "labels.csv")
```
 
---
## 📈 Estimate Marginal Means (MMs)
Choice-Level MMs (Specific Level)
Suppose you want to estimate, within a given profile pair, the probability of choosing a profile that includes "40% of pre-tax income" (`level3`) for Housing Cost (`att1`) rather than one that includes "15% of pre-tax income" (`level1`) for the same attribute, averaging over all combinations of the other attributes and across respondents; then use the following code:
```{r}
qoi <- set_qoi(
  .structure = "choice_level",
  .att_choose = "att1",
  .lev_choose = "level3",
  .att_notchoose = "att1",
  .lev_notchoose = "level1"
)
mm2 <- projoint(out1, qoi)
print(mm2)
summary(mm2)
```
Choice-Level AMCEs (Specific Level)
Suppose you want to quantify how the choice probability **changes** between the following profile pairs:
- choosing a profile that includes "40% of pre-tax income" (`level3`) for Housing Cost (`att1`) versus one that includes "15% of pre-tax income" (`level1`) for Housing Cost (`att1`); **and**
- [baseline] choosing a profile that includes "30% of pre-tax income" (`level2`) for Housing Cost (`att1`) versus one that includes "15% of pre-tax income" (`level1`) for Housing Cost (`att1`); 
averaging over all combinations of the other attributes and across respondents. Then write the following code:
```{r}
qoi <- set_qoi(
  .structure = "choice_level",
  .estimand = "amce",
  .att_choose = "att1",
  .lev_choose = "level3",
  .att_notchoose = "att1",
  .lev_notchoose = "level1",
  .att_choose_b = "att1",
  .lev_choose_b = "level2",
  .att_notchoose_b = "att1",
  .lev_notchoose_b = "level1"
)
amce2 <- projoint(out1, qoi)
print(amce2)
summary(amce2)
```
Predict IRR Using `predict_tau()`
```{r}
data(out1_arranged)
predicted_irr <- predict_tau(out1_arranged)
print(predicted_irr)
summary(predicted_irr)
plot(predicted_irr)
```
Estimate
- Specify your quantity of interest:
```{r}
qoi_mm <- set_qoi(
  .structure = "choice_level", # default
  .att_choose = "att1", 
  .lev_choose = "level1", 
  .att_notchoose = "att1", 
  .lev_notchoose = "level3"
)
```
- Estimate
```{r}
choice_mm <- projoint(
  .data = out1_arranged, 
  .qoi = qoi_mm, 
  .ignore_position = TRUE
)
```
 
Visualize (Levels) 
```{r, fig.width = 6, fig.height = 3}
plot(choice_mm)
```
 
Visualize (Differences)
```{r, fig.width = 8, fig.height = 2}
plot(choice_mm, .type = "pointrange")
```
 
---
🏠 **Home:** [Home](https://yhoriuchi.github.io/projoint/index.html)