
#STUDY: VOICE SURVEY NURSING HOMES

#Get the codes and create additional variables before analyzing

rm(list=ls())

#Libraries
library(haven) # for dealing with labelled dataframe
library(tidyverse) # for data handling
library(dplyr) # to order the dataset
library(writexl) #to export as an excel file (the subdataset for coding)
library(readxl)
library(openxlsx)

#Define path
path <- "yourpath" #Replace "yourpath" with the place where the data is saved


#######################################################################
#Create functions to simplify the code later
########################################################################

#Function to get the n and proportions
prop=function(data, var){
  data %>% 
    drop_na({{var}}) %>%
    group_by({{var}}) %>%
    summarise(n=n()) %>% 
    mutate(n_cum=cumsum(n), f=n/
             sum(n), f_cum=cumsum(f)) %>%
   print(n = 80)
}

#Function for proportions but by group
propby=function(data, var1, var2){
  data %>% 
    drop_na({{var1}}, {{var2}}) %>%
    group_by({{var1}}, {{var2}}) %>%
    summarise(n=n()) %>% 
    mutate(n_cum=cumsum(n), f=n/
             sum(n), f_cum=cumsum(f))%>%
    print(n = 80)
}

#Function for means, st dev, etc.
meanandmore=function(data, var){
  data %>% 
    filter(!is.na({{var}})) %>% #needed when there are NAs
    summarise(mean=mean({{var}}), stdev=sd({{var}}), min=min({{var}}), max=max({{var}}), median=median({{var}}))
}

#Function for means, st dev, etc. but by group
meanandmoreby=function(data, var1, var2){
  data %>% 
    filter(!is.na({{var1}})) %>% #needed when there are NAs
    group_by({{var2}}) %>%
    summarise(mean=mean({{var1}}), stdev=sd({{var1}}), min=min({{var1}}), max=max({{var1}}), median=median({{var1}}))
}

###################################################################################################################
#Add codes for WHYQUALITY 
##################################################################################################################

#Get Excels from coders 1 & 2
#The names for the variables include C1 or C2 at the end, to distinguish from who the variables come from

datafinalC1 <- read_excel(file.path(path,"FinalWHYQUALITY-Coder1.xlsx"))
datafinalC2 <- read_excel(file.path(path,"FinalWHYQUALITY-Coder2.xlsx"))

#coder 2 did not code the number of themes, so we count it here before merging the files
datafinalC2 <- datafinalC2 %>%
  mutate(NTHEMES_QUALITYC2 = case_when(
    VALID_QUALITYC3==4 ~ 10- rowSums(across(starts_with("THEME"), ~ is.na(.))),
    VALID_QUALITYC3!=4 ~ NA
    ))
    
prop(datafinalC2, NTHEMES_QUALITYC2)
prop(datafinalC2, VALID_QUALITYC3)
propby(datafinalC2, VALID_QUALITYC3, NTHEMES_QUALITYC2)

#We drop 2 variables that are present in both excel files: WHYQUALITY_OPEN and VALID_QUALITYC3
#otherwise, it creates a name.x and name.y version of the variables
datafinalC2 <- datafinalC2 %>%
  select(-WHYQUALITY_OPEN, -VALID_QUALITYC3)

#now merge both datasets
datafinalC1C2 <- merge(datafinalC1, datafinalC2, by = "id_ea")

#Drop again WHYQUALITY_OPEN because it is also in the main data
datafinalC1C2 <- datafinalC1C2 %>%
  select(-WHYQUALITY_OPEN)

#Merge this back with all the rest of the data
#dataset with ID needed for this step
maindata <- read_rds(file.path(path,"clean_dataallcomplete_addvar.rds"))
maindata = maindata %>% filter(type=="complete")#select only completes

datacodingWHYQUALITYfinal_merged <- merge(maindata, datafinalC1C2, by = "id_ea") 

#Put as factor
datacodingWHYQUALITYfinal_merged = as_factor(datacodingWHYQUALITYfinal_merged)

#Change the labels for validity
datacodingWHYQUALITYfinal_merged <- datacodingWHYQUALITYfinal_merged %>%
  mutate(VALID_QUALITYC1 = case_when(
    VALID_QUALITYC1 == 1 ~ "1 Non sense",
    VALID_QUALITYC1 == 2 ~ "2 Not in line",
    VALID_QUALITYC1 == 3 ~ "3 DK",
    VALID_QUALITYC1 == 4 ~ "4 Valid",
    VALID_QUALITYC1 == 9 ~ "9 To be defined",
    VALID_QUALITYC1 == 10 ~ "10 Depends"
  ))

