#Completed in R version 4.2.1 (2022-06-23 ucrt) [platform: x86_64-w64-mingw32]
#Including the following packages:
library(haven) #package version 2.5.1
library(dplyr) #package version 1.0.10
library(stringr) #package version 1.4.1

#2.1. Sample

##2.1.0 Merging required SHARE datasets and variables

##Import data from the SHARE coverscreen file
variables <- c("mergeid", "country", "gender", "yrbirth", "mobirth", 
               "hhsize", "int_year", "int_month", "nursinghome")
SHAREdata <- read_sav("data/raw_data/sharew8_rel8-0-0_cv_r.sav") %>% 
  subset(., select = variables, interview == 1)

##Custom function to add variables from other SHARE datasets
SHAREjoin <- function(release_version, wave, questionnaire, variables) {
  dataset <- read_sav(paste0("data/raw_data/share", wave, "_rel", release_version, "_", questionnaire, ".sav"))
  removeitems <- c("_w9|_w8|_w7|_w6|_w5|_w4|_w3|_w2|_")
  dataset <- dataset %>% rename_all(list(~str_replace(., removeitems, "")))
  variables2 <- variables %>% str_replace(., removeitems, "")
  datamerged <- left_join(SHAREdata,
                          dataset %>% select(mergeid, all_of(variables2)),
                          by = "mergeid", all.x = TRUE)
  variables3 <- paste0(variables2, "_", wave)
  rename_with(datamerged, .fn = ~paste0(variables3), .cols = all_of(variables2))}

##Import other SHARE variables
variables <- c("cciw_w8_main")
SHAREdata <- SHAREjoin("8-0-0", "w8", "gv_weights", variables)

variables <- c("ep005_")
SHAREdata <- SHAREjoin("8-0-0", "w8", "ep", variables)

variables <- c("isced1997_r")
SHAREdata <- SHAREjoin("8-0-0", "w8", "gv_isced", variables)

variables <- c("ph005", "ph006d1", "ph006d2", "ph006d3", "ph006d4", "ph006d5", "ph006d6", 
               "ph006d10", "ph006d11", "ph006d12", "ph006d13", "ph006d14", "ph006d15", 
               "ph006d16", "ph006d18", "ph006d19", "ph006d20", "ph006d21", "ph043_", 
               "ph044_", "ph046_", "ph048d1", "ph048d5", "ph048d6", "ph048d9", "ph049d1", 
               "ph049d2", "ph049d3", "ph049d4", "ph049d5", "ph049d6","ph049d7","ph049d8", 
               "ph049d9", "ph049d10", "ph049d11", "ph049d12", "ph049d13", "ph049d14", 
               "ph049d15")
SHAREdata <- SHAREjoin("8-0-0", "w8", "ph", variables)

variables <- c("cf008tot", "cf016tot", "numeracy2", "euro8", "euro9", "gali", "sphus", "maxgrip")
SHAREdata <- SHAREjoin("8-0-0", "w8", "gv_health", variables)

variables <- c("br016_")
SHAREdata <- SHAREjoin("8-0-0", "w8", "br", variables)

variables <- c("co007_")
SHAREdata <- SHAREjoin("8-0-0", "w8", "co", variables)

variables <- c("hc012_")
SHAREdata <- SHAREjoin("8-0-0", "w8", "hc", variables)

variables <- c("cf003_", "cf004_", "cf005_", "cf006_", "cf010_", "cf823_", 
               "cf826_", "cf827_", "cf828_", "cf829_", "cf830_", "cf831_", 
               "cf833_", "cf834_", "cf835_", "cf837_", "cf838_", "cf839_", 
               "cf103_")
SHAREdata <- SHAREjoin("8-0-0", "w8", "cf", variables)

variables <- c("mn101_")
SHAREdata <- SHAREjoin("8-0-0", "w8", "technical_variables", variables)


##2.1.1 Aged ≥65 years, longitudinal questionnaire, 3 drawing tests not impaired)

SHAREdata <- SHAREdata %>%
  mutate(
    age_w8 = case_when( #Age calculated from year and month variables
      yrbirth > 0 & int_year > 0 & mobirth > 0 & int_month > 0 ~ (int_year + (int_month-1)/12) - (yrbirth + (mobirth-1)/12),
      yrbirth > 0 & int_year > 0 ~ int_year - yrbirth)) #less precise estimate for those without a month of birth

studysample1 <- SHAREdata[(SHAREdata$age_w8 >= 65 & !is.na(SHAREdata$age_w8)) & 
                            (SHAREdata$mn101_w8 == 1 & !is.na(SHAREdata$mn101_w8)) &
                            (!(SHAREdata$cf830_w8 == 7 | SHAREdata$cf831_w8 == 7 | SHAREdata$cf833_w8 == 7) | (is.na(SHAREdata$cf830_w8) | is.na(SHAREdata$cf831_w8 == 7) | is.na(SHAREdata$cf833_w8))),] #Will remove NA values later

##2.1.2 No missing data for variables of interest

studysample2 <- studysample1[
  !(#For cognitive variables -2 ("refused") was considered missing and -1 ("don't know") was considered a test fail
    studysample1$cf008tot_w8 == -2 | is.na(studysample1$cf008tot_w8) | #word registration  
      studysample1$cf010_w8 == -2 | is.na(studysample1$cf010_w8) |#verbal fluency
      studysample1$cf016tot_w8 == -2 | is.na(studysample1$cf016tot_w8) | #word recall
      studysample1$cf003_w8 == -2 | is.na(studysample1$cf003_w8) | #date
      studysample1$cf004_w8  == -2 | is.na(studysample1$cf004_w8) | #month
      studysample1$cf005_w8  == -2 | is.na(studysample1$cf005_w8) | #year
      studysample1$cf006_w8  == -2 | is.na(studysample1$cf006_w8) | #day of week
      studysample1$numeracy2_w8 == -2 | is.na(studysample1$numeracy2_w8) | #serial7s
      studysample1$cf823_w8 == -2 | is.na(studysample1$cf823_w8) | #counting backwards
      studysample1$cf827_w8 == -2 | is.na(studysample1$cf827_w8) | #object naming: scissors
      studysample1$cf828_w8 == -2 | is.na(studysample1$cf828_w8) | #object naming: cactus
      studysample1$cf829_w8 == -2 | is.na(studysample1$cf829_w8) | #object naming: pharmacy
      studysample1$cf830_w8 == -2 | is.na(studysample1$cf830_w8) | #copy infinity loop drawing
      studysample1$cf831_w8 == -2 | is.na(studysample1$cf831_w8) | #copy cube drawing
      studysample1$cf833_w8 == -2 | is.na(studysample1$cf833_w8)) & #draw clock 
    !(#For non-cognitive variables -2 ("refused") and -1 ("don't know") were considered missing
      studysample1$ph006d16_w8 < 0 | is.na(studysample1$ph006d16_w8) | #dementia/CI question
        studysample1$cf103_w8 < 0 | is.na(studysample1$cf103_w8) | #SMC question
        studysample1$ph049d7_w8 < 0 | is.na(studysample1$ph049d7_w8) | #IADL question(s)
        studysample1$isced1997r_w8 < 0 | is.na(studysample1$isced1997r_w8) | studysample1$isced1997r_w8 >= 95 | #Education Also excluded 95 ("still in school") and 97 ("other")
        studysample1$yrbirth < 0 | is.na(studysample1$yrbirth) | studysample1$int_year < 0 | is.na(studysample1$int_year) | #Age (calculated from birth/interview year)
        studysample1$gender < 0 | is.na(studysample1$gender) | #female sex (i.e. gender)
        studysample1$hhsize < 0 | is.na(studysample1$hhsize) | #Lives alone (i.e. household size)
        (is.na(studysample1$ph043_w8) | studysample1$ph043_w8 < 4) & (studysample1$ph044_w8 < 0 | is.na(studysample1$ph044_w8)) | #eyesight (distance + near, 4 and 5 are "fair" & "poor")
        (is.na(studysample1$ph044_w8) | studysample1$ph044_w8 < 4) & (studysample1$ph043_w8 < 0 | is.na(studysample1$ph043_w8)) | #eyesight (distance + near, 4 and 5 are "fair" & "poor")
        studysample1$ph046_w8 < 0 | is.na(studysample1$ph046_w8) | #hearing
        studysample1$sphus_w8 < 0 | is.na(studysample1$sphus_w8) | #Self-rated health
        studysample1$ph005_w8 < 0 | is.na(studysample1$ph005_w8) | #GALI
        (studysample1$ep005_w8 < 0 | is.na(studysample1$ep005_w8)) & (studysample1$nursinghome < 1 | is.na(studysample1$nursinghome)) | #employment
        rowSums(cbind(studysample1$euro8_w8 < 0 | is.na(studysample1$euro8_w8), #excluded if 3 or more missing frailty (SHARE-FI) items
                      studysample1$euro9_w8 < 0 | is.na(studysample1$euro9_w8),
                      studysample1$maxgrip_w8 < 0 | is.na(studysample1$maxgrip_w8),
                      studysample1$ph048d1_w8 < 0 | is.na(studysample1$ph048d1_w8),
                      studysample1$br016_w8 < 0 | is.na(studysample1$br016_w8))) >= 3 | 
        studysample1$hc012_w8 < 0 | is.na(studysample1$hc012_w8)) #hospitalisation
  ,]

##2.1.3 No unclear diagnostic categories 
##Script applied later on after diagnostic categories have been defined [see script 2.4. Cognitive Classifications]
##sample_primary <- studysample1[!(is.na(sample_primary$cognitivegroups_w8)),]
