Skip to contents

Introduction

MaxDiff data can be exported in different formats depending on the survey platform, experimental design, and export settings. To analyze it using the g6tr.md package, the data must first be transformed into a specific tidy format.

This vignette focuses on preparing MaxDiff data exported from Qualtrics, though the process may differ for other survey platforms.

The md_parse_data() function is designed to convert Qualtrics exports into the required tidy format for modeling functions. While it may work for similar data structures, we cannot guarantee compatibility with other sources. If your data comes from a different platform, you may need to manually reshape it using dplyr or other tools to match the required format.

Survey data

We start with a dataframe which contains the following columns:

  • A response id column.

  • (optional) A column containing case weights.

  • A set of display order columns: These columns, numbering N_B * N_I, where N_B is the number of blocks (i.e. “tasks”) and N_I is the number of items in our MD experiment, record the order of item display. If item i appears in block b, the respective column marks its sequential position, whereas an NA is recorded for items not shown. Considering typically around 5 items are displayed per block from a pool of 15 or more, for each response a majority of these columns will contain NA.

  • A set of answer columns: with a similar structure as display order columns, but containing answers. In each block one item must be selected as strongest (most important/valuable/…) and one as weakest (least …). Items shown but not selected as either may or may not be marked in the data, depending on the survey programming, so answer columns will be categorical variables with either 3 levels (strongest/weakest/shown, NA for not shown) or 2 levels (strongest/weakest, NA for not selected or not shown).

By default, Display Order columns are not part of the Qualtrics survey export, and thus, require explicit request. Use g6tr.survey::srv_get_data() with display_order = TRUE to include them.

Parsing the data

Column names and value coding will vary from survey to survey. Before running MD models, the data needs to be brought to a standard format. We do so with the help of the md_parse_data() function by essentially providing it with instructions on how to find all the MD data described in the previous paragraph. Let’s do it step-by-step on an example data provided with the package: example_md_data.

  • The Standard name for a response id in our surveys is response_id. This should be assigned to the id_col parameter of the function, it can be quoted or unquoted.
  • We don’t have a weights column so we can leave the corresponding parameter, weight_col, untouched. It will be assumed that all case weights are equal to 1.
  • The names of the Display Order and Answer columns should adhere to a specific pattern that incorporates information about the block number and the item number. For each of the two column sets we need to provide this pattern.

Here’s how we can identify these columns and recognize the patterns in practice. Typically all MaxDiff-related columns contain "_md_" in their names, so let’s extract all these columns and look at their names.

example_md_data |>
  select(contains("_md_")) |>
  names() |>
  head(60)
#>  [1] "a1_md_block_1"     "a1_md_block_2"     "a1_md_block_3"    
#>  [4] "a1_md_block_4"     "a1_md_block_5"     "a1_md_block_6"    
#>  [7] "a1_md_block_7"     "a1_md_block_8"     "a1_md_block_9"    
#> [10] "a1_md_block_10"    "a1_md_block_11"    "a1_md_block_12"   
#> [13] "a1_md_block_13"    "a1_md_block_14"    "a1_md_block_15"   
#> [16] "a1_md_block_16"    "a1_md_block_17"    "a1_md_block_18"   
#> [19] "a1_md_block_19"    "a1_md_block_20"    "a1_md_block_21"   
#> [22] "a1_md_block_22"    "a1_md_block_23"    "a1_md_block_24"   
#> [25] "a1_md_block_25"    "a1_md_block_26"    "a1_md_block_do_1" 
#> [28] "a1_md_block_do_2"  "a1_md_block_do_3"  "a1_md_block_do_4" 
#> [31] "a1_md_block_do_5"  "a1_md_block_do_6"  "a1_md_block_do_7" 
#> [34] "a1_md_block_do_8"  "a1_md_block_do_9"  "a1_md_block_do_10"
#> [37] "a1_md_block_do_11" "a1_md_block_do_12" "a1_md_block_do_13"
#> [40] "a1_md_block_do_14" "a1_md_block_do_15" "a1_md_block_do_16"
#> [43] "a1_md_block_do_17" "a1_md_block_do_18" "a1_md_block_do_19"
#> [46] "a1_md_block_do_20" "a1_md_block_do_21" "a1_md_block_do_22"
#> [49] "a1_md_block_do_23" "a1_md_block_do_24" "a1_md_block_do_25"
#> [52] "a1_md_block_do_26" "a2_md_block_1"     "a2_md_block_2"    
#> [55] "a2_md_block_3"     "a2_md_block_4"     "a2_md_block_5"    
#> [58] "a2_md_block_6"     "a2_md_block_7"     "a2_md_block_8"

