This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
# Wrap data simulation in a function | |
sim_data <- function(N) { | |
########################################################################### | |
# parameters | |
########################################################################### | |
set.seed(123) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Compare the performance of two purportedly-fast ways of generating dummy variables from character vector. | |
# NOTE: fastDummies retains the original variable by default while modeldb does not | |
# Dependencies | |
library(microbenchmark) | |
library(fastDummies) | |
library(modeldb) | |
library(dplyr) | |
# Simulate data: 1 million rows, 1 variable with 26 unique values |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set.seed(123) | |
############################################## | |
# Study parameters | |
############################################## | |
# Sample size | |
n <- 63 | |
# Mean of outcome |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################ | |
# Define function: create_predictors | |
# Creates randomly generated data sets containing | |
# all design variables, attributes, and dummy | |
# variables for the right-hand side of the conjoint | |
# analysis (excludes the outcome variable), | |
# based on specified data parameter values. | |
# Data are "long," sucht hat each row of the data | |
# is a conjoint profile. | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################### | |
# Define function: create_value_dummies | |
# Create dummy variables indicating if a given | |
# profile takes on a specific value from its | |
# possible values. Create a dummy for the range | |
# of the variable, independent of whether the actual | |
# random sampling does (not) draw that value and assign | |
# it to a profile. | |
######################################################### |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################## | |
# Define function: create_attributes | |
# | |
# Creates randomly generated vectors of values from sets | |
# of potential values for a specified number of attributes | |
# | |
# *Arguments* | |
# | |
# attr_names (optional) | |
# vector of attribute names of attr_n length. If no values |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################### | |
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################################### | |
# 'loadpkg' function | |
# Checks if a vector of packages are installed. If not, installs the | |
# package. Then loads all packages in vector. | |
######################################################################### | |
loadpkg <- function(toLoad){ | |
for(lib in toLoad){ | |
if(! lib %in% installed.packages()[,1]) { | |
install.packages(lib, repos='http://cran.rstudio.com/') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################################################### | |
# This gist contains a quick walk-through of several ways to produce scales capturing | |
# the average value of one or more variables. Each row (observation) gets its own | |
# value. We'll assume your data are not fully "tidy." What I mean by this is that you | |
# have an observation for each row and you want to calculate that observation's value | |
# on the scale, but each variable that should go into your scale is in its own column. | |
######################################################################################### | |
######################################################################################### | |
# Set-up (packages, fake data, etc.) |
NewerOlder