datacodingWHYQUALITYfinal_merged <- datacodingWHYQUALITYfinal_merged %>%
  mutate(VALID_QUALITYC2 = case_when(
    VALID_QUALITYC2 == 1 ~ "1 Non sense",
    VALID_QUALITYC2 == 2 ~ "2 Not in line",
    VALID_QUALITYC2 == 3 ~ "3 DK",
    VALID_QUALITYC2 == 4 ~ "4 Valid",
    VALID_QUALITYC2 == 9 ~ "9 To be defined",
    VALID_QUALITYC2 == 10 ~ "10 Depends"
  ))

datacodingWHYQUALITYfinal_merged <- datacodingWHYQUALITYfinal_merged %>%
  mutate(VALID_QUALITYC3 = case_when(
    VALID_QUALITYC3 == 1 ~ "1 Non sense",
    VALID_QUALITYC3 == 2 ~ "2 Not in line",
    VALID_QUALITYC3 == 3 ~ "3 DK",
    VALID_QUALITYC3 == 4 ~ "4 Valid",
    VALID_QUALITYC3 == 10 ~ "10 Depends"
  ))

prop(datacodingWHYQUALITYfinal_merged, VALID_QUALITYC3)

#Change labels for abbreviations

datacodingWHYQUALITYfinal_merged <- datacodingWHYQUALITYfinal_merged %>%
  mutate(ABBR_QUALITYC1 = case_when(
    ABBR_QUALITYC1 == 0 ~ "None",
    ABBR_QUALITYC1 == 1 ~ "Abbreviation(s)",
    ABBR_QUALITYC1 == 2 ~ "Emoticon(s)/Emoji(s)",
  ))

datacodingWHYQUALITYfinal_merged <- datacodingWHYQUALITYfinal_merged %>%
  mutate(ABBR_QUALITYC2 = case_when(
    ABBR_QUALITYC2 == 0 ~ "None",
    ABBR_QUALITYC2 == 1 ~ "Abbreviation(s)",
    ABBR_QUALITYC2 == 2 ~ "Emoticon(s)/Emoji(s)",#we do not have any of those
  ))

#Change labels for problems

datacodingWHYQUALITYfinal_merged <- datacodingWHYQUALITYfinal_merged %>%
  mutate(PB_QUALITYC1 = case_when(
    PB_QUALITYC1 == 0 ~ "No problem detected",
    PB_QUALITYC1 == 1 ~ "Incomplete sentence(s)",
    PB_QUALITYC1 == 2 ~ "Part of the answer repeated",
    PB_QUALITYC1 == 3 ~ "Other problem(s)",
  ))

datacodingWHYQUALITYfinal_merged <- datacodingWHYQUALITYfinal_merged %>%
  mutate(PB_QUALITYC2 = case_when(
    PB_QUALITYC2 == 0 ~ "No problem detected",
    PB_QUALITYC2 == 1 ~ "Incomplete sentence(s)",
    PB_QUALITYC2 == 2 ~ "Part of the answer repeated",
    PB_QUALITYC2 == 3 ~ "Other problem(s)",
  ))

#keeping it in R format

write_rds(datacodingWHYQUALITYfinal_merged,file.path(path,"datafinalWHYQUALITY.rds"))


###################################################################################################################
#Add codes for WHYTRANSP
##################################################################################################################

#Get Excels from coders 1 & 2 

datafinalC1TRANSP <- read_excel(file.path(path,"FinalWHYTRANSP-Coder1.xlsx"))
datafinalC2TRANSP <- read_excel(file.path(path,"FinalWHYTRANSP-Coder2.xlsx"))

#Drop 2 variables that are present in both excel files: WHYQUALITY_OPEN and VALID_QUALITYC3
datafinalC2TRANSP <- datafinalC2TRANSP %>%
  select(-WHYTRANSP_FINAL.x, -VALID_TRANSPC3)

#now merge both datasets
datafinalC1C2TRANSP <- merge(datafinalC1TRANSP, datafinalC2TRANSP, by = "id_ea")

#Merge this back with all the rest of the data: we use datafinalWHYQUALITY to get also the codes of WHYQUALITY 
#dataset with ID needed for this step

datafinalWHYQUALITY = read_rds(file.path(path,"datafinalWHYQUALITY.rds"))
datafinalWHYQUALITY_mergedTRANSP <- merge(datafinalWHYQUALITY, datafinalC1C2TRANSP, by = "id_ea") 

#Put as factor
datafinalWHYQUALITY_mergedTRANSP = as_factor(datafinalWHYQUALITY_mergedTRANSP)

#Code the number of themes  

variables_to_checkC1 <- c("REASONS1_TRANSPC1", "REASONS2_TRANSPC1", "REASONS3_TRANSPC1",
                          "ATTRI1_TRANSPC1", "ATTRI2_TRANSPC1",
                          "SOURCE1_TRANSPC1", "SOURCE2_TRANSPC1", "SOURCE3_TRANSPC1",
                          "DIFF1_TRANSPC1", "DIFF2_TRANSPC1", "DIFF3_TRANSPC1", "DIFF4_TRANSPC1")

