Skip to content

Instantly share code, notes, and snippets.

@bgall
Last active December 1, 2019 20:46
Show Gist options
  • Save bgall/f980b4ca832ed3aac3ceb5d318d29f2d to your computer and use it in GitHub Desktop.
Save bgall/f980b4ca832ed3aac3ceb5d318d29f2d to your computer and use it in GitHub Desktop.
Creates "skeleton" data frame of conjoint parameters.
#########################################################
# Define function: create_design_df
#
# Description: Generates a data frame with participant
# IDs, choice set ID, profile ID, for specified numbers of
# participants, choice tasks per participant, and profiles
# per choice tasks
#
# Arguments:
# N = # of participants
# K = # choice tasks observed
# W = # of profiles per choice task
#########################################################
create_design_df <- function(N, K, W) {
temp <- data.frame(
participant_id = sort(rep(seq_len(N), K * W)),
screen_id = sort(rep(seq_len(K * N), W)),
profile_id = seq_len(N * K * W)
)
# Add first_profile == 1 if first in profile set, 0 otherwise
temp %>%
dplyr::group_by(screen_id) %>%
dplyr::mutate(first_profile = ifelse(row_number() == 1, 1, 0)) %>%
dplyr::ungroup()
}
#########################################################
# Example
#########################################################
#
# Generate example parameter values
# N = 200
# K = 2
# W = 2
#
# Create design data
# create_design_df(N = N, K = K, W = W)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment