The following scripts can be used to replicate the data-set of Franken, Bekhuis, and Tolsma (2024). It may also be obtained by downloading: Download conjoint.RDa
To copy the code, click the button in the upper right corner of the code-chunks.
rm(list = ls())
gc()
fpackage.check
: Check if packages are installed (and
install if not) in Rfsave
: Function to save data with time stamp in correct
directoryfload
: Load R-objects under new namesfshowdf
: Print objects (tibble
/
data.frame
) nicely on screen in .Rmd
.fpackage.check <- function(packages) {
lapply(packages, FUN = function(x) {
if (!require(x, character.only = TRUE)) {
install.packages(x, dependencies = TRUE)
library(x, character.only = TRUE)
}
})
}
fsave <- function(x, file, location = "./data/processed/", ...) {
if (!dir.exists(location))
dir.create(location)
datename <- substr(gsub("[:-]", "", Sys.time()), 1, 8)
totalname <- paste(location, datename, file, sep = "")
print(paste("SAVED: ", totalname, sep = ""))
save(x, file = totalname)
}
fload <- function(fileName) {
load(fileName)
get(ls()[ls() != "fileName"])
}
fshowdf <- function(x, digits = 2, ...) {
knitr::kable(x, digits = digits, "html", ...) %>%
kableExtra::kable_styling(bootstrap_options = c("striped", "hover")) %>%
kableExtra::scroll_box(width = "100%", height = "300px")
}
tidyverse
: data wranglingreshape2
: reshaping datahaven
: read and write various data formatssjlabelled
: work with labelled (SPSS) datapackages = c("tidyverse", "haven", "sjlabelled", "reshape2")
fpackage.check(packages)
rm(packages)
All data from the ‘Transition Into Active Living’ (TRIAL) study will be deposited in DANS Data Station SSH at a later time (Bekhuis et al. 2021).
For now, we share parts of the data (i.e., conjoint data, important background/moderator variables): Download trial_part.Rda
Download the data-file, and put it in the ./data/
folder. But first, make a ./data/
folder:
ifelse(!dir.exists("data"), dir.create("data"), FALSE)
Load in the downloaded data file.
df <- fload("./data shared/trial_part.Rda")
# interaction variables current involvement df$V5
df$activeW2 <- ifelse(df$V5 == 1, "no", "yes")
# prop.table(table(df$activeW2, useNA = 'always')) #64 percent currently active, at w2
# self report sports frequency
sport1 <- ifelse(df$V6a == 1, 7, ifelse(df$V6a == 2, 4, ifelse(df$V6a == 3, 1.5, ifelse(df$V6a == 4,
0.5, NA))))
sport2 <- ifelse(df$V6b == 1, 7, ifelse(df$V6b == 2, 4, ifelse(df$V6b == 3, 1.5, ifelse(df$V6b == 4,
0.5, NA))))
sport3 <- ifelse(df$V6c == 1, 7, ifelse(df$V6c == 2, 4, ifelse(df$V6c == 3, 1.5, ifelse(df$V6c == 4,
0.5, NA))))
df$sportsfreq <- NA
df$sportsfreq[which(df$activeW2 == "yes")] <- rowSums(cbind(sport1, sport2, sport3)[which(df$activeW2 ==
"yes"), ], na.rm = TRUE)
# table(df$sportsfreq) cap at 14, only 1 higher.
df$sportsfreq <- ifelse(df$sportsfreq > 14, 14, df$sportsfreq)
# psych::describe(df$sportsfreq)
# gender
df$gender <- ifelse(df$GESLACHT2 == 1, "man", ifelse(df$GESLACHT2 == 2, "woman", "other"))
# attach id
df$id <- 1:nrow(df)
# remove columns we won't need:
df %>%
select(-c(V5:GESLACHT2)) -> df
names(df)
# to long format
# each list element contains the choices for each set. thus presatielist[[2]][3] is the
# 'presatie'-attribute of person 3, in choice set 2
prestatielist <- list()
prestatielist[[1]] <- c("LoopKeuzesets_1_LoopAlternatieven_1_Prestaties", "LoopKeuzesets_1_LoopAlternatieven_2_Prestaties",
"LoopKeuzesets_1_LoopAlternatieven_3_Prestaties")
prestatielist[[2]] <- c("LoopKeuzesets_2_LoopAlternatieven_1_Prestaties", "LoopKeuzesets_2_LoopAlternatieven_2_Prestaties",
"LoopKeuzesets_2_LoopAlternatieven_3_Prestaties")
prestatielist[[3]] <- c("LoopKeuzesets_3_LoopAlternatieven_1_Prestaties", "LoopKeuzesets_3_LoopAlternatieven_2_Prestaties",
"LoopKeuzesets_3_LoopAlternatieven_3_Prestaties")
kennislist <- list()
kennislist[[1]] <- c("LoopKeuzesets_1_LoopAlternatieven_1_Kennis", "LoopKeuzesets_1_LoopAlternatieven_2_Kennis",
"LoopKeuzesets_1_LoopAlternatieven_3_Kennis")
kennislist[[2]] <- c("LoopKeuzesets_2_LoopAlternatieven_1_Kennis", "LoopKeuzesets_2_LoopAlternatieven_2_Kennis",
"LoopKeuzesets_2_LoopAlternatieven_3_Kennis")
kennislist[[3]] <- c("LoopKeuzesets_3_LoopAlternatieven_1_Kennis", "LoopKeuzesets_3_LoopAlternatieven_2_Kennis",
"LoopKeuzesets_3_LoopAlternatieven_3_Kennis")
gezelliglist <- list()
gezelliglist[[1]] <- c("LoopKeuzesets_1_LoopAlternatieven_1_Gezellig", "LoopKeuzesets_1_LoopAlternatieven_2_Gezellig",
"LoopKeuzesets_1_LoopAlternatieven_3_Gezellig")
gezelliglist[[2]] <- c("LoopKeuzesets_2_LoopAlternatieven_1_Gezellig", "LoopKeuzesets_2_LoopAlternatieven_2_Gezellig",
"LoopKeuzesets_2_LoopAlternatieven_3_Gezellig")
gezelliglist[[3]] <- c("LoopKeuzesets_3_LoopAlternatieven_1_Gezellig", "LoopKeuzesets_3_LoopAlternatieven_2_Gezellig",
"LoopKeuzesets_3_LoopAlternatieven_3_Gezellig")
esteemlist <- list()
esteemlist[[1]] <- c("LoopKeuzesets_1_LoopAlternatieven_1_Aanmoediging", "LoopKeuzesets_1_LoopAlternatieven_2_Aanmoediging",
"LoopKeuzesets_1_LoopAlternatieven_3_Aanmoediging")
esteemlist[[2]] <- c("LoopKeuzesets_2_LoopAlternatieven_1_Aanmoediging", "LoopKeuzesets_2_LoopAlternatieven_2_Aanmoediging",
"LoopKeuzesets_2_LoopAlternatieven_3_Aanmoediging")
esteemlist[[3]] <- c("LoopKeuzesets_3_LoopAlternatieven_1_Aanmoediging", "LoopKeuzesets_3_LoopAlternatieven_2_Aanmoediging",
"LoopKeuzesets_3_LoopAlternatieven_3_Aanmoediging")
chosen <- c("V84_LoopKeuzesets_1", "V85_LoopKeuzesets_2", "V86_LoopKeuzesets_3")
for (set in 1:3) {
# to long format
for (choice in 1:3) {
data <- as.data.frame(df)
data$set <- set
data$options <- choice
data$comparison <- data[, names(data) == prestatielist[[set]][choice]]
data$knowledge <- data[, names(data) == kennislist[[set]][choice]]
data$companionship <- data[, names(data) == gezelliglist[[set]][choice]]
data$encouragement <- data[, names(data) == esteemlist[[set]][choice]]
data$chosen <- data[, names(data) == chosen[set]]
data <- data[, names(data) %in% c("id", "gender", "activeW2", "sportsfreq", "set", "options",
"comparison", "knowledge", "companionship", "encouragement", "chosen")]
if (set == 1 & choice == 1) {
df_long <- data
} else {
df_long <- rbind(df_long, data)
}
}
}
# order
df_long <- df_long[order(df_long$id, df_long$set, df_long$options), ]
# define the choice
df_long$choice <- (df_long$chosen == c(1, 2, 3)[df_long$options])
# recode number into answer
df_long$comparison <- ifelse(df_long$comparison == 1, "really likes to compare sports performances",
ifelse(df_long$comparison == 2, "somewhat likes to compare sports performances", ifelse(df_long$comparison ==
3, "does not like to compare sports performances", NA)))
df_long$knowledge <- ifelse(df_long$knowledge == 1, "knows more than you about effective training and the right technique",
ifelse(df_long$knowledge == 2, "knows as much as you about effective training and the right technique",
ifelse(df_long$knowledge == 3, "knows less than you about effective training and the right technique",
NA)))
df_long$companionship <- ifelse(df_long$companionship == 1, "exercises to socially engage", ifelse(df_long$companionship ==
2, "wants a combination of social interaction and purposeful training", ifelse(df_long$companionship ==
3, "exercises purposefully and seriously", NA)))
df_long$encouragement <- ifelse(df_long$encouragement == 1, "always encourages you", ifelse(df_long$encouragement ==
2, "sometimes encourages you", ifelse(df_long$encouragement == 3, "never encourages you", NA)))
row.names(df_long) <- 1:nrow(df_long)
# remove 'chosen' and 'options' variable
data <- df_long[, -c(6, 11)]
# recode Y
data$choice <- ifelse(data$choice == TRUE, 1, 0)
# convert features to factor level names(data)
cols_to_convert <- c("comparison", "knowledge", "companionship", "encouragement", "activeW2", "gender",
"set")
data[cols_to_convert] <- lapply(data[cols_to_convert], as.factor)
# re-order
data$comparison <- factor(data$comparison, levels = c("really likes to compare sports performances",
"somewhat likes to compare sports performances", "does not like to compare sports performances"))
data$knowledge <- factor(data$knowledge, levels = c("knows more than you about effective training and the right technique",
"knows as much as you about effective training and the right technique", "knows less than you about effective training and the right technique"))
data$companionship <- factor(data$companionship, levels = c("exercises to socially engage", "wants a combination of social interaction and purposeful training",
"exercises purposefully and seriously"))
data$encouragement <- factor(data$encouragement, levels = c("always encourages you", "sometimes encourages you",
"never encourages you"))
# fix(data)
fsave(data, "conjoint.Rda")
Copyright © 2025 Rob Franken