variables_to_checkC2 <- c("REASONS1_TRANSPC2", "REASONS2_TRANSPC2", "REASONS3_TRANSPC2",
                          "ATTRI1_TRANSPC2", "ATTRI2_TRANSPC2",
                          "SOURCE1_TRANSPC2", "SOURCE2_TRANSPC2", "SOURCE3_TRANSPC2",
                          "DIFF1_TRANSPC2", "DIFF2_TRANSPC2", "DIFF3_TRANSPC2", "DIFF4_TRANSPC2")

datafinalWHYQUALITY_mergedTRANSP  <- datafinalWHYQUALITY_mergedTRANSP  %>%
  mutate(NTHEMES_TRANSP1b = case_when(
    VALID_TRANSPC3==4 ~ rowSums(across(all_of(variables_to_checkC1), ~ . != "none" & !is.na(.))),
    VALID_TRANSPC3!=4 ~ NA
  ))

datafinalWHYQUALITY_mergedTRANSP  <- datafinalWHYQUALITY_mergedTRANSP  %>%
  mutate(NTHEMES_TRANSP2b = case_when(
    VALID_TRANSPC3==4 ~ rowSums(across(all_of(variables_to_checkC2), ~ . != "none" & !is.na(.))),
    VALID_TRANSPC3!=4 ~ NA
  ))


#Change labels for validity
datafinalWHYQUALITY_mergedTRANSP <- datafinalWHYQUALITY_mergedTRANSP %>%
  mutate(VALID_TRANSPC1 = case_when(
    VALID_TRANSPC1 == 1 ~ "1 Non sense",
    VALID_TRANSPC1 == 2 ~ "2 Not in line",
    VALID_TRANSPC1 == 3 ~ "3 DK",
    VALID_TRANSPC1 == 4 ~ "4 Valid",
    VALID_TRANSPC1 == 9 ~ "9 To be defined",
    VALID_TRANSPC1 == 10 ~ "10 Depends"
  ))

datafinalWHYQUALITY_mergedTRANSP <- datafinalWHYQUALITY_mergedTRANSP %>%
  mutate(VALID_TRANSPC2 = case_when(
    VALID_TRANSPC2 == 1 ~ "1 Non sense",
    VALID_TRANSPC2 == 2 ~ "2 Not in line",
    VALID_TRANSPC2 == 3 ~ "3 DK",
    VALID_TRANSPC2 == 4 ~ "4 Valid",
    VALID_TRANSPC2 == 9 ~ "9 To be defined",
    VALID_TRANSPC2 == 10 ~ "10 Depends"
  ))

datafinalWHYQUALITY_mergedTRANSP <- datafinalWHYQUALITY_mergedTRANSP %>%
  mutate(VALID_TRANSPC3 = case_when(
    VALID_TRANSPC3 == 1 ~ "1 Non sense",
    VALID_TRANSPC3 == 2 ~ "2 Not in line",
    VALID_TRANSPC3 == 3 ~ "3 DK",
    VALID_TRANSPC3 == 4 ~ "4 Valid",
    VALID_TRANSPC3 == 10 ~ "10 Depends"
  ))

prop(datafinalWHYQUALITY_mergedTRANSP, VALID_TRANSPC3)


#Change labels for ASPECTS
#1) Only answered to the "why" part of the question
#2) Only answered the second part of the question (difference public/private)
#3) Answered both parts

datafinalWHYQUALITY_mergedTRANSP <- datafinalWHYQUALITY_mergedTRANSP %>%
  mutate(ASPECTS_TRANSPC1 = case_when(
    ASPECTS_TRANSPC1 == 1 ~ "First part",
    ASPECTS_TRANSPC1 == 2 ~ "Second part",
    ASPECTS_TRANSPC1 == 3 ~ "Both parts",
  ))

datafinalWHYQUALITY_mergedTRANSP <- datafinalWHYQUALITY_mergedTRANSP %>%
  mutate(ASPECTS_TRANSPC2 = case_when(
    ASPECTS_TRANSPC2 == 1 ~ "First part",
    ASPECTS_TRANSPC2 == 2 ~ "Second part",
    ASPECTS_TRANSPC2 == 3 ~ "Both parts",
  ))


#Change labels for abbreviations

datafinalWHYQUALITY_mergedTRANSP <- datafinalWHYQUALITY_mergedTRANSP %>%
  mutate(ABBR_TRANSPC1 = case_when(
    ABBR_TRANSPC1 == 0 ~ "None",
    ABBR_TRANSPC1 == 1 ~ "Abbreviation(s)",
    ABBR_TRANSPC1 == 2 ~ "Emoticon(s)/Emoji(s)",
  ))

