## Mikael Poul Johannesson
## 2018
## Start matter ------------------------------------------------------
library(here)
library(haven)
library(tidyverse)
if (!require(selshare)) {
devtools::install_github("mikajoh/selshare")
}
## Load data ---------------------------------------------------------
## Prepared data from the Norwegian Citizen panel on the respondents
## from wave 1 through 7. Generated by `01_data_rsp.R`.
## Md5sum: 93fe0067d8bdc6f129dceddb11c65f0c
## tools::md5sum(here("data", "w17_rsp_data.csv"))
w17 <- read.csv(
here("data", "ncp_rsp_w17.csv"),
stringsAsFactors = FALSE
)
## The Norwegian Citizen Panel, Wave 8
## Md5sum: 81849e3f0f8f056ee4f6406a8ac69b40
## tools::md5sum(here("raw", "Norwegian Citizen Panel - wave 8 EN.sav"))
w8_raw <- read_sav(
here("raw", "Norwegian Citizen Panel - wave 8 EN.sav")
)
## Prep party favorability -------------------------------------------
rsp_party <-
w17 %>%
select(rsp_id, matches("rsp_like_")) %>%
gather(rsp_party, like, matches("rsp_like_"), na.rm = TRUE) %>%
mutate(
rsp_party = case_when(
rsp_party == "rsp_like_ap" ~ "Labour Party (CL)",
rsp_party == "rsp_like_frp" ~ "Progress Party (FR)",
rsp_party == "rsp_like_h" ~ "Conservative Party (CR)",
rsp_party == "rsp_like_krf" ~ "Christian Democrats (C)",
rsp_party == "rsp_like_mdg" ~ "Green Party (C)",
rsp_party == "rsp_like_rodt" ~ "Red Party (FL)",
rsp_party == "rsp_like_sp" ~ "Agrarian Party (C)",
rsp_party == "rsp_like_sv" ~ "Socialist Left Party (L)",
rsp_party == "rsp_like_v" ~ "Liberal Party (C)")
)
## Prep wave 8 experiment --------------------------------------------
## `rsp_` denotes respondent-level information.
w8_01 <-
w8_raw %>%
mutate(
rsp_id = as.numeric(responseid),
rsp_age = as.numeric(r8P5_1),
rsp_age_cat = case_when(
r8P5_2 == 1 ~ "18-29 yrs",
r8P5_2 == 2 ~ "30-59 yrs",
r8P5_2 == 3 ~ "60 yrs and above"),
rsp_gender = case_when(
r8P1 == 1 ~ "Male",
r8P1 == 2 ~ "Female"),
rsp_edu = case_when(
r8B3_1 %in% 1:2 ~ "Lower or intermediate",
r8B3_1 == 3 ~ "Higher"),
rsp_polint = case_when(
r8k1 %in% 1:5 ~ 6 - as.numeric(r8k1)),
rsp_party = case_when(
r8k204 == 1 ~ "Christian Democratic Party",
r8k204 == 2 ~ "Conservative Party",
r8k204 == 3 ~ "Progress Party",
r8k204 == 4 ~ "Liberal Party",
r8k204 == 5 ~ "Socialist Left Party",
r8k204 == 6 ~ "Agrarian Party",
r8k204 == 7 ~ "Green Party",
r8k204 == 8 ~ "Social Democratic Party",
r8k204 == 9 ~ "Red Party"),
rsp_polscale = case_when(
r8k8_1 %in% 1:11 ~ as.numeric(r8k8_1)),
rsp_polside = case_when(
rsp_polscale %in% 1:5 ~ "Left",
rsp_polscale == 6 ~ "Centre",
rsp_polscale %in% 7:11 ~ "Right"),
rsp_trust = case_when(
r8k15 %in% 1:10 ~ as.numeric(r8k15)),
rsp_att_punish = as.numeric(r8straff),
rsp_att_growth = as.numeric(r8vekst),
rsp_att_redist = as.numeric(r8storstat),
rsp_att_selfeffort = as.numeric(r8egeninnsats),
rsp_att_gayrights = as.numeric(r8parlikhet),
rsp_att_eu = as.numeric(r8euint),
rsp_att_imm = as.numeric(r8innvandring),
rsp_para_interface = case_when(
r8enhetstype %in% c(1, 3) ~ "pc or generic",
r8enhetstype == 2 ~ "touch"),
rsp_para_device = case_when(
r8mobil == 1 ~ "Did not use smart phone",
r8mobil == 2 ~ "Used smart phone"),
) %>%
mutate_at(
vars(matches("rsp_att_")),
function(x) ifelse(x %in% 1:7, 8 - x, NA)
)
w8_02 <-
w8_01 %>%
gather(exp_version, exp_post, r8pad3a, r8pad3b, r8pad3c, na.rm = TRUE) %>%
mutate(
exp_post = ifelse(exp_post %in% 97:98, NA, exp_post),
exp_type = case_when(
grepl("anbefale", r8pad3_qtextran_0) ~ "Share",
grepl("lese", r8pad3_qtextran_0) ~ "Read"),
exp_version = case_when(
exp_version == "r8pad3a" ~ "Person-only",
exp_version == "r8pad3b" ~ "Shares-only",
exp_version == "r8pad3c" ~ "Both"),
) %>%
select(-r8pad3_qtextran_0) %>%
filter(!is.na(exp_post)) %>%
select(matches("rsp_"), matches("exp_"), matches("r8pad3")) %>%
select(-r8pad3_ran)
## The raw experiment data comes in the form of one variable for each
## cell of the conjoint table. The `r8pad3_dimrantext_\\d` variables
## are the left side treatment headers as shown to respondents;
## `selectivesharing::treat_names_w8()` returns a df which matches these
## headers with the var names we want.
w8_03 <-
w8_02 %>%
select(matches("rsp_"), matches("exp_"), matches("r8pad3")) %>%
gather(
row_treat, treat_lab,
matches("r8pad3_dimrantext_"),
na.rm = TRUE
) %>%
left_join(treat_names_w8(), by = "treat_lab") %>%
filter(!is.na(treat)) %>%
mutate(
row_treat = gsub("^.*(\\d)$", "\\1", row_treat),
row_treat = as.numeric(row_treat)
)
## The `r8pad3_dimranlabel(\\d)_(\\d)` variables shows which value
## label were shown in which \\1 column and \\2 row of the conjoint
## table. `selectivesharing::val_labs_w8()` returns a df which matches
## these value labels (and prev retrieved treatment names) to new
## value labels in english. Also note that `exp_` denotes
## experiment-level information and `prs_` denotes decision-level
## information.
w8_04 <-
w8_03 %>%
gather(row_val, val_no, matches("r8pad3_dimranlabel")) %>%
mutate(
row = gsub("^r8pad3_dimranlabel(\\d)_(\\d)$", "\\2", row_val),
row = as.numeric(row),
prs_n = gsub("^r8pad3_dimranlabel(\\d)_(\\d)$", "\\1", row_val),
prs_n = as.numeric(prs_n)
) %>%
filter(row == row_treat) %>%
left_join(val_labs_w8(), by = c("treat", "val_no")) %>%
select(-matches("row"), -treat_lab, -val_no) %>%
spread(treat, val) %>%
mutate(
prs_post = case_when(
exp_post == prs_n ~ 1,
exp_post != prs_n ~ 0),
prs_party_match = case_when(
prs_party == rsp_party ~ "Preferes the same party",
prs_party != rsp_party ~ "Preferes different parties"),
prs_shr_party_match = case_when(
prs_shr_party == rsp_party ~ "Mostly mentioned preferred party",
prs_shr_party != rsp_party ~ "Did not mostly mention prefered party")
) %>%
select(matches("rsp_"), matches("exp_"), matches("prs_")) %>%
filter(!is.na(prs_post))
## To get data on favorability (like/dislike party mentioned) we
## attach data from waves 1 through 7 of the NCP.
w8_05 <-
w8_04 %>%
left_join(
rsp_party %>% rename(prs_party = rsp_party),
by = c("rsp_id", "prs_party")
) %>%
rename(prs_party_like = like) %>%
left_join(
rsp_party %>% rename(prs_shr_party = rsp_party),
by = c("rsp_id", "prs_shr_party")
) %>%
rename(prs_shr_party_like = like)
w8 <- w8_05
## Write data to file ------------------------------------------------
write.csv(
w8,
file = here("data", "ncp_exp_w8.csv"),
row.names = FALSE
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.