Skip to content

Commit

Permalink
small updates to cleaning and modeling
Browse files Browse the repository at this point in the history
  • Loading branch information
jhelvy committed Oct 26, 2021
1 parent 75688ff commit 3cac02e
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 222 deletions.
15 changes: 14 additions & 1 deletion code/data_cleaning.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ nrow(data)

# Create choice data ---------

# First gather the data
# First convert the data to long format
choiceData <- data %>%
pivot_longer(
cols = cbc1:cbc8,
Expand All @@ -136,5 +136,18 @@ choiceData <- choiceData %>%

head(choiceData)

# Create new values for respID & obsID
nRespondents <- nrow(data)
nAlts <- max(survey$altID)
nQuestions <- max(survey$qID)
choiceData$respID <- rep(seq(nRespondents), each = nAlts*nQuestions)
choiceData$obsID <- rep(seq(nRespondents*nQuestions), each = nAlts)

# Reorder columns - it's nice to have the "ID" variables first
choiceData <- choiceData %>%
select(ends_with("ID"), "choice", everything())

head(choiceData)

# Save cleaned data for modeling
write_csv(choiceData, here("data", "choiceData.csv"))
11 changes: 11 additions & 0 deletions code/install.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Install packages - only need to do this once!

# Install packages from CRAN
install.packages(c(
"fastDummies",
"here",
"janitor",
"logitr",
"remotes",
"tidyverse"
))
8 changes: 4 additions & 4 deletions code/modeling.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ data <- clean_names(data)

# Estimate the model
model <- logitr(
data = data,
choice = "choice",
obsID = "obs_id",
pars = c(
data = data,
outcome = "choice",
obsID = "obs_id",
pars = c(
"price",
"type_fuji", "type_gala", "type_honeycrisp", "type_pink_lady",
"freshness_average", "freshness_excellent")
Expand Down
Loading

0 comments on commit 3cac02e

Please sign in to comment.