datafinalWHYQUALITY_mergedTRANSP <- datafinalWHYQUALITY_mergedTRANSP %>%
  mutate(ABBR_TRANSPC2 = case_when(
    ABBR_TRANSPC2 == 0 ~ "None",
    ABBR_TRANSPC2 == 1 ~ "Abbreviation(s)",
    ABBR_TRANSPC2 == 2 ~ "Emoticon(s)/Emoji(s)",#we do not have any of those
  ))

#Change labels for problems

datafinalWHYQUALITY_mergedTRANSP <- datafinalWHYQUALITY_mergedTRANSP %>%
  mutate(PB1_TRANSPC1 = case_when(
    PB1_TRANSPC1 == 0 ~ "No problem detected",
    PB1_TRANSPC1 == 1 ~ "Incomplete sentence(s)",
    PB1_TRANSPC1 == 2 ~ "Part of the answer repeated",
    PB1_TRANSPC1 == 3 ~ "Other problem(s)",
  ))

datafinalWHYQUALITY_mergedTRANSP <- datafinalWHYQUALITY_mergedTRANSP %>%
  mutate(PB2_TRANSPC1 = case_when(
    PB2_TRANSPC1 == 0 ~ "No problem detected",
    PB2_TRANSPC1 == 1 ~ "Incomplete sentence(s)",
    PB2_TRANSPC1 == 2 ~ "Part of the answer repeated",
    PB2_TRANSPC1 == 3 ~ "Other problem(s)",
  ))

datafinalWHYQUALITY_mergedTRANSP <- datafinalWHYQUALITY_mergedTRANSP %>%
  mutate(PB1_TRANSPC2 = case_when(
    PB1_TRANSPC2 == 0 ~ "No problem detected",
    PB1_TRANSPC2 == 1 ~ "Incomplete sentence(s)",
    PB1_TRANSPC2 == 2 ~ "Part of the answer repeated",
    PB1_TRANSPC2 == 3 ~ "Other problem(s)",
  ))

datafinalWHYQUALITY_mergedTRANSP <- datafinalWHYQUALITY_mergedTRANSP %>%
  mutate(PB2_TRANSPC2 = case_when(
    PB2_TRANSPC2 == 0 ~ "No problem detected",
    PB2_TRANSPC2 == 1 ~ "Incomplete sentence(s)",
    PB2_TRANSPC2 == 2 ~ "Part of the answer repeated",
    PB2_TRANSPC2 == 3 ~ "Other problem(s)",
  ))


#Keeping it in R format

write_rds(datafinalWHYQUALITY_mergedTRANSP,file.path(path,"datafinalWHYTRANSP.rds"))


###################################################################################################################
#Add codes for WHYTRUST
##################################################################################################################


#Get data from coders 1 & 2 

datafinalC1TRUST <- read_excel(file.path(path,"FinalWHYTRUST-Coder1.xlsx"))
datafinalC2TRUST <- read_excel(file.path(path,"FinalWHYTRUST-Coder2.xlsx"))

prop(datafinalC1TRUST, VALID_TRUSTC3)
prop(datafinalC2TRUST, VALID_TRUSTC3)

#Drop 2 variables that are present in both excel files: WHYTRUSTNH_FINAL.x and VALID_TRUSTC3
datafinalC2TRUST <- datafinalC2TRUST %>%
  select(-WHYTRUSTNH_FINAL.x, -VALID_TRUSTC3)#minus sign means we drop it

#Merge both datasets 
datafinalC1C2TRUST <- merge(datafinalC1TRUST, datafinalC2TRUST, by = "id_ea")

#Merge this back with all the rest of the data
#dataset with ID needed for this step

datafinalWHYTRANSP = read_rds(file.path(path,"datafinalWHYTRANSP.rds")) #This one has already codes for WHYQUALIYT & WHYTRANSP

datafinalWHYQUALITY_mergedTRANSPTRUST <- merge(datafinalWHYTRANSP, datafinalC1C2TRUST, by = "id_ea")

#Put as factor
datafinalWHYQUALITY_mergedTRANSPTRUST = as_factor(datafinalWHYQUALITY_mergedTRANSPTRUST)

#Filling missing values in VALID_TRUSTC3 (so far only values when coder 3 intervened)
#when C3 is missing, put the value of C2 (which should be the same as the one of C1 - this is the case)
datafinalWHYQUALITY_mergedTRANSPTRUST  <- datafinalWHYQUALITY_mergedTRANSPTRUST  %>%
  mutate(VALID_TRUSTC3 = if_else(is.na(VALID_TRUSTC3), VALID_TRUSTC2, VALID_TRUSTC3))

#Code the number of themes 

