This article explains how to automatically transform a standard Alida MaxDiff data export into a structure that can be analyzed in Q using Hierarchical Bayes or Latent Class Analysis.
Alida MaxDiff data is exported with one row per task and one variable for each alternative shown in that task:
The process outlined here transforms the Alida data by creating:
- A MaxDiff design matrix
- A respondent-level dataset with Most/least preferred variables for each task
This is the structure needed to run a MaxDiff analysis in Q.
Requirements
- A standard Alida MaxDiff data export.
Method
Step 1 - Import the Alida MaxDiff dataset into Q
Import your Alida MaxDiff data export into Q as you would any other dataset.
Step 2 - Create a Version variable
Next, we'll create a Version variable that assigns an incrementing version number to each respondent. This will be used to create the design and will also be included in the respondent-level dataset. We do this using a JavaScript variable:
- Go to the Variables and Questions tab.
- Right-click on a row header and select Insert Variable(s) > JavaScript Formula > Numeric.
- Check the Access all data rows (advanced) option.
- Change Label and Name to
Version. - Enter the code below.
- Click OK.
var result = new Array();
var version = 1;
var lastID = MemberID[0];
for (var i = 0; i < N; i++) {
if (MemberID[i] != lastID) {
version++;
lastID = MemberID[i];
}
result[i] = version;
}
resultStep 3 - Create the MaxDiff design
This step generates the MaxDiff design matrix from the Alida data:
- From the toolbar, select Create > R Output.
-
Copy and paste the following into the R Code section of the object inspector on the right:
version <- Version ##UPDATE##################################### notshownlabel <- 'Did not see' vals <- data.frame( ##UPDATE##################################### var1name, var2name, var3name, var4name, var5name, var6name, var7name, var8name, var9name, var10name ) altcount <- sum(vals[1, ] != notshownlabel) get_alts <- function(row) { indices <- which(row != notshownlabel) length(indices) <- altcount return(indices) } alts <- t(apply(vals, 1, get_alts)) colnames(alts) <- paste0("Alt", 1:altcount) result <- data.frame( Version = Version, Task = as.numeric(Set), alts ) result <- result[order(result$Version, result$Task), ] rownames(result) <- NULL md.design <- result - Update the value in the
notshownlabelline to the label of the alternative that was not shown to respondents for each task. "Did not see" is the standard Alida label for this option, but you can confirm by going to the Variables and Questions tab and clicking the Values button for any of the alternative variables. You can then see the labels for each option: - Update the
valssection with a comma-separated list of your alternative variable names. You can get this by going to the Variables and Questions tab and copying these from the Name column.
Important: Ensure that the last alternative name in the list does not have a comma at the end. - Click Calculate.
Step 4 - Create the most/least preferred dataset
This step generates the respondent-level most and least preferred variables for each task.
- From the toolbar menu, select Create > R Output.
-
Copy and paste the following into the R Code section of the object inspector on the right:
##UPDATE##################################### mostlabel = "Most likely to use" ##UPDATE##################################### leastlabel = "Least likely to use" ##UPDATE##################################### alt_names <- c( 'var1name', 'var2name', 'var3name', 'var4name', 'var5name', 'var6name', 'var7name', 'var8name', 'var9name', 'var10name' ) ##UPDATE##################################### raw <- data.frame( MemberID = MemberID, Set = Set, Version = Version, var1name = var1name, var2name = var2name, var3name = var3name, var4name = var4name, var5name = var5name, var6name = var6name, var7name = var7name, var8name = var8name, var9name = var9name, var10name = var10name ) n_questions <- max(as.numeric(Set), na.rm = TRUE) members <- unique(raw$MemberID) n_members <- length(members) col_names <- c('MemberID', 'Version') for (q in 1:n_questions) { col_names <- c(col_names, paste0('most', q), paste0('least', q)) } output <- data.frame(matrix(NA, nrow = n_members, ncol = length(col_names))) colnames(output) <- col_names for (m in 1:n_members) { mid <- members[m] member_rows <- raw[raw$MemberID == mid, ] output$MemberID[m] <- mid output$Version[m] <- member_rows$Version[1] for (q in 1:n_questions) { q_row <- member_rows[member_rows$Set == q, ] if (nrow(q_row) == 1) { most_val <- NA least_val <- NA for (a in 1:length(alt_names)) { col <- alt_names[a] if (col %in% colnames(q_row)) { val <- q_row[[col]] if (!is.na(val) && val == mostlabel) most_val <- a if (!is.na(val) && val == leastlabel) least_val <- a } } output[[paste0('most', q)]][m] <- most_val output[[paste0('least', q)]][m] <- least_val } } } md.data <- output - Update the value in the
mostlabelline to the label of the alternative that was selected as the most preferred. You can get this by going to the Variables and Questions tab and clicking the Values button for any of the alternative variables. - Update the value in the
leastlabelline to the label of the alternative that was selected as the least preferred. -
Update the
altnamessection with a list of comma-separated list of alternative variable names. You can get this by going to the Variables and Questions tab and copying these from the Name column.Tip: Make sure each variable name is wrapped in single quotes.
Tip: Ensure that the last alternative in the list does not have a comma at the end. -
Update the
rawsection with a comma-separated list of variable names, where each variable is in the form ofvarname = varname.Tip: Ensure that the last alternative in the list does not have a comma at the end. - Click Calculate to generate the most/least data table.
Step 5 - Export the most/least data table to Excel
Next, you'll need to export and save the respondent-level most/least data table to Excel:
- Select the
md.dataoutput. - Right-click and select Export to Office > Excel (or click the Excel icon in the toolbar).
- Confirm the following export settings:
- Format - To Excel
- Table Style - Unstyled
- Export to - New Workbook
- Use legacy exporter - unchecked
- Click OK.
- Navigate to the folder where you want to save the table.
- Enter a File name.
- Click Save.
Step 6 - Import the respondent-level data as a data set
We can now import the respondent table as a data set as follows:
- Select Data Set > Add to Project > From File.
- Navigate to and select the file exported in Step 5 above.
- Click Open.
- Click OK to Automatically detect data file structure.
Step 7 - Run the MaxDiff analysis
With the design and most/least variables in place, you are now ready to run the MaxDiff:
- Go to Create > Marketing > MaxDiff, then select either Hierarchical Bayes or Latent Class Analysis.
- In the
max.diffproperties, select the following inputs:-
Design - select
md.designfrom the design output created in Step 3 above. -
Add alternative labels - click this button to enter labels for each alternative starting in cell A2.
Tip: Copy the alternative labels from the Label column on the Variables and Questions tab. -
Version - select the
Versionvariable from the respondent most/least dataset. - Best selections - select the most1, most2, most3, etc.
- Least selections - select the least1, least2, least3, etc.
-
Design - select
- Click Calculate.