Takes a dataframe containing
id column
weight column
maxdiff answer columns
maxdiff display order columns
and instructions on how to find these columns. Returns a list containing maxdiff data in tidy tall format and item dictionary.
Usage
md_parse_data(
data,
id_col,
answer_cols,
display_order_cols,
weight_col = NULL,
weakest_value,
strongest_value,
strictness = "strict"
)Arguments
- data
A data.frame.
- id_col
Quoted or unquoted id column name.
- answer_cols, display_order_cols
Patterns of maxdiff answer and display order column names. Must contain "{block}" and "{item}" placeholders for block (or maxdiff task) and item index numbers correspondingly.
- weight_col
Quoted or unquoted weight column name.
- weakest_value, strongest_value
Answer value corresponding to weakest/strongest option.
- strictness
One of
"strict","ignore_missing_blocks"or"ignore_broken_blocks". Specify the level of strictness for data checks.with the default one,
"strict", error will be thrown if some respondents have less blocks than others or if some respondents have "broken" blocks. We determine the number of items that need to be in a block as the value that occurs most frequently among all blocks. Broken block is a block with either a different number of items or not exactly one strongest or weakest answer.with
"ignore_missing_blocks"– it is acceptable for some respondents to have less blocks than others.with
"ignore_broken_blocks"– broken blocks as well as missing blocks will not cause error. Broken blocks will be removed from analysis data.
Examples
if (interactive()) {
# glimpse at all vars to figure out answer and DO columns
dplyr::glimpse(example_md_data)
}
# look at one of the answer cols to determine weakest/strongest values
unique(example_md_data$a10_md_block_1)
#> <labelled<double>[3]>: Regardless of whether or not you agree with the statements, select the statements for which you have the strongest and weakest opinion.
#> [CurrentLoopNumber] of [TotalLoops] - block_10 - Affirmative action gives an unfair advantage to certain people based
#> [1] NA 4 1
#>
#> Labels:
#> value label
#> 1 Strongest opinion
#> 4 Weakest opinion
# strongest value: 1
# weakest value: 4
if (FALSE) { # \dontrun{
# will throw error because some respondents have fewer blocks
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
)
} # }
library(haven)
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"
)