So we have two sets of columns, "a{number}_md_block_{number}" and "a{number}_md_block_do_{number}". Clearly, the latter is the set of display order columns and the former is, accordingly – answer columns. But let’s print a column from each set to confirm this and to identify which of the numbers in the patterns corresponds to the block and which – to the item.

example_md_data$a2_md_block_do_13
#>   [1] NA NA NA NA NA NA  3 NA NA  5 NA NA NA NA NA NA NA NA NA  1 NA  2  4 NA NA
#>  [26]  2 NA NA NA NA NA NA NA  3 NA  5 NA NA NA NA  4 NA NA NA NA  5 NA NA NA NA
#>  [51] NA NA NA  1 NA NA NA NA NA  5 NA  3 NA NA NA NA NA  2 NA NA NA NA NA NA NA
#>  [76] NA NA  1 NA NA NA NA  3 NA NA NA NA NA  5 NA NA  1 NA NA  1 NA NA NA  2 NA
#> [101] NA NA  1 NA NA  1 NA NA NA NA  5  4 NA NA NA NA NA NA NA NA NA NA NA NA NA
#> [126] NA NA NA NA NA NA NA NA  1  1 NA NA NA NA NA NA NA  4 NA  1  3 NA NA NA NA
#> [151] NA NA NA NA NA NA NA NA NA  5 NA NA NA NA NA  5 NA NA NA  1  2 NA NA NA NA
#> [176]  2 NA NA NA NA  5 NA NA  5 NA NA NA NA NA NA NA NA NA NA NA NA NA  5 NA NA
#> [201] NA NA  1 NA NA NA NA NA NA NA NA NA  4 NA NA NA NA  4 NA NA
#> attr(,"label")
#> [1] "Regardless of whether or not you agree with the statements, select the statements for which you have the strongest and weakest opinion. \n[CurrentLoopNumber] of  [TotalLoops] - Display Order It is essential for a healthy democracy that Americans engage with"
#> attr(,"format.spss")
#> [1] "F40.0"
example_md_data$a2_md_block_13
#> <haven_labelled[220]>
#>   [1] NA NA NA NA NA NA NA NA NA  1 NA NA NA NA NA NA NA NA NA  4 NA NA NA NA NA
#>  [26]  1 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA  1 NA NA NA NA
#>  [51] NA NA NA  1 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
#>  [76] NA NA  1 NA NA NA NA NA NA NA NA NA NA NA NA NA  1 NA NA  1 NA NA NA  4 NA
#> [101] NA NA  1 NA NA  4 NA NA NA NA NA  1 NA NA NA NA NA NA NA NA NA NA NA NA NA
#> [126] NA NA NA NA NA NA NA NA NA  1 NA NA NA NA NA NA NA  4 NA NA NA NA NA NA NA
#> [151] NA NA NA NA NA NA NA NA NA  1 NA NA NA NA NA NA NA NA NA  1  1 NA NA NA NA
#> [176] NA NA NA NA NA NA NA NA  1 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
#> [201] NA NA  4 NA NA NA NA NA NA NA NA NA NA NA NA NA NA  1 NA NA

