This article describes how to transform MaxDiff preference share variables to use as the Alternatives input in TURF analysis.
Requirements
- A Displayr document with preference share variables either created from a MaxDiff analysis output in Displayr or included in the underlying data set.
Method
The way we'll get the highest-ranked alternative for each respondent is to work out which of the alternatives has the highest preference share for each respondent. Then replace the highest value with 1, and all others with a 0. We can do this with some R code.
When you have a MaxDiff analysis output in Displayr
This method assumes you have already run a MaxDiff analysis from the toolbar via > Advanced Analysis > MaxDiff > Hierarchical Bayes / Latent Class Analysis / Multinomial Logit.
- Select the MaxDiff output you want to use in the TURF analysis.
- In the object inspector on the right, click Data > Save Variable(s) > Preference Shares. This saves out the preference share data we'll need.
- Find the preference share variable set in the Data Sources tree.
- Click the small arrow next to the variable set to expand it:
- Select the first variable in the list. In my screenshot above it's the "Apple" variable.
- In the object inspector, go to the Data > R Code - Data box.
- Set the first line equal to "shares" like this:
-
Then paste in the code below. You can skip the first 3 lines, just paste everything from "#find the max value across all attributes" on down.
shares = prop.table(exp(as.matrix((flipMaxDiff::RespondentParameters(max.diff.2)))), 1)
#set first line equal to "shares".
#Do not overwrite the code that was already there.
# find the max value across all attributes
z <- apply(shares[, 1:ncol(shares)], 1, max)
# substitute the maximum value of each row with 1. Assign 0 to the others
max_turf <- ifelse(shares == z, 1, 0)
#return data
max_turf - Click the red Calculate button at the top of the object inspector.
- Next, go back to the Data Sources tree and select the top of the variable set again:
- With it selected, go to the General tab of the object inspector.
- Give this modified variable set a new Label
-
From the Data tab, update the Structure dropdown to Binary - Multi
- Click the Fix button in the orange warning that appears in the object inspector.
- Click Proceed Regardless.
- Click OK.
Now you can use this variable set as the input variables in your TURF.
When you have imported preference share variables in your data set
- Click the plus sign
when you hover over any variable in the Data Sources tree
- From the Insert Variable(s) menu, select Custom Code > Multiple R Variables > Numeric to create a Numeric - Multi variable set.
- Type the number of variables you want to create in the How Many Variables Do You Want to Create? box to match the number of attributes in your preference share variables.
- Click OK
- Press R Code - Data > Edit Code
- Replace the example code with the following but update the variable set label reference in line 2 to match the label of your preference share variables:
# get the preference shares
shares <- `MaxDiff Preference Shares`
# remove SUM column
shares <- shares[,colnames(shares) != "SUM"]
# find the max value across all attributes
z <- apply(shares[, 1:ncol(shares)], 1, max)
# substitute the maximum value of each row with 1. Assign 0 to the others
max_turf <- ifelse(shares == z, 1, 0) - Press Calculate.
- Follow steps 11 to 16 from the previous method.
Next
How to Save Respondent-Level Preference Shares from a MaxDiff Latent Class Analysis