variables_to_checkC1 <- c("THEME1_TRUSTC1", "THEME2_TRUSTC1", "THEME3_TRUSTC1", "THEME4_TRUSTC1",
                          "DIFF1_TRUSTC1", "DIFF2_TRUSTC1", "DIFF3_TRUSTC1", 
                          "DIMEN1_TRUSTC1", "DIMEN2_TRUSTC1", "DIMEN3_TRUSTC1")

variables_to_checkC2 <- c("THEME1_TRUSTC2", "THEME2_TRUSTC2", "THEME3_TRUSTC2", "THEME4_TRUSTC2",
                          "DIFF1_TRUSTC2", "DIFF2_TRUSTC2", "DIFF3_TRUSTC2", 
                          "DIMEN1_TRUSTC2", "DIMEN2_TRUSTC2", "DIMEN3_TRUSTC2")

datafinalWHYQUALITY_mergedTRANSPTRUST  <- datafinalWHYQUALITY_mergedTRANSPTRUST  %>%
  mutate(NTHEMES_TRUST1b = case_when(
    VALID_TRUSTC3==4 ~ rowSums(across(all_of(variables_to_checkC1), ~ . != "none" & !is.na(.))),
    VALID_TRUSTC3!=4 ~ NA
  ))

datafinalWHYQUALITY_mergedTRANSPTRUST  <- datafinalWHYQUALITY_mergedTRANSPTRUST  %>%
  mutate(NTHEMES_TRUST2b = case_when(
    VALID_TRUSTC3==4 ~ rowSums(across(all_of(variables_to_checkC2), ~ . != "none" & !is.na(.))),
    VALID_TRUSTC3!=4 ~ NA
  ))

prop(datafinalWHYQUALITY_mergedTRANSPTRUST, NTHEMES_TRUST2b)
prop(datafinalWHYQUALITY_mergedTRANSPTRUST, NTHEMES_TRUST1b)

#Change the labels for validity
datafinalWHYQUALITY_mergedTRANSPTRUST <- datafinalWHYQUALITY_mergedTRANSPTRUST %>%
  mutate(VALID_TRUSTC1 = case_when(
    VALID_TRUSTC1 == 1 ~ "1 Non sense",
    VALID_TRUSTC1 == 2 ~ "2 Not in line",
    VALID_TRUSTC1 == 3 ~ "3 DK",
    VALID_TRUSTC1 == 4 ~ "4 Valid",
    VALID_TRUSTC1 == 5 ~ "5 Refer previous answer",
    VALID_TRUSTC1 == 9 ~ "9 To be defined",
    VALID_TRUSTC1 == 10 ~ "10 Depends"
  ))

datafinalWHYQUALITY_mergedTRANSPTRUST <- datafinalWHYQUALITY_mergedTRANSPTRUST %>%
  mutate(VALID_TRUSTC2 = case_when(
    VALID_TRUSTC2 == 1 ~ "1 Non sense",
    VALID_TRUSTC2 == 2 ~ "2 Not in line",
    VALID_TRUSTC2 == 3 ~ "3 DK",
    VALID_TRUSTC2 == 4 ~ "4 Valid",
    VALID_TRUSTC2 == 5 ~ "5 Refer previous answer",
    VALID_TRUSTC2 == 9 ~ "9 To be defined",
    VALID_TRUSTC2 == 10 ~ "10 Depends"
  ))

datafinalWHYQUALITY_mergedTRANSPTRUST <- datafinalWHYQUALITY_mergedTRANSPTRUST %>%
  mutate(VALID_TRUSTC3 = case_when(
    VALID_TRUSTC3 == 1 ~ "1 Non sense",
    VALID_TRUSTC3 == 2 ~ "2 Not in line",
    VALID_TRUSTC3 == 3 ~ "3 DK",
    VALID_TRUSTC3 == 5 ~ "5 Refer previous answer",
    VALID_TRUSTC3 == 4 ~ "4 Valid",
    VALID_TRUSTC3 == 10 ~ "10 Depends"
  ))

prop(datafinalWHYQUALITY_mergedTRANSPTRUST, VALID_TRUSTC3)
prop(datafinalWHYQUALITY_mergedTRANSPTRUST, VALID_TRUSTC1)
prop(datafinalWHYQUALITY_mergedTRANSPTRUST, VALID_TRUSTC2)

#Change the labels for ASPECTS

datafinalWHYQUALITY_mergedTRANSPTRUST <- datafinalWHYQUALITY_mergedTRANSPTRUST %>%
  mutate(ASPECTS_TRUSTC1 = case_when(
    ASPECTS_TRUSTC1 == 1 ~ "First part",
    ASPECTS_TRUSTC1 == 2 ~ "Second part",
    ASPECTS_TRUSTC1 == 3 ~ "Both parts",
  ))

