#library
library(tidyverse)
#education recode ego function
feducation_ego <- function(x) {
x2 <- ifelse(x == 1, 6, x)
x3 <- ifelse(x == 2, 10, x2)
x4 <- ifelse(x == 3, 11.5, x3)
x5 <- ifelse(x == 4, 10.5, x4)
x6 <- ifelse(x == 5, 15, x5)
x7 <- ifelse(x == 6, 16, x6)
x8 <- ifelse(x == 7, NA, x7)
x9 <- ifelse(x == 8, 4, x8)
x10 <- ifelse(x == 9, NA, x9)
return(x10)
}
#education recode alter function
feducation_alter <- function(x) {
x2 <- ifelse(x == 1, 4, x)
x3 <- ifelse(x == 2, 6, x2)
x4 <- ifelse(x == 3, 10, x3)
x5 <- ifelse(x == 4, 11.5, x4)
x6 <- ifelse(x == 5, 10.5, x5)
x7 <- ifelse(x == 6, 15, x6)
x8<- ifelse(x == 7, 16, x7)
return(x8)
}
#event transition
event_transition_f <- function(event, option, time) {
if (option == "event") {
#for when we want 1, 0.5, 0.25.
y <- if_else(lag(event, 2) == 1, 0.25, 0)
y <- if_else(lag(event) == 1, 0.5, y)
y <- if_else(event == 1, 1, y)
y <- if_else(is.na(y) & !is.na(event), 0, y)
return(y)
}
if (option == "transition") {
#when it is a transition 00001111
y <- tibble(time, event) %>%
filter(event == 1)
if (nrow(y) == 0) {
y <- ifelse(!is.na(time) & !is.na(event), 0, NA)
return(y)
} else{
y <- y %>%
pull(time)
y <- if_else(time >= y, 1, 0)
return(y)
}
}
}
load("datafiles/data-processed/disaggregated_data/2023-06-12_liss-repeated-risk-data.rda")
load("datafiles/data-processed/common_data/0623_v5_liss_merged_core_file.rds")
Step 1: select the variables needed from the liss_long file and rename them accordingly. Step 2: use pivot_longer to make a long file. Step 3: perform data manipulation on the alter variables.
#------------------------------ Add ego and Alter Data ------------------------------#
#alter data
#get alter data.
alter_data <- liss_long %>%
select(
nomem_encr,
survey_wave,
starts_with("alter_id"),
starts_with("educ_alter"),
starts_with("age_alter"),
starts_with("gender_alter"),
starts_with("poltalk_alter"),
starts_with("talk_alter"),
starts_with("origin_alter"),
starts_with("rel_alter"),
starts_with("dear_alter"),
starts_with("prof_alter"),
starts_with("work_a"),
starts_with("length")
) %>%
mutate(across(.cols = 1:62, #remove haven labelled
.fns = ~as.numeric(.x)))
#create long file so alter is in dyad
alter_data <- alter_data %>%
rename(educ_alter.1 = educ_alter1,
educ_alter.2 = educ_alter2,
educ_alter.3 = educ_alter3,
educ_alter.4 = educ_alter4,
educ_alter.5 = educ_alter5,
alter_id.1 = alter_id_1,
alter_id.2 = alter_id_2,
alter_id.3 = alter_id_3,
alter_id.4 = alter_id_4,
alter_id.5 = alter_id_5,
age_alter.1 = age_alter1,
age_alter.2 = age_alter2,
age_alter.3 = age_alter3,
age_alter.4 = age_alter4,
age_alter.5 = age_alter5,
gender_alter.1 = gender_alter1,
gender_alter.2 = gender_alter2,
gender_alter.3 = gender_alter3,
gender_alter.4 = gender_alter4,
gender_alter.5 = gender_alter5,
rel_alter.1 = rel_alter1,
rel_alter.2 = rel_alter2,
rel_alter.3 = rel_alter3,
rel_alter.4 = rel_alter4,
rel_alter.5 = rel_alter5,
poltalk_alter.1 = poltalk_alter1,
poltalk_alter.2 = poltalk_alter2,
poltalk_alter.3 = poltalk_alter3,
poltalk_alter.4 = poltalk_alter4,
poltalk_alter.5 = poltalk_alter5,
talk_alter.1 = talk_alter1,
talk_alter.2 = talk_alter2,
talk_alter.3 = talk_alter3,
talk_alter.4 = talk_alter4,
talk_alter.5 = talk_alter5,
origin_alter.1 = origin_alter1,
origin_alter.2 = origin_alter2,
origin_alter.3 = origin_alter3,
origin_alter.4 = origin_alter4,
origin_alter.5 = origin_alter5,
dear_alter.1 = dear_alter1,
dear_alter.2 = dear_alter2,
dear_alter.3 = dear_alter3,
dear_alter.4 = dear_alter4,
dear_alter.5 = dear_alter5,
prof_alter.1 = prof_alter1,
prof_alter.2 = prof_alter2,
prof_alter.3 = prof_alter3,
prof_alter.4 = prof_alter4,
prof_alter.5 = prof_alter5,
work_alter.1 = work_a1,
work_alter.2 = work_a2,
work_alter.3 = work_a3,
work_alter.4 = work_a4,
work_alter.5 = work_a5,
length.1 = length_1,
length.2 = length_2,
length.3 = length_3,
length.4 = length_4,
length.5 = length_5) %>%
pivot_longer(3:62,
names_to = c("name","alter"),
names_pattern = "(.+)\\.(.+)",
values_to = "value") %>%
pivot_wider(names_from = name,
values_from = value)
#create dyad_id and drop alter handle
alter_data <- alter_data %>%
mutate(dyad_id = paste0(nomem_encr, alter_id),
survey_wave = as.numeric(survey_wave)) %>%
filter(!is.na(alter_id)) %>%
select(!alter)
#alter data prep
alter_data <- alter_data %>%
mutate(
educ_alter = feducation_alter(educ_alter),
gender_alter = ifelse(is.na(gender_alter), 3, gender_alter),
gender_alter_fac = factor(
gender_alter,
levels = 1:3,
labels = c("Male", "Female", "Missing")
),
rel_alter_rec = ifelse(rel_alter == 1, 1, rel_alter),
rel_alter_rec = ifelse(rel_alter_rec > 2 &
rel_alter_rec < 5, 2, rel_alter_rec),
rel_alter_rec = ifelse(rel_alter_rec == 5, 3, rel_alter_rec),
rel_alter_rec = ifelse(rel_alter_rec == 6, 4, rel_alter_rec),
rel_alter_rec = ifelse(rel_alter_rec == 7, 5, rel_alter_rec),
rel_alter_rec = ifelse(rel_alter_rec == 8, 6, rel_alter_rec),
rel_alter_rec = ifelse(rel_alter_rec == 9, 7, rel_alter_rec),
rel_alter_rec = ifelse(rel_alter_rec == 10, 8, rel_alter_rec),
rel_alter_rec = ifelse(rel_alter_rec == 11, 9, rel_alter_rec),
rel_alter_rec = ifelse(is.na(rel_alter_rec), 10, rel_alter_rec),
rel_alter_rec = factor(
rel_alter_rec,
levels = 1:10,
labels = c(
"Partner",
"Close family",
"Other family",
"Colleague",
"Same group or club",
"Neighbour",
"Friend",
"Advisor",
"Other",
"Missing"
)
),
origin_alter_rec = case_when(
origin_alter == 1 ~ 0,
origin_alter > 1 &
origin_alter < 8 ~ 1,
origin_alter == 8 ~ 2
),
origin_alter_rec_fac = factor(
origin_alter_rec,
levels = 0:2,
labels = c(
"No migration background",
"Non-Western migration background",
"Western migration background"
)
)
)
#add the alter data to the event_data
repeated_event_data <- repeated_event_data %>%
left_join(alter_data, by = c("dyad_id", "survey_wave", "nomem_encr"))
Step 1: select the variables needed from the liss_long file and rename and transform them accordingly. Step 2: use pivot_longer to make a long file. Step 3: perform data manipulation on the ego variables.
#add ego level data
ego_data <- liss_long %>% #select variables from long_file
select(
nomem_encr,
survey_wave,
oplmet,
leeftijd,
geslacht,
nettoink,
belbezig,
burgstat,
partner,
gebjaar,
year_residence,
year_municipality,
starts_with("close"),
origin,
aantalki,
belbezig,
partner_current,
partner_same,
partner_different_reason,
partner_none_reason,
has_children,
birthyear_firstchild
) %>%
mutate(#transform data
survey_wave = as.numeric(survey_wave),
year = as.numeric(survey_wave) + 2007,
educ_ego = feducation_ego(oplmet),
year = as.factor(survey_wave + 2007),
age_sq = leeftijd * leeftijd,
age = leeftijd,
gender = geslacht,
gender = ifelse(is.na(gender), 3, gender),
gender_fac = factor(
gender,
levels = 1:3,
labels = c("Male", "Female", "Missing")
),
origin_rec = case_when(
origin == 0 ~ 0,
origin == 102 | origin == 202 ~ 1,
origin == 101 | origin == 201 ~ 2
),
origin_rec_fac = factor(
origin_rec,
levels = 0:2,
labels = c(
"No migration background",
"Non-Western migration background",
"Western migration background"
)
),
belbezig_rec = ifelse(belbezig == 2 | belbezig == 3, 2, belbezig),
belbezig_rec = ifelse(belbezig == 4 |
belbezig == 5, 3, belbezig_rec),
belbezig_rec = ifelse(belbezig == 7 , 4, belbezig_rec),
belbezig_rec = ifelse(belbezig == 8, 5, belbezig_rec),
belbezig_rec = ifelse(belbezig == 9, 6, belbezig_rec),
belbezig_rec = ifelse(belbezig == 6 |
belbezig >= 10, 7, belbezig_rec),
employment_status = factor(
belbezig_rec,
levels = c(1:7),
labels = c(
"employed",
"self-employed",
"unemployed",
"student",
"housework",
"retired",
"other"
)
),
paid_work = if_else(belbezig < 4, 1, 0),
first_child = ifelse(birthyear_firstchild == year, 1, 0),
divorced_seperated = ifelse((partner_same == 2 & partner_different_reason == 1) |
(partner_current == 2 & partner_none_reason == 1), 1, 0),
lost_partner = ifelse((partner_same == 2 & partner_different_reason == 2) |
(partner_current == 2 & partner_none_reason == 2), 1, 0)
) %>%
#create change scores.
group_by(nomem_encr) %>%
dplyr::arrange(nomem_encr, survey_wave) %>%
mutate(
new_residence = ifelse(year_residence == lag(year_residence), 0, 1),
new_municipality = ifelse(year_municipality == lag(year_municipality), 0, 1),
divorced_background = ifelse(burgstat == 3 &
lag(burgstat) != 3, 1, 0),
child_change_hh = ifelse(lag(aantalki) == aantalki, 0, 1),
child_diff_hh = aantalki - lag(aantalki),
first_child_hh = ifelse(aantalki > 0 & lag(aantalki) == 0, 1, 0),
employment_change = ifelse(belbezig_rec == lag(belbezig_rec), 0, 1),
retired = ifelse(lag(belbezig_rec) != 6 & belbezig_rec == 6, 1, 0),
unemployment = ifelse(lag(belbezig_rec) != 3 & belbezig_rec == 3, 1, 0),
employment = ifelse(lag(belbezig_rec) == 3 & belbezig_rec < 3, 1, 0)
) %>%
ungroup()
# Robustness prep: create different types of changes: transition and event.
#first child transition and event
ego_data <- ego_data %>%
group_by(nomem_encr) %>%
arrange(nomem_encr, survey_wave) %>%
mutate(first_child_event = event_transition_f(event = first_child,
option = "event"),
first_child_transition = event_transition_f(event = first_child,
option = "transition",
time = survey_wave)) %>%
ungroup()
#create absolute change scores and some extra variables.
ego_data <- ego_data %>%
mutate(
moving = new_municipality + new_residence,
moving = factor(
moving,
levels = 0:2,
labels = c("no_move", "new_residence",
"new_residence_municipality")
),
moving_fac = ifelse(moving == "no_move", 1, moving),
moving_fac = ifelse(moving == "new_residence", 2, moving_fac),
moving_fac = ifelse(moving == "new_residence_municipality", 3, moving_fac),
moving_fac = ifelse(is.na(moving), 4, moving_fac),
moving_fac = factor(
moving_fac,
levels = 1:4,
labels = c("no_move", "new_residence",
"new_municipality", "missing")
),
divorced_fac = ifelse(is.na(divorced_seperated), 2, divorced_seperated),
divorced_fac = factor(
divorced_fac,
levels = 0:2,
labels = c("not divorced", "divorced", "missing")
)
)
#divorce as transition and event
ego_data <- ego_data %>%
group_by(nomem_encr) %>%
arrange(nomem_encr, survey_wave) %>%
mutate(divorced_event = event_transition_f(event = divorced_seperated,
option = "event"),
divorced_transition = event_transition_f(event = divorced_seperated,
option = "transition",
time = survey_wave)) %>%
ungroup()
#lost_partner as transition and event
ego_data <- ego_data %>%
group_by(nomem_encr) %>%
arrange(nomem_encr, survey_wave) %>%
mutate(lost_partner_event = event_transition_f(event = lost_partner,
option = "event"),
lost_partner_transition = event_transition_f(event = lost_partner,
option = "transition",
time = survey_wave)) %>%
ungroup()
#moving as event or transition.
ego_data <- ego_data %>%
mutate(move = if_else(!is.na(new_municipality) == 1 | !is.na(new_residence) == 1, 1, 0)) %>%
group_by(nomem_encr) %>%
arrange(nomem_encr, survey_wave) %>%
mutate(
new_municipality_event = event_transition_f(event = new_municipality,
option = "event"),
new_municipality_transition = event_transition_f(
event = new_municipality,
option = "transition",
time = survey_wave
),
new_residence_event = event_transition_f(event = new_residence,
option = "event"),
new_residence_transition = event_transition_f(
event = new_residence,
option = "transition",
time = survey_wave
),
move_event = event_transition_f(event = move,
option = "event"),
move_transition = event_transition_f(
event = move,
option = "transition",
time = survey_wave
)
) %>%
ungroup()
#employment status change as transition or event
ego_data <- ego_data %>%
group_by(nomem_encr) %>%
arrange(nomem_encr, survey_wave) %>%
mutate(employment_event = event_transition_f(event = employment,
option = "event"),
employment_transition = event_transition_f(event = employment,
option = "transition",
time = survey_wave),
retired_event = event_transition_f(event = retired,
option = "event"),
retired_transition = event_transition_f(event = retired,
option = "transition",
time = survey_wave),
unemployed_event = event_transition_f(event = unemployment,
option = "event"),
unemployed_transition = event_transition_f(event = unemployment,
option = "transition",
time = survey_wave),
) %>%
ungroup()
#fill ego's origin with information of later waves.
ego_data <- ego_data %>%
group_by(nomem_encr) %>%
mutate(
origin_rec_nar = ifelse(is.na(origin_rec), min(origin_rec, na.rm = T), origin_rec),
origin_rec_nar = ifelse(origin_rec_nar > 3, NA, origin_rec_nar)
) %>%
ungroup() %>%
mutate(origin_rec_nar_fac = factor(
origin_rec_nar,
levels = 0:2,
labels = c(
"No migration background",
"Non-Western migration background",
"Western migration background"
)
))
#add ego data.
repeated_event_data <- repeated_event_data %>%
left_join(ego_data, by = c("nomem_encr", "survey_wave"))
Export the ego alter prepped data so we can use it for creation of network data and for dyad similarity variables.
save(repeated_event_data, file = "datafiles/data-processed/disaggregated_data/2023-06-12_liss-repeated-risk-alter-ego-data.rda")
save(ego_data,
file = "datafiles/data-processed/disaggregated_data/2023-06-12_ego-data.rda")
save(alter_data,
file = "datafiles/data-processed/disaggregated_data/2023-06-12_alter-data.rda")
Copyright © 2023 Jeroense Thijmen