Indeed the _do_ column contains item order numbers from 1 to 5 and the other column contains either 1 corresponding to “Strongest opinion” or 4 – “Weakest opinion”. Also, from the column’s label we see that the first number in the name corresponds to the block, then the second must be the item number. To signify this in the patterns we need to use {block} and {item} placeholders, that is: "a{block}_md_block_{item}" and "a{block}_md_block_do_{item}". Altogether, recalling the strongest/weakest answer coding we get the following call:

example_md_parsed_data <-
  md_parse_data(
    example_md_data,
    id_col = response_id,
    answer_cols = "a{block}_md_block_{item}",
    display_order_cols = "a{block}_md_block_do_{item}",
    strongest_value = 1,
    weakest_value = 4
  )
#> Error in `pivot_longer()`:
#> ! Can't combine `a1_md_block_1` <haven_labelled> and `a1_md_block_2` <haven_labelled>.
#> ✖ Some attributes are incompatible.
#> ℹ The author of the class should implement vctrs methods.
#> ℹ See <https://vctrs.r-lib.org/reference/faq-error-incompatible-attributes.html>.

Strictness levels

We did everything correctly but still got an error. Generally, the fact that some respondents have fewer responses recorded than others wouldn’t be a problem for our model. But it’s always a good idea to stop here and try to figure out why this happened. Therefore, the default is the most “strict” behavior of the parsing function. To suppress the error, as you may have noticed from the message, you can pass “ignore_missing_blocks” to the strictness parameter.

example_md_parsed_data <-
  md_parse_data(
    example_md_data,
    id_col = response_id,
    answer_cols = "a{block}_md_block_{item}",
    display_order_cols = "a{block}_md_block_do_{item}",
    strongest_value = 1,
    weakest_value = 4,
    strictness = "ignore_missing_blocks" 
  )

The above call should run smoothly. However, in principle it may happen that some respondents have blocks that are wrongly formatted in one way or another. This may be different number of items per block, or not exactly one strongest or weakest item selected. This, in turn, will be a problem for the model and with the default or “ignore_missing_blocks” strictness will throw an error. Use “ignore_broken_blocks” strictness to suppress it. Such “broken” data can not be used in the model, so it will be removed but you will be able to continue with conducting the analysis.

Modifying parsed data

Let’s take a look at the parsed data object. It is a data frame where the item column is stored as a factor, with its levels representing the item labels:

example_md_parsed_data
#> # A tibble: 15,475 × 8
#>    id       weight block display_order item  answer answer_col display_order_col
#>    <chr>     <dbl> <int>         <dbl> <fct> <fct>  <chr>      <chr>            
#>  1 R_0BySp…      1     1             1 Amer… stron… a1_md_blo… a1_md_block_do_7 
#>  2 R_0BySp…      1     1             2 The … none   a1_md_blo… a1_md_block_do_17
#>  3 R_0BySp…      1     1             3 Affi… weake… a1_md_blo… a1_md_block_do_1 
#>  4 R_0BySp…      1     1             4 The … none   a1_md_blo… a1_md_block_do_6 
#>  5 R_0BySp…      1     1             5 It i… none   a1_md_blo… a1_md_block_do_13
#>  6 R_0BySp…      1     2             1 The … none   a2_md_blo… a2_md_block_do_5 
#>  7 R_0BySp…      1     2             2 It i… stron… a2_md_blo… a2_md_block_do_13
#>  8 R_0BySp…      1     2             3 Univ… none   a2_md_blo… a2_md_block_do_12
#>  9 R_0BySp…      1     2             4 The … weake… a2_md_blo… a2_md_block_do_19
#> 10 R_0BySp…      1     2             5 Ensu… none   a2_md_blo… a2_md_block_do_9 
#> # ℹ 15,465 more rows

If you need to rename or reorder items, you can modify the factor levels, for example, using forcats. Additionally, you may want to filter the data — such as removing respondents with fewer than 10 blocks:

example_md_parsed_data <-
  example_md_parsed_data |>
  group_by(id) |>
  filter(n_distinct(block) >= 10) |>
  ungroup()