datafinalWHYQUALITY_mergedTRANSPTRUST <- datafinalWHYQUALITY_mergedTRANSPTRUST %>%
  mutate(ASPECTS_TRUSTC2 = case_when(
    ASPECTS_TRUSTC2 == 1 ~ "First part",
    ASPECTS_TRUSTC2 == 2 ~ "Second part",
    ASPECTS_TRUSTC2 == 3 ~ "Both parts",
  ))

#Change the labels for abbreviations

datafinalWHYQUALITY_mergedTRANSPTRUST <- datafinalWHYQUALITY_mergedTRANSPTRUST %>%
  mutate(ABBR_TRUSTC1 = case_when(
    ABBR_TRUSTC1 == 0 ~ "None",
    ABBR_TRUSTC1 == 1 ~ "Abbreviation(s)",
    ABBR_TRUSTC1 == 2 ~ "Emoticon(s)/Emoji(s)",
  ))

datafinalWHYQUALITY_mergedTRANSPTRUST <- datafinalWHYQUALITY_mergedTRANSPTRUST %>%
  mutate(ABBR_TRUSTC2 = case_when(
    ABBR_TRUSTC2 == 0 ~ "None",
    ABBR_TRUSTC2 == 1 ~ "Abbreviation(s)",
    ABBR_TRUSTC2 == 2 ~ "Emoticon(s)/Emoji(s)",#we do not have any of those
  ))

#Change the labels for problems

datafinalWHYQUALITY_mergedTRANSPTRUST <- datafinalWHYQUALITY_mergedTRANSPTRUST %>%
  mutate(PB1_TRUSTC1 = case_when(
    PB1_TRUSTC1 == 0 ~ "No problem detected",
    PB1_TRUSTC1 == 1 ~ "Incomplete sentence(s)",
    PB1_TRUSTC1 == 2 ~ "Part of the answer repeated",
    PB1_TRUSTC1 == 3 ~ "Other problem(s)",
  ))

datafinalWHYQUALITY_mergedTRANSPTRUST <- datafinalWHYQUALITY_mergedTRANSPTRUST %>%
  mutate(PB2_TRUSTC1 = case_when(
    PB2_TRUSTC1 == 0 ~ "No problem detected",
    PB2_TRUSTC1 == 1 ~ "Incomplete sentence(s)",
    PB2_TRUSTC1 == 2 ~ "Part of the answer repeated",
    PB2_TRUSTC1 == 3 ~ "Other problem(s)",
  ))

datafinalWHYQUALITY_mergedTRANSPTRUST <- datafinalWHYQUALITY_mergedTRANSPTRUST %>%
  mutate(PB1_TRUSTC2 = case_when(
    PB1_TRUSTC2 == 0 ~ "No problem detected",
    PB1_TRUSTC2 == 1 ~ "Incomplete sentence(s)",
    PB1_TRUSTC2 == 2 ~ "Part of the answer repeated",
    PB1_TRUSTC2 == 3 ~ "Other problem(s)",
  ))

datafinalWHYQUALITY_mergedTRANSPTRUST <- datafinalWHYQUALITY_mergedTRANSPTRUST %>%
  mutate(PB2_TRUSTC2 = case_when(
    PB2_TRUSTC2 == 0 ~ "No problem detected",
    PB2_TRUSTC2 == 1 ~ "Incomplete sentence(s)",
    PB2_TRUSTC2 == 2 ~ "Part of the answer repeated",
    PB2_TRUSTC2 == 3 ~ "Other problem(s)",
  ))


#Keeping it in R format

write_rds(datafinalWHYQUALITY_mergedTRANSPTRUST,file.path(path,"datafinalALLCODES.rds"))


########################################################################################################
#use dataset saved just before which includes all codes
########################################################################################################

datafinalALLCODES = read_rds(file.path(path,"datafinalALLCODES.rds"))

prop(datafinalALLCODES, VALID_TRUSTC3)
prop(datafinalALLCODES, VALID_TRANSPC3)
prop(datafinalALLCODES, VALID_QUALITYC3)

##################################################################################################
#Transform explanatory variables when needed to have them in the format needed for the regressions
##################################################################################################

#GENDER
prop(datafinalALLCODES, GENDER)

#Create a new dataset to avoid any problem in the other one that required lot of codes to be created
datafinalALLCODESreg = datafinalALLCODES %>%
  mutate(FEMALE = case_when(
    GENDER == "Masculino" ~ 0,
    GENDER == "Femenino" ~ 1
  ))

prop(datafinalALLCODESreg,  FEMALE)

#AGE --> relationship might not be linear, so create groups
meanandmore(datafinalALLCODESreg, AGE)

datafinalALLCODESreg = datafinalALLCODESreg %>%
  mutate(SIXTYPLUS = case_when(
    59 < AGE  ~ 1,
    AGE < 60  ~ 0,
    TRUE ~ NA
  ))

datafinalALLCODESreg = datafinalALLCODESreg %>%
  mutate(THIRTYLESS = case_when(
    30 < AGE  ~ 0,
    AGE < 31  ~ 1,
    TRUE ~ NA
  ))

prop(datafinalALLCODESreg, AGE)
prop(datafinalALLCODESreg, SIXTYPLUS)
prop(datafinalALLCODESreg, THIRTYLESS)

#EDUC --> create dummies
prop(datafinalALLCODESreg, EDUC_REC)

levels(datafinalALLCODESreg$EDUC_REC) <- c("Low","Mid","High") # To reduce the labels

datafinalALLCODESreg = datafinalALLCODESreg %>%
  mutate(HIGH_EDUC = case_when(
    EDUC_REC != "High" ~ 0,
    EDUC_REC == "High" ~ 1,
    TRUE ~ NA
  ))

datafinalALLCODESreg = datafinalALLCODESreg %>%
  mutate(LOW_EDUC = case_when(
    EDUC_REC != "Low" ~ 0,
    EDUC_REC == "Low" ~ 1,
    TRUE ~ NA
  ))

prop(datafinalALLCODESreg, LOW_EDUC)
prop(datafinalALLCODESreg, HIGH_EDUC)


#LANGUAGE 
prop(datafinalALLCODESreg, LANGUAGE)

datafinalALLCODESreg = datafinalALLCODESreg %>%
  mutate(SPANISH = case_when(
    LANGUAGE == "No, no es una de mis lenguas maternas" ~ 0,
    LANGUAGE != "No, no es una de mis lenguas maternas" ~ 1,
    TRUE ~ NA
  ))

prop(datafinalALLCODESreg, SPANISH)

#Knowledge about nursing homes
prop(datafinalALLCODESreg, KNOW_FILTER)#this is a factor, transform as numeric

datafinalALLCODESreg$KNOW_FILTERnum <- as.numeric(datafinalALLCODESreg$KNOW_FILTER)

prop(datafinalALLCODESreg, KNOW_FILTERnum)

#Social trust: 5= more trust
prop(datafinalALLCODESreg, SOCTRUST)#same here, it is a factor, transform as numeric

datafinalALLCODESreg$SOCTRUSTnum <- as.numeric(datafinalALLCODESreg$SOCTRUST)

prop(datafinalALLCODESreg, SOCTRUSTnum)

#CONFIDENTIAL: 5 = more trust
prop(datafinalALLCODESreg, CONFIDENTIAL)

datafinalALLCODESreg$CONFIDENTIALnum <- as.numeric(datafinalALLCODESreg$CONFIDENTIAL)

prop(datafinalALLCODESreg, CONFIDENTIALnum)

#create dummy for confidential
datafinalALLCODESreg= datafinalALLCODESreg %>%
  mutate(CONFIDENTIALdum = case_when(
    CONFIDENTIAL == "Nada " ~ 0,
    CONFIDENTIAL == "Poco" ~ 0,
    CONFIDENTIAL == "Algo" ~ 0,
    CONFIDENTIAL == "Mucho" ~ 0,
    CONFIDENTIAL == "Totalmente" ~ 1,
    TRUE ~ NA
  ))

#COMFORT: 5 = more comfort
prop(datafinalALLCODESreg, COMFORT)

datafinalALLCODESreg$COMFORTnum <- as.numeric(datafinalALLCODESreg$COMFORT)

prop(datafinalALLCODESreg, COMFORTnum)

datafinalALLCODESreg = datafinalALLCODESreg %>%
  mutate(COMFORTdum = case_when(
    COMFORT == "Nada cómodo/a" ~ 0,
    COMFORT == "Poco cómodo/a" ~ 0,
    COMFORT == "Bastante cómodo/a" ~ 1,
    COMFORT == "Muy cómodo/a" ~ 1,
    COMFORT == "Totalmente cómodo/a" ~ 1,
    TRUE ~ NA
  ))

              
#NCOMPLETE3M
meanandmore(datafinalALLCODESreg, NCOMPLETE3M)

datafinalALLCODESreg$logNCOMPLETE3M <- log(datafinalALLCODESreg$NCOMPLETE3M)

meanandmore(datafinalALLCODESreg, logNCOMPLETE3M)

#Use of voice or dictation in daily life

#FREQDICTATION
prop(datafinalALLCODESreg, FREQDICTATION)

#110 people said they did not know it exist: create specific dummy for those
datafinalALLCODESreg = datafinalALLCODESreg %>%
  mutate(DIDNOTKNOWDICTATION = case_when(
    FREQDICTATION == "No sabía que existía" ~ 1,
    FREQDICTATION != "No sabía que existía" ~ 0,
    TRUE ~ NA
  ))

#Create another dummy for those using it at least sometimes
datafinalALLCODESreg = datafinalALLCODESreg %>%
  mutate(USEDICTATIONDL =ifelse(FREQDICTATION != "No sabía que existía" & FREQDICTATION != "Nunca", 1, 0)) 

prop(datafinalALLCODESreg, DIDNOTKNOWDICTATION)
prop(datafinalALLCODESreg, USEDICTATIONDL)

#FREQVOICE
prop(datafinalALLCODESreg, FREQVOICE)

#36 people said they did not know it exist: create specific dummy for those
datafinalALLCODESreg = datafinalALLCODESreg %>%
  mutate(DIDNOTKNOWVOICE = case_when(
    FREQVOICE == "No sabía que existía" ~ 1,
    FREQVOICE != "No sabía que existía" ~ 0,
    TRUE ~ NA
  ))

#Create another dummy for those using it at least sometimes 
datafinalALLCODESreg = datafinalALLCODESreg %>%
  mutate(USEVOICEDL =ifelse(FREQVOICE != "No sabía que existía" & FREQVOICE  != "Nunca", 1, 0))

prop(datafinalALLCODESreg, DIDNOTKNOWVOICE)
prop(datafinalALLCODESreg, USEVOICEDL)

#DEVICE --> create dummy "mobile"
prop(datafinalALLCODESreg, DEVICE)

datafinalALLCODESreg = datafinalALLCODESreg %>%
  mutate(MOBILE = case_when(
    DEVICE == "Desktop" ~ 0,
    DEVICE == "Tablet" ~ 1,
    DEVICE == "Mobile" ~ 1,
    TRUE ~ NA
  ))

prop(datafinalALLCODESreg, MOBILE)

#MULTITASKING during survey
prop(datafinalALLCODESreg, TASK_1) #listen music or watch TV
prop(datafinalALLCODESreg, TASK_2) #speak with others
prop(datafinalALLCODESreg, TASK_3) #emails internet..
prop(datafinalALLCODESreg, TASK_4) #other

#to count the number of tasks -> create variable NBTASKS
tasks<-data.frame(datafinalALLCODESreg$TASK_1, datafinalALLCODESreg$TASK_2, datafinalALLCODESreg$TASK_3, datafinalALLCODESreg$TASK_4) #create a sub data with the 4 dummies

datafinalALLCODESreg$NBTASKS <- rowSums(tasks == "Sí") 

prop(datafinalALLCODESreg, NBTASKS)

#PLACE --> recode home vs the rest
prop(datafinalALLCODESreg, PLACE) 

datafinalALLCODESreg <- datafinalALLCODESreg %>% 
  mutate(HOME = case_when(
    PLACE == "En casa" ~ 1,
    PLACE != "En casa" ~ 0,
  ))

prop(datafinalALLCODESreg, HOME) 

#PEOPLE
prop(datafinalALLCODESreg, PEOPLE) 

datafinalALLCODESreg <- datafinalALLCODESreg %>% 
  mutate(PEOPLEAROUND = case_when(
    PEOPLE == "Sí" ~ 1,
    PLACE != "Sí" ~ 0,
  ))

prop(datafinalALLCODESreg, PEOPLEAROUND)

#extremity of opinion to previous question 

prop(datafinalALLCODESreg, TRANSPARENT)

datafinalALLCODESreg = datafinalALLCODESreg %>%
  mutate(TRANSP_EXTREME = case_when(
    TRANSPARENT == "Ninguna información" ~ 1,
    TRANSPARENT == "Poca información" ~ 0,
    TRANSPARENT == "Alguna información" ~ 0,
    TRANSPARENT == "Mucha información" ~ 0,
    TRANSPARENT == "Muchísima información" ~ 1,
    TRUE ~ NA
  ))

prop(datafinalALLCODESreg, TRANSP_EXTREME)

prop(datafinalALLCODESreg, TRUSTNH)

datafinalALLCODESreg = datafinalALLCODESreg %>%
  mutate(TRUST_EXTREME = case_when(
    TRUSTNH == "Nada" ~ 1,
    TRUSTNH == "Poco" ~ 0,
    TRUSTNH == "Algo" ~ 0,
    TRUSTNH == "Mucho" ~ 0,
    TRUSTNH == "Totalmente" ~ 1,
    TRUE ~ NA
  ))

prop(datafinalALLCODESreg, TRUST_EXTREME)



############################################################################
#Keep the dataset with all variables: the one used in most analyses
##############################################################################

write_rds(datafinalALLCODESreg,file.path(path,"datafinalALLCODESreg.rds"))

datafinalALLCODESregnoID = datafinalALLCODESreg %>%
  select(-id_ea)

write_rds(datafinalALLCODESregnoID,file.path(path,"datafinalALLCODESregnoID.rds"))


