require (lavaan)
install.packages ("foreign")
require (ggplot2)
require (reshape2)
require (foreign)
require (plyr)
install.packages ("psych")
require (car)
require (GPArotation)
require (psych)
install.packages ("haven")
require (haven)
getwd()
setwd("/Users/joannamchugh_1/Desktop")
install.packages ("tidyverse")
require (tidyverse)
covdata <- read.spss ("/Users/joannamchugh_1/Desktop/Longitudinal dataset_WAVE 1 2 3 4.sav", use.value.labels= TRUE, to.data.frame= TRUE, trim.factor.names = TRUE, use.missings = TRUE) # Change path (paths for PC have back slashes in the place of the forward slashes above which are Mac specific)

h19<- haven::read_sav ("/Users/joannamchugh_1/Desktop/Longitudinal dataset_WAVE 1 2 3 4.sav")
h19$C19_Infected %>% attr('label')


nrow (covdata)
require (dplyr)
names (covdata)
covdata %>% count (covdata$pid) 

# Wave 1 Loneliness - format for use in the model.----
table (covdata$Loneliness1)
covdata$Lon1_w1 = as.numeric (covdata$Loneliness1)
covdata$Lon1_w1 = car::recode (covdata$Lon1_w1, "1=0; 2=1; 3=2")
table (covdata$Lon1_w1)
# above four lines convert loneliness item 1 into a numeric variable with the values 0, 1, and 2 (as per Hughes' scoring, 2004)
table (covdata$Loneliness2)
covdata$Lon2_w1 = as.numeric (covdata$Loneliness2)
covdata$Lon2_w1 = car::recode (covdata$Lon2_w1, "1=0; 2=1; 3=2")
table (covdata$Lon2_w1)
table (covdata$Loneliness3)
covdata$Lon3_w1 = as.numeric (covdata$Loneliness3)
covdata$Lon3_w1 = car::recode (covdata$Lon3_w1, "1=0; 2=1; 3=2")
table (covdata$Lon3_w1)
covdata$LonT_w1 = covdata$Lon1_w1 + covdata$Lon2_w1 + covdata$Lon3_w1 # sums items into an overall total.

table (covdata$LonT_w1)
lonw1alpha = subset (covdata, select = c (Lon1_w1, Lon2_w1, Lon3_w1)) # creates a subsetted dataset on which to calculate cronbach's alpha
psych::alpha (lonw1alpha) # calculate alpha using the function in the "psych" package
psych::describe (covdata$LonT_w1) # basic descriptives using the describe function from the "psych" package (sometimes there are two functions in separate packages/in base R and you have to specify that you want the one from this package by typing e.g. "psych::" first )
# (click the arrow on the margin to expand each section)
# Wave 2 Loneliness - as above ----
table (covdata$W2Loneliness1)
covdata$Lon1_w2 = as.numeric (covdata$W2Loneliness1)
covdata$Lon1_w2 = car::recode (covdata$Lon1_w2, "1=0; 2=1; 3=2")
table (covdata$Lon1_w2)
table (covdata$W2Loneliness2)
covdata$Lon2_w2 = as.numeric (covdata$W2Loneliness2)
covdata$Lon2_w2 = car::recode (covdata$Lon2_w2, "1=0; 2=1; 3=2")
table (covdata$Lon2_w2)
table (covdata$W2Loneliness3)
covdata$Lon3_w2 = as.numeric (covdata$W2Loneliness3)
covdata$Lon3_w2 = car::recode (covdata$Lon3_w2, "1=0; 2=1; 3=2")
table (covdata$Lon3_w2)
covdata$LonT_w2 = covdata$Lon1_w2 + covdata$Lon2_w2 + covdata$Lon3_w2
table (covdata$LonT_w2)
lonw2alpha = subset (covdata, select = c (Lon1_w2, Lon2_w2, Lon3_w2))
psych::alpha (lonw2alpha)
psych::describe (covdata$LonT_w2)


# Wave 3 Loneliness----
table (covdata$W3_Loneliness1)
covdata$Lon1_w3 = as.numeric (covdata$W3_Loneliness1)
covdata$Lon1_w3 = car::recode (covdata$Lon1_w3, "1=0; 2=1; 3=2")
table (covdata$Lon1_w3)
table (covdata$W3_Loneliness2)
covdata$Lon2_w3 = as.numeric (covdata$W3_Loneliness2)
covdata$Lon2_w3 = car::recode (covdata$Lon2_w3, "1=0; 2=1; 3=2")
table (covdata$Lon2_w3)
table (covdata$W3_Loneliness3)
covdata$Lon3_w3 = as.numeric (covdata$W3_Loneliness3)
covdata$Lon3_w3 = car::recode (covdata$Lon3_w3, "1=0; 2=1; 3=2")
table (covdata$Lon3_w3)
covdata$LonT_w3 = covdata$Lon1_w3 + covdata$Lon2_w3 + covdata$Lon3_w3
table (covdata$LonT_w3)
lonw3alpha = subset (covdata, select = c (Lon1_w3, Lon2_w3, Lon3_w3))
psych::alpha (lonw3alpha)
psych::describe (covdata$LonT_w3)

# Wave 4 Loneliness----
table (covdata$W4_Loneliness1)
covdata$Lon1_w4 = as.numeric (covdata$W4_Loneliness1)
covdata$Lon1_w4 = car::recode (covdata$Lon1_w4, "1=0; 2=1; 3=2")
table (covdata$Lon1_w4)
table (covdata$W4_Loneliness2)
covdata$Lon2_w4 = as.numeric (covdata$W4_Loneliness2)
covdata$Lon2_w4 = car::recode (covdata$Lon2_w4, "1=0; 2=1; 3=2")
table (covdata$Lon2_w4)
table (covdata$W4_Loneliness3)
covdata$Lon3_w4 = as.numeric (covdata$W4_Loneliness3)
covdata$Lon3_w4 = car::recode (covdata$Lon3_w4, "1=0; 2=1; 3=2")
table (covdata$Lon3_w4)
covdata$LonT_w4 = covdata$Lon1_w4 + covdata$Lon2_w4 + covdata$Lon3_w4
table (covdata$LonT_w4)
lonw4alpha = subset (covdata, select = c (Lon1_w4, Lon2_w4, Lon3_w4))
psych::alpha (lonw4alpha)
psych::describe (covdata$LonT_w4)


# Wave 1 GAD (The GAD-7 score is calculated by assigning scores of 0, 1, 2, and 3, to the response categories of 'not at all', 'several days', 'more than half the days', and 'nearly every day', respectively, and adding together the scores for the seven questions).----
table (covdata$GAD_1)
covdata$GAD1w1 = as.numeric (covdata$GAD_1)
table (covdata$GAD1w1)
covdata$GAD1w1 = car::recode (covdata$GAD1w1, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD1w1)

table (covdata$GAD_2)
covdata$GAD2w1 = as.numeric (covdata$GAD_2)
table (covdata$GAD2w1)
covdata$GAD2w1 = car::recode (covdata$GAD2w1, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD2w1)

table (covdata$GAD_3)
covdata$GAD3w1 = as.numeric (covdata$GAD_3)
table (covdata$GAD3w1)
covdata$GAD3w1 = car::recode (covdata$GAD3w1, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD3w1)

table (covdata$GAD_4)
covdata$GAD4w1 = as.numeric (covdata$GAD_4)
table (covdata$GAD4w1)
covdata$GAD4w1 = car::recode (covdata$GAD4w1, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD4w1)

table (covdata$GAD_5)
covdata$GAD5w1 = as.numeric (covdata$GAD_5)
table (covdata$GAD5w1)
covdata$GAD5w1 = car::recode (covdata$GAD5w1, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD5w1)

table (covdata$GAD_6)
covdata$GAD6w1 = as.numeric (covdata$GAD_6)
table (covdata$GAD6w1)
covdata$GAD6w1 = car::recode (covdata$GAD6w1, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD6w1)

table (covdata$GAD_7)
covdata$GAD7w1 = as.numeric (covdata$GAD_7)
table (covdata$GAD7w1)
covdata$GAD7w1 = car::recode (covdata$GAD7w1, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD7w1)

covdata$GADw1 = (covdata$GAD1w1 + covdata$GAD2w1 + covdata$GAD3w1 + covdata$GAD4w1 + covdata$GAD5w1 + covdata$GAD6w1 + covdata$GAD7w1)
psych::describe (covdata$GADw1)
GAD1alpha <- subset(covdata, select = c(GAD1w1, GAD2w1, GAD3w1, GAD4w1, GAD5w1, GAD6w1, GAD7w1))
psych::alpha (GAD1alpha)

# GAD wave 2 ----
table (covdata$W2_GAD_1)
covdata$GAD1w2 = as.numeric (covdata$W2_GAD_1)
table (covdata$GAD1w2)
covdata$GAD1w2 = car::recode (covdata$GAD1w2, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD1w2)

table (covdata$W2_GAD_2)
covdata$GAD2w2 = as.numeric (covdata$W2_GAD_2)
table (covdata$GAD2w2)
covdata$GAD2w1 = car::recode (covdata$GAD2w1, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD2w2)

table (covdata$W2_GAD_3)
covdata$GAD3w2 = as.numeric (covdata$W2_GAD_3)
table (covdata$GAD3w2)
covdata$GAD3w2 = car::recode (covdata$GAD3w2, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD3w2)

table (covdata$W2_GAD_4)
covdata$GAD4w2 = as.numeric (covdata$W2_GAD_4)
table (covdata$GAD4w2)
covdata$GAD4w2 = car::recode (covdata$GAD4w2, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD4w2)

table (covdata$W2_GAD_5)
covdata$GAD5w2 = as.numeric (covdata$W2_GAD_5)
table (covdata$GAD5w2)
covdata$GAD5w2 = car::recode (covdata$GAD5w2, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD5w2)

table (covdata$W2_GAD_6)
covdata$GAD6w2 = as.numeric (covdata$W2_GAD_6)
table (covdata$GAD6w2)
covdata$GAD6w2 = car::recode (covdata$GAD6w2, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD6w2)

table (covdata$W2_GAD_7)
covdata$GAD7w2 = as.numeric (covdata$W2_GAD_7)
table (covdata$GAD7w2)
covdata$GAD7w2 = car::recode (covdata$GAD7w2, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD7w2)

covdata$GADw2 = (covdata$GAD1w2 + covdata$GAD2w2 + covdata$GAD3w2 + covdata$GAD4w2 + covdata$GAD5w2 + covdata$GAD6w2 + covdata$GAD7w2)
psych::describe (covdata$GADw2)
GAD2alpha <- subset(covdata, select = c(GAD1w2, GAD2w2, GAD3w2, GAD4w2, GAD5w2, GAD6w2, GAD7w2))
psych::alpha (GAD2alpha)



# GAD wave 3----
names (covdata)
table (covdata$W3_GAD1)
covdata$GAD1w3 = as.numeric (covdata$W3_GAD1)
table (covdata$GAD1w3)
covdata$GAD1w3 = car::recode (covdata$GAD1w3, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD1w3)

table (covdata$W3_GAD2)
covdata$GAD2w3 = as.numeric (covdata$W3_GAD2)
table (covdata$GAD2w3)
covdata$GAD2w3 = car::recode (covdata$GAD2w3, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD2w3)

table (covdata$W3_GAD3)
covdata$GAD3w3 = as.numeric (covdata$W3_GAD3)
table (covdata$GAD3w3)
covdata$GAD3w3 = car::recode (covdata$GAD3w3, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD3w3)

table (covdata$W3_GAD4)
covdata$GAD4w3 = as.numeric (covdata$W3_GAD4)
table (covdata$GAD4w3)
covdata$GAD4w3 = car::recode (covdata$GAD4w3, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD4w3)

table (covdata$W3_GAD5)
covdata$GAD5w3 = as.numeric (covdata$W3_GAD5)
table (covdata$GAD5w3)
covdata$GAD5w3 = car::recode (covdata$GAD5w3, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD5w3)

table (covdata$W3_GAD6)
covdata$GAD6w3 = as.numeric (covdata$W3_GAD6)
table (covdata$GAD6w3)
covdata$GAD6w3 = car::recode (covdata$GAD6w3, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD6w3)

table (covdata$W3_GAD7)
covdata$GAD7w3 = as.numeric (covdata$W3_GAD7)
table (covdata$GAD7w3)
covdata$GAD7w3 = car::recode (covdata$GAD7w3, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD7w3)

covdata$GADw3 = (covdata$GAD1w3 + covdata$GAD2w3 + covdata$GAD3w3 + covdata$GAD4w3 + covdata$GAD5w3 + covdata$GAD6w3 + covdata$GAD7w3)
psych::describe (covdata$GADw3)
GAD3alpha <- subset(covdata, select = c(GAD1w3, GAD2w3, GAD3w3, GAD4w3, GAD5w3, GAD6w3, GAD7w3))
psych::alpha (GAD3alpha)

# GAD wave 4----

names (covdata)
table (covdata$W4_GAD1)
covdata$GAD1w4 = as.numeric (covdata$W4_GAD1)
table (covdata$GAD1w4)
covdata$GAD1w4 = car::recode (covdata$GAD1w4, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD1w4)

table (covdata$W4_GAD2)
covdata$GAD2w4 = as.numeric (covdata$W4_GAD2)
table (covdata$GAD2w4)
covdata$GAD2w4 = car::recode (covdata$GAD2w4, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD2w4)

table (covdata$W4_GAD3)
covdata$GAD3w4 = as.numeric (covdata$W4_GAD3)
table (covdata$GAD3w4)
covdata$GAD3w4 = car::recode (covdata$GAD3w4, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD3w4)

table (covdata$W4_GAD4)
covdata$GAD4w4 = as.numeric (covdata$W4_GAD4)
table (covdata$GAD4w4)
covdata$GAD4w4 = car::recode (covdata$GAD4w4, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD4w4)

table (covdata$W4_GAD5)
covdata$GAD5w4 = as.numeric (covdata$W4_GAD5)
table (covdata$GAD5w4)
covdata$GAD5w4 = car::recode (covdata$GAD5w4, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD5w4)

table (covdata$W4_GAD6)
covdata$GAD6w4 = as.numeric (covdata$W4_GAD6)
table (covdata$GAD6w4)
covdata$GAD6w4 = car::recode (covdata$GAD6w4, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD6w4)

table (covdata$W4_GAD7)
covdata$GAD7w4 = as.numeric (covdata$W4_GAD7)
table (covdata$GAD7w4)
covdata$GAD7w4 = car::recode (covdata$GAD7w4, "1=0; 2=1; 3=2; 4=3")
table (covdata$GAD7w4)


covdata$GADw4 = (covdata$GAD1w4 + covdata$GAD2w4 + covdata$GAD3w4 + covdata$GAD4w4 + covdata$GAD5w4 + covdata$GAD6w4 + covdata$GAD7w4)
psych::describe (covdata$GADw4)
GAD4alpha <- subset(covdata, select = c(GAD1w4, GAD2w4, GAD3w4, GAD4w4, GAD5w4, GAD6w4, GAD7w4))
psych::alpha (GAD4alpha)


######### Existential Loneliness  #######
levels (covdata$W4_EXL1) # I amsurrounded by strangers i cannot connect with
covdata$W4_EXL1n = as.numeric(covdata$W4_EXL1)
table (covdata$W4_EXL1)
table (covdata$W4_EXL1n)
covdata$W4_EXL1n <- car::recode (covdata$W4_EXL1n, "1=0; 2=0; 3=1; 4=1; 5=1")
table (covdata$W4_EXL1n)

table (covdata$W4_EXL2) # I feel I have people i can trust and rely on if i need them
covdata$W4_EXL2n = as.numeric (covdata$W4_EXL2)
table (covdata$W4_EXL2n)
covdata$W4_EXL2n <- car::recode (covdata$W4_EXL2n, "1=0; 2=0; 3=1; 4=1; 5=1")

table (covdata$W4_EXL3) # I stay in bad relationships too long in order not to be alone
covdata$W4_EXL3n = as.numeric (covdata$W4_EXL3)
table (covdata$W4_EXL3n)
covdata$W4_EXL3n <- car::recode (covdata$W4_EXL3n, "1=0; 2=0; 3=1; 4=1; 5=1")
table (covdata$W4_EXL3n)

table (covdata$W4_EXL4) # I mean osmething to others
covdata$W4_EXL4n = as.numeric (covdata$W4_EXL4)
table (covdata$W4_EXL4n)
covdata$W4_EXL4n <- car::recode (covdata$W4_EXL4n, "1=0; 2=0; 3=1; 4=1; 5=1")
table (covdata$W4_EXL4n)

table (covdata$W4_EXL5) # Important relationships have ended or become weaker
covdata$W4_EXL5n = as.numeric (covdata$W4_EXL5)
table (covdata$W4_EXL5n)
covdata$W4_EXL5n <- car::recode (covdata$W4_EXL5n, "1=0; 2=0; 3=1; 4=1; 5=1")
table (covdata$W4_EXL5n)

table (covdata$W4_EXL6) # no one else in the world can understand my feelings
covdata$W4_EXL6n = as.numeric (covdata$W4_EXL6)
table (covdata$W4_EXL6n)
covdata$W4_EXL6n <- car::recode (covdata$W4_EXL6n, "1=0; 2=0; 3=1; 4=1; 5=1")
table (covdata$W4_EXL6n)

table (covdata$W4_EXL7) # my world seems so different from everyone else's
covdata$W4_EXL7n = as.numeric (covdata$W4_EXL7)
table (covdata$W4_EXL7n)
covdata$W4_EXL7n <- car::recode (covdata$W4_EXL7n, "1=0; 2=0; 3=1; 4=1; 5=1")
table (covdata$W4_EXL7n)

table (covdata$W4_EXL8) # I am happy with the way i have lived my life
covdata$W4_EXL8n = as.numeric (covdata$W4_EXL8)
table (covdata$W4_EXL8n)
covdata$W4_EXL8n <- car::recode (covdata$W4_EXL8n, "1=0; 2=0; 3=1; 4=1; 5=1")
table (covdata$W4_EXL8n)

table (covdata$W4_EXL9) # there is a purpose to my life
covdata$W4_EXL9n = as.numeric (covdata$W4_EXL9)
table (covdata$W4_EXL9n)
covdata$W4_EXL9n <- car::recode (covdata$W4_EXL9n, "1=0; 2=0; 3=1; 4=1; 5=1")
table (covdata$W4_EXL9n)

table (covdata$W4_EXL10) # I feel helpless
covdata$W4_EXL10n = as.numeric (covdata$W4_EXL10)
table (covdata$W4_EXL10n)
covdata$W4_EXL10n <- car::recode (covdata$W4_EXL10n, "1=0; 2=0; 3=1; 4=1; 5=1")
table (covdata$W4_EXL10n)

table (covdata$W4_EXL11) # I feel at the mercy of the world
covdata$W4_EXL11n = as.numeric (covdata$W4_EXL11)
table (covdata$W4_EXL11n)
covdata$W4_EXL11n <- car::recode (covdata$W4_EXL11n, "1=0; 2=0; 3=1; 4=1; 5=1")
table (covdata$W4_EXL11n)

table (covdata$W4_EXL12) # i feel dead
covdata$W4_EXL12n = as.numeric (covdata$W4_EXL12)
table (covdata$W4_EXL12n)
covdata$W4_EXL12n <- car::recode (covdata$W4_EXL12n, "1=0; 2=0; 3=1; 4=1; 5=1")
table (covdata$W4_EXL12n)

table (covdata$W4_EXL13) # the universe is full of meaning
covdata$W4_EXL13n = as.numeric (covdata$W4_EXL13)
table (covdata$W4_EXL13n)
covdata$W4_EXL13n <- car::recode (covdata$W4_EXL13n, "1=0; 2=0; 3=1; 4=1; 5=1")
table (covdata$W4_EXL13n)

table (covdata$W4_EXL14) # i feel that there is little point to life
covdata$W4_EXL14n = as.numeric (covdata$W4_EXL14)
table (covdata$W4_EXL14n)
covdata$W4_EXL14n <- car::recode (covdata$W4_EXL14n, "1=0; 2=0; 3=1; 4=1; 5=1")
table (covdata$W4_EXL14n)

table (covdata$W4_Existential_Loneliness)
psych::describe (covdata$W4_Existential_Loneliness)
covdata$W4_EL <- (covdata$W4_EXL1n + covdata$W4_EXL2n + covdata$W4_EXL3n + covdata$W4_EXL4n + covdata$W4_EXL5n + covdata$W4_EXL6n + covdata$W4_EXL7n + covdata$W4_EXL8n + covdata$W4_EXL9n + covdata$W4_EXL10n + covdata$W4_EXL11n + covdata$W4_EXL12n + covdata$W4_EXL13n + covdata$W4_EXL14n)
psych::describe (covdata$W4_EL)
names (covdata)
ELalpha <- subset (covdata, select = c(W4_EXL1n, W4_EXL2n, W4_EXL3n ,W4_EXL4n , W4_EXL5n , W4_EXL6n , W4_EXL7n , W4_EXL8n , W4_EXL9n,W4_EXL10n , W4_EXL11n , W4_EXL12n , W4_EXL13n , W4_EXL14n))

require (QuantPsyc)

psych::alpha (ELalpha)

psych::omega (ELalpha)


# Depression Wave 1---- 

table (covdata$DEP1w1)
covdata$DEP1w1 = as.numeric (covdata$DEP1)
covdata$DEP1w1 = car::recode (covdata$DEP1w1, "1=0; 2=1; 3=2; 4=3")
table (covdata$DEP2)
covdata$DEP2w1 = as.numeric (covdata$DEP2)
covdata$DEP2w1 = car::recode (covdata$DEP2w1, "1=0; 2=1; 3=2; 4=3")
table (covdata$DEP2w1)
table (covdata$DEP3w1)
covdata$DEP3w1 = as.numeric (covdata$DEP3)
covdata$DEP3w1 = car::recode (covdata$DEP3w1, "1=0; 2=1; 3=2; 4=3")
table (covdata$DEP4)
covdata$DEP4w1 = as.numeric (covdata$DEP4)
covdata$DEP4w1 = car::recode (covdata$DEP4w1,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP4w1)
table (covdata$DEP5) 
covdata$DEP5w1 = as.numeric (covdata$DEP5)
covdata$DEP5w1 = car::recode (covdata$DEP5w1,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP5w1)
table (covdata$DEP6)
covdata$DEP6w1 = as.numeric (covdata$DEP6)
covdata$DEP6w1 = car::recode (covdata$DEP6w1,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP6w1)
table (covdata$DEP7)
covdata$DEP7w1 = as.numeric (covdata$DEP7)
covdata$DEP7w1 = car::recode (covdata$DEP7w1,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP7w1)
table (covdata$DEP8)
covdata$DEP8w1 = as.numeric (covdata$DEP8)
covdata$DEP8w1 = car::recode (covdata$DEP8w1,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP8w1)
table (covdata$DEP9)
covdata$DEP9w1 = as.numeric (covdata$DEP9)
covdata$DEP9w1 = car::recode (covdata$DEP9w1,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP9w1)
depw1alpha <- subset (covdata, select = c(DEP1w1, DEP2w1, DEP3w1, DEP4w1, DEP5w1, DEP6w1, DEP7w1, DEP8w1, DEP9w1))


psych::alpha(depw1alpha)
covdata$depw1 <- covdata$DEP1w1 + covdata$DEP2w1 + covdata$DEP3w1 + covdata$DEP4w1 + covdata$DEP5w1 + covdata$DEP6w1 + covdata$DEP7w1 + covdata$DEP8w1 + covdata$DEP9w1
psych::describe (covdata$depw1)
table(covdata$Depression_cat)
prop.table(table(covdata$Depression_cat))
table (covdata$Depression)
covdata$depcat = cut (covdata$depw1, breaks = c(0, 4, 9, 14, 19, 27), labels = c("0-5", "5-9","10-14", "15-19", "20+"), include.lowest = TRUE)
table (covdata$depcat)
prop.table(table(covdata$depcat))


# Depression Wave 2----

table (covdata$W2_DEP1)
covdata$DEP1w2 = as.numeric (covdata$W2_DEP1)
covdata$DEP1w2 = car::recode (covdata$DEP1w2, "1=0; 2=1; 3=2; 4=3")
table (covdata$DEP1w2)
table (covdata$W2_DEP2)
covdata$DEP2w2 = as.numeric (covdata$W2_DEP2)
covdata$DEP2w2 = car::recode (covdata$DEP2w2, "1=0; 2=1; 3=2; 4=3")
table (covdata$DEP2w2)
table (covdata$W2_DEP3)
covdata$DEP3w2 = as.numeric (covdata$W2_DEP3)
covdata$DEP3w2 = car::recode (covdata$DEP3w2, "1=0; 2=1; 3=2; 4=3")
table (covdata$DEP3w2)
table (covdata$W2_DEP4)
covdata$DEP4w2 = as.numeric (covdata$W2_DEP4)
covdata$DEP4w2 = car::recode (covdata$DEP4w2,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP4w2)
table (covdata$W2_DEP5) 
covdata$DEP5w2 = as.numeric (covdata$W2_DEP5)
covdata$DEP5w2 = car::recode (covdata$DEP5w2,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP5w2)
table (covdata$W2_DEP6)
covdata$DEP6w2 = as.numeric (covdata$W2_DEP6)
covdata$DEP6w2 = car::recode (covdata$DEP6w2,"1=0; 2=1; 3=2; 4=3")
table (covdata$DEP6w2)
table (covdata$W2_DEP7)
covdata$DEP7w2 = as.numeric (covdata$W2_DEP7)
covdata$DEP7w2 = car::recode (covdata$DEP7w2,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP7w2)
table (covdata$W2_DEP8)
covdata$DEP8w2 = as.numeric (covdata$W2_DEP8)
covdata$DEP8w2 = car::recode (covdata$DEP8w2,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP8w2)
table (covdata$W2_DEP9)
covdata$DEP9w2 = as.numeric (covdata$W2_DEP9)
covdata$DEP9w2 = car::recode (covdata$DEP9w2,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP9w2)
depw2alpha <- subset (covdata, select = c(DEP1w2, DEP2w2, DEP3w2, DEP4w2, DEP5w2, DEP6w2, DEP7w2, DEP8w2, DEP9w2))
psych::alpha(depw2alpha)
covdata$depw2 <- covdata$DEP1w2 + covdata$DEP2w2 + covdata$DEP3w2 + covdata$DEP4w2 + covdata$DEP5w2 + covdata$DEP6w2 + covdata$DEP7w2 + covdata$DEP8w2 + covdata$DEP9w2
psych::describe (covdata$depw2)
covdata$depcatw2 = cut (covdata$depw2, breaks = c(0, 4, 9, 14, 19, 27), labels = c("0-5", "5-9","10-14", "15-19", "20+"), include.lowest = TRUE)
table (covdata$depcatw2)
prop.table(table(covdata$depcatw2))


# Depression Wave 3----

table (covdata$W3_DEP1)
covdata$DEP1w3 = as.numeric (covdata$W3_DEP1)
covdata$DEP1w3 = car::recode (covdata$DEP1w3, "1=0; 2=1; 3=2; 4=3")
table (covdata$DEP1w3)
table (covdata$W3_DEP2)
covdata$DEP2w3 = as.numeric (covdata$W3_DEP2)
covdata$DEP2w3 = car::recode (covdata$DEP2w3, "1=0; 2=1; 3=2; 4=3")
table (covdata$DEP2w3)
table (covdata$W3_DEP3)
covdata$DEP3w3 = as.numeric (covdata$W3_DEP3)
covdata$DEP3w3 = car::recode (covdata$DEP3w3, "1=0; 2=1; 3=2; 4=3")
table (covdata$DEP3w3)
table (covdata$W3_DEP4)
covdata$DEP4w3 = as.numeric (covdata$W3_DEP4)
covdata$DEP4w3 = car::recode (covdata$DEP4w3,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP4w3)
table (covdata$W3_DEP5) 
covdata$DEP5w3 = as.numeric (covdata$W3_DEP5)
covdata$DEP5w3 = car::recode (covdata$DEP5w3,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP5w3)
table (covdata$W3_DEP6)
covdata$DEP6w3 = as.numeric (covdata$W3_DEP6)
covdata$DEP6w3 = car::recode (covdata$DEP6w3,"1=0; 2=1; 3=2; 4=3")
table (covdata$DEP6w3)
table (covdata$W3_DEP7)
covdata$DEP7w3 = as.numeric (covdata$W3_DEP7)
covdata$DEP7w3 = car::recode (covdata$DEP7w3,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP7w3)
table (covdata$W3_DEP8)
covdata$DEP8w3 = as.numeric (covdata$W3_DEP8)
covdata$DEP8w3 = car::recode (covdata$DEP8w3,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP8w3)
table (covdata$W3_DEP9)
covdata$DEP9w3 = as.numeric (covdata$W3_DEP9)
covdata$DEP9w3 = car::recode (covdata$DEP9w3,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP9w3)
depw3alpha <- subset (covdata, select = c(DEP1w3, DEP2w3, DEP3w3, DEP4w3, DEP5w3, DEP6w3, DEP7w3, DEP8w3, DEP9w3))
psych::alpha(depw3alpha)
covdata$depw3 <- covdata$DEP1w3 + covdata$DEP2w3 + covdata$DEP3w3 + covdata$DEP4w3 + covdata$DEP5w3 + covdata$DEP6w3 + covdata$DEP7w3 + covdata$DEP8w3 + covdata$DEP9w3
psych::describe (covdata$depw3)
covdata$depcatw3 = cut (covdata$depw3, breaks = c(0, 4, 9, 14, 19, 27), labels = c("0-5", "5-9","10-14", "15-19", "20+"), include.lowest = TRUE)
table (covdata$depcatw3)
prop.table(table(covdata$depcatw3))


# Depression Wave 4----

table (covdata$W4_DEP1)
covdata$DEP1w4 = as.numeric (covdata$W4_DEP1)
covdata$DEP1w4 = car::recode (covdata$DEP1w4, "1=0; 2=1; 3=2; 4=3")
table (covdata$DEP1w4)
table (covdata$W4_DEP2)
covdata$DEP2w4 = as.numeric (covdata$W4_DEP2)
covdata$DEP2w4 = car::recode (covdata$DEP2w4, "1=0; 2=1; 3=2; 4=3")
table (covdata$DEP2w4)
table (covdata$W4_DEP3)
covdata$DEP3w4 = as.numeric (covdata$W4_DEP3)
covdata$DEP3w4 = car::recode (covdata$DEP3w4, "1=0; 2=1; 3=2; 4=3")
table (covdata$DEP3w4)
table (covdata$W4_DEP4)
covdata$DEP4w4 = as.numeric (covdata$W4_DEP4)
covdata$DEP4w4 = car::recode (covdata$DEP4w4,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP4w4)
table (covdata$W4_DEP5) 
covdata$DEP5w4 = as.numeric (covdata$W4_DEP5)
covdata$DEP5w4 = car::recode (covdata$DEP5w4,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP5w4)
table (covdata$W4_DEP6)
covdata$DEP6w4 = as.numeric (covdata$W4_DEP6)
covdata$DEP6w4 = car::recode (covdata$DEP6w4,"1=0; 2=1; 3=2; 4=3")
table (covdata$DEP6w4)
table (covdata$W4_DEP7)
covdata$DEP7w4 = as.numeric (covdata$W4_DEP7)
covdata$DEP7w4 = car::recode (covdata$DEP7w4,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP7w4)
table (covdata$W4_DEP8)
covdata$DEP8w4 = as.numeric (covdata$W4_DEP8)
covdata$DEP8w4 = car::recode (covdata$DEP8w4,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP8w4)
table (covdata$W4_DEP9)
covdata$DEP9w4 = as.numeric (covdata$W4_DEP9)
covdata$DEP9w4 = car::recode (covdata$DEP9w4,"1=0; 2=1; 3=2; 4=3" )
table (covdata$DEP9w4)
depw4alpha <- subset (covdata, select = c(DEP1w4, DEP2w4, DEP3w4, DEP4w4, DEP5w4, DEP6w4, DEP7w4, DEP8w4, DEP9w4))
psych::alpha(depw4alpha)
covdata$depw4 <- covdata$DEP1w4 + covdata$DEP2w4 + covdata$DEP3w4 + covdata$DEP4w4 + covdata$DEP5w4 + covdata$DEP6w4 + covdata$DEP7w4 + covdata$DEP8w4 + covdata$DEP9w4
psych::describe (covdata$depw4)
covdata$depcatw4 = cut (covdata$depw4, breaks = c(0, 4, 9, 14, 19, 27), labels = c("0-5", "5-9","10-14", "15-19", "20+"), include.lowest = TRUE)
table (covdata$depcatw4)
prop.table(table(covdata$depcatw4))



# Covariates! and subsetting----- 
# (From Lee: age, sex, ethnicity, marital status, education, employment status, wealth, long-term physical illness, mobility impairment, pain, BMI, cognitive functioning, and baseline depressive symptoms, as well as social network size, frequency of social contact, participation in social groups, and perceived social support)


names (covdata)
psych::describe (covdata$Age_years)
prop.table (table(covdata$Sex))
table (covdata$Sex)
table (covdata$Ethnicity)
table (covdata$Ethnicity_Binary)
prop.table(table(covdata$Ethnicity_Binary))
table (covdata$W2_Marital_Status)
table (covdata$W2_Marital_Status_Binary)
prop.table (table (covdata$W2_Marital_Status_Binary))
table (covdata$Education)
levels (covdata$Education)
covdata$ed <- mapvalues (covdata$Education, from = c("No Qualifications","Junior / Inter Cert","Leaving cert","Undergraduate degree","Diploma", "Postgraduate degree","Other qualifications","Technical qualification"), to = c("none", "second level", "second level", "third level", "third level", "third level", NA, NA) )
table (covdata$ed)
prop.table(table(covdata$ed))
table (covdata$Employment)
levels (covdata$Employment)
covdata$emp <- mapvalues (covdata$Employment, from = c ("Employed full time", "Self-employed full time", "Employed part time", "Self-employed part time", "Recently unemployed due to the Coronavirus", "Unemployed not due to the Coronavirus", "Retired", "Student", "Cannot work due to disability, illness, or some other reason"), to = c("Employed", "Employed", "Employed", "Employed", "Unemployed - COVID", "Unemployed", "Unemployed", "Unemployed", "Unemployed"))
table (covdata$emp)
prop.table(table(covdata$emp))
covdata$emp2 <- mapvalues (covdata$emp, from = c("Employed", "Unemployed - COVID", "Unemployed"), to = c("Employed", "Unemployed", "Unemployed"))
table (covdata$emp2)
table (covdata$Income2019)
prop.table (table(covdata$Income2019))
class (covdata$Income2019)
covdata$IncomeOrdered <- factor (covdata$Income2019, order = TRUE)
table(covdata$IncomeOrdered)

table (covdata$Mental_Health_Treatment)
levels (covdata$Mental_Health_Treatment)
prop.table (table (covdata$Mental_Health_Treatment))

prop.table(table (covdata$Live_Alone))
class (covdata$Live_Alone)
covdata$LiveAloneN = as.numeric (covdata$Live_Alone)
table (covdata$LiveAloneN)
covdata$LAN = car::recode (covdata$LiveAloneN, "1=0; 2=1")
prop.table(table (covdata$LAN))
covdata$LAorder = factor(covdata$Live_Alone, order = TRUE)
table (covdata$LiveAloneN)



table (covdata$W4_SocialContact1) # How often are you in contact with any members of your family, that is, any of your brothers, sisters, parents, or children who do not live with you, including visits, phone calls, letters, or emails? 
table (covdata$W4_SocialContact2) # How often are you in contact with any of your friends, including visits, phone calls, letters, or emails? 
covdata$W4_SCn <- as.numeric (covdata$W4_SocialContact1)
table (covdata$W4_SCn)
covdata$W4_SCn = car::recode (covdata$W4_SCn, "1=1; 2=1; 3=1; 4=1; 5=0; 6=0; 7=0; 8=0")
table (covdata$W4_SCn)
covdata$W4SC2n <- as.numeric (covdata$W4_SocialContact2)
table (covdata$W4SC2n)
covdata$W4SC2n = car::recode (covdata$W4SC2n, "1=1; 2=1; 3=1; 4=1; 5=0; 6=0; 7=0; 8=0")
table (covdata$LAN)
table (covdata$W4_IPV_Relationship_Now)

covdata$RelN = as.numeric (covdata$W4_IPV_Relationship_Now)
table (covdata$RelN)
covdata$RelN = car::recode (covdata$RelN, "1=1; 2=0")
table (covdata$W4_Children)
#SocSup1
class (covdata$W4_SocialSupport1) #how often is each of the following kinds of support available to you if you need it - to help if you were confined to bed
table (covdata$W4_SocialSupport2) # to take you to the doctor if you need it
table (covdata$W4_SocialSupport3) # to prepare your meals if you are unable to do it yourself
table (covdata$W4_SocialSupport4) # to help with daily chores if you were sick
table (covdata$W4_SocialSupport5) # to have a good time with
table (covdata$W4_SocialSupport6) # to turn to for suggestions about how to deal with a personal problem
table (covdata$W4_SocialSupport7) # who understands your problems
table (covdata$W4_SocialSupport8) # to love and make you feel wanted
# Empathy
table (covdata$W4_Empathy1) # how much do you identify with (feel a part of, feel love toward, have concern for) each of the following? people in my community

names (covdata)
table (covdata$W4_Empathy2) # people from Ireland
table (covdata$W4_Empathy3) # all humans everwyhwere
table (covdata$W4_Empathy4) # how much would you say you care (feel upset, want to help) when bad things happen to each of the following: people in my community
table (covdata$W4_Empathy5) # people from Ireland
table (covdata$W4_Empathy6) # all humans everywhere
table (covdata$W4_Empathy7) # when they are in need, how much do you want to help each of the following? people in my community
table (covdata$W4_Empathy8) # people from Ireland
table (covdata$W4_Empathy9) # all humans everywhere
names (covdata)
# NB chronic health problem is wave 4. 
cov <- subset (covdata, select = c("pid", "Age_years", "Sex", "Ethnicity_Binary", "W2_Marital_Status_Binary","ed", "emp2", "IncomeOrdered", "Chronic_Illness_self",  "Mental_Health_Treatment", "Employ_HealthCare", "Neighborhood_Belongingness", "Dx_health_preC19","TRUST_1", "TRUST_2" , "TRUST_3", "TRUST_4", "TRUST_5" , "TRUST_6" , "TRUST_7" , "TRUST_8" , "TRUST_9", "C19_Infected", "Self_Esteem" , "W2_C19_Died_SomeoneClose",   "W4_EXL1n", "W4_EXL2n", "W4_EXL3n" ,"W4_EXL4n" , "W4_EXL5n" , "W4_EXL6n" , "W4_EXL7n" , "W4_EXL8n" , "W4_EXL9n","W4_EXL10n" , "W4_EXL11n" , "W4_EXL12n" , "W4_EXL13n" , "W4_EXL14n", "LonT_w1", "Lon1_w1", "Lon2_w1", "Lon3_w1","LonT_w2",  "Lon1_w2", "Lon2_w2", "Lon3_w2", "LonT_w3", "Lon1_w3", "Lon2_w3", "Lon3_w3", "LonT_w4", "Lon1_w4", "Lon2_w4", "Lon3_w4","GADw1", "GAD1w1", "GAD2w1", "GAD3w1", "GAD4w1", "GAD5w1", "GAD6w1", "GAD7w1", "GADw2", "GAD1w2", "GAD2w2", "GAD3w2", "GAD4w2", "GAD5w2", "GAD6w2", "GAD7w2", "GADw3", "GAD1w3", "GAD2w3", "GAD3w3", "GAD4w3", "GAD5w3", "GAD6w3", "GAD7w3", "GADw4", "GAD1w4", "GAD2w4", "GAD3w4", "GAD4w4", "GAD5w4", "GAD6w4", "GAD7w4", "W4_SocialContact1", "W4_SocialContact2", "W4_SocialSupport1", "W4_SocialSupport2", "W4_SocialSupport3", "W4_SocialSupport4", "W4_SocialSupport5", "W4_SocialSupport6", "W4_SocialSupport7", "W4_SocialSupport8", "W4_Empathy1", "W4_Empathy2", "W4_Empathy3", "W4_Empathy4", "W4_Empathy5", "W4_Empathy6", "W4_Empathy7", "W4_Empathy8", "W4_Empathy9", "depw1", "DEP1w1", "DEP2w1", "DEP3w1", "DEP4w1", "DEP5w1", "DEP6w1", "DEP7w1", "DEP8w1", "DEP9w1", "depw2", "DEP1w2", "DEP2w2", "DEP3w2", "DEP4w2", "DEP5w2", "DEP6w2", "DEP7w2", "DEP8w2", "DEP9w2", "DEP9w3",  "DEP1w3", "DEP2w3", "DEP3w3", "DEP4w3", "DEP5w3", "DEP6w3", "DEP7w3", "DEP8w3", "DEP9w3", "depw4", "DEP1w4", "DEP2w4", "DEP3w4", "DEP4w4", "DEP5w4", "DEP6w4", "DEP7w4", "DEP8w4", "DEP9w4", "RelN", "W4_SCn", "W4SC2n", "W4_ReligiousBelief1","W4_ReligiousBelief2.", "W4_ReligiousBelief3", "W4_ReligiousBelief4.", "W4_ReligiousBelief5", "W4_ReligiousBelief6.", "W4_ReligiousBelief7", "W4_ReligiousBelief8.", "W4_Trust_Body1","W4_Trust_Body2", "W4_Trust_Body3", "W4_Trust_Body4", "W4_Trust_Body5", "W4_Trust_Body6", "W4_Trust_Body7", "W4_Trust_Body8",     "Employ_Face2face" , "Adults_household","Children_household" , "Live_Alone" ))

covdata$W4_Religious

# Empathy
table (cov$W4_Empathy1) # how much do you identify with (feel a part of, feel love toward, have concern for) each of the following? people in my community
cov$Emp1 <- as.numeric (cov$W4_Empathy1)
table (cov$Emp1)

table (covdata$W4_Empathy2) # people from Ireland
cov$Emp2 <- as.numeric (cov$W4_Empathy2)
table (covdata$W4_Empathy3) # all humans everwyhwere
cov$Emp3 <- as.numeric (cov$W4_Empathy3)
table (covdata$W4_Empathy4) # how much would you say you care (feel upset, want to help) when bad things happen to each of the following: people in my community
cov$Emp4 <- as.numeric (cov$W4_Empathy4)
table (covdata$W4_Empathy5) # people from Ireland
cov$Emp5 <- as.numeric (cov$W4_Empathy5)
table (covdata$W4_Empathy6) # all humans everywhere
cov$Emp6 <- as.numeric (cov$W4_Empathy6)
table (covdata$W4_Empathy7) # when they are in need, how much do you want to help each of the following? people in my community
cov$Emp7 <- as.numeric (cov$W4_Empathy7)
table (covdata$W4_Empathy8) # people from Ireland
cov$Emp8 <- as.numeric (cov$W4_Empathy8)
table (covdata$W4_Empathy9)
cov$Emp9 <- as.numeric (cov$W4_Empathy9)

names (cov)
class (cov$Ethnicity_Binary)
table (cov$Ethnicity_Binary)
cov$EthnN <- as.numeric (cov$Ethnicity_Binary)
cov$sexN <- as.numeric (cov$Sex)
table(cov$Dx_health_preC19)
cov$DxN <- as.numeric (cov$Dx_health_preC19)
cov$HCN <- as.numeric (cov$Employ_HealthCare)
table (cov$emp2)
cov$emp2N <- as.numeric (cov$emp2)
cov$ageD <- cov$Age_years/100
cov$SC = (cov$W4_SCn + cov$W4SC2n)/2
table (cov$W4_SocialSupport1)
cov$Soc1 <- as.numeric (cov$W4_SocialSupport1)
cov$Soc2 <- as.numeric (cov$W4_SocialSupport2)
cov$Soc3 <- as.numeric (cov$W4_SocialSupport3)
cov$Soc4 <- as.numeric (cov$W4_SocialSupport4)
cov$Soc5 <- as.numeric (cov$W4_SocialSupport5)
cov$Soc6 <- as.numeric (cov$W4_SocialSupport6)
cov$Soc7 <- as.numeric (cov$W4_SocialSupport7)
cov$Soc8 <- as.numeric (cov$W4_SocialSupport8)

names (cov)
names (cov)
table (cov$W4_ReligiousBelief2)
table (cov$Rel2)
cov$Rel1 <- as.numeric (cov$W4_ReligiousBelief1)
cov$Rel2 <- as.numeric (cov$W4_ReligiousBelief2.)
cov$Rel3 <- as.numeric (cov$W4_ReligiousBelief3)
cov$Rel4 <- as.numeric (cov$W4_ReligiousBelief4.)
cov$Rel5 <- as.numeric (cov$W4_ReligiousBelief5)
cov$Rel6 <- as.numeric (cov$W4_ReligiousBelief6.)
cov$Rel7 <- as.numeric (cov$W4_ReligiousBelief7)
cov$Rel8 <- as.numeric (cov$W4_ReligiousBelief8.)
# mangle religious beliefs and trust for inclusion in cross sectional analysis with ExL. 

table (cov$W4_Trust_Body1)
cov$Trust1 <- as.numeric  (cov$W4_Trust_Body1)
cov$Trust2 <- as.numeric  (cov$W4_Trust_Body2)
cov$Trust3 <- as.numeric  (cov$W4_Trust_Body3)
cov$Trust4 <- as.numeric  (cov$W4_Trust_Body4)
cov$Trust5 <- as.numeric  (cov$W4_Trust_Body5)
cov$Trust6 <- as.numeric  (cov$W4_Trust_Body6)
cov$Trust7 <- as.numeric  (cov$W4_Trust_Body7)
cov$Trust8 <- as.numeric  (cov$W4_Trust_Body8)
table (cov$W4_Trust_Body2)
table (cov$W4_Trust_Body3)
table (cov$W4_Trust_Body4)
table (cov$W4_Trust_Body5)
table (cov$W4_Trust_Body6)
table (cov$W4_Trust_Body7)
table (cov$W4_Trust_Body8)

covv<- data.frame (cov)
names (covv)

prop.table(table (cov$Employ_Face2face))
table (cov$Live_Alone)
cov$LA <- as.numeric (cov$Live_Alone)
table (cov$LA)
cov$LAN = car::recode (cov$LA, "1=0; 2=1")
prop.table(table (covdata$LAN))

table (cov$Adults_household)
class (cov$Adults_household)
cov$AH <- as.numeric (cov$Adults_household)
psych::describe(cov$AH)
############# analyses#########
# Long format data required. 


names (cov)

# GC model = to see how loneliness changes on average over time, as well as separating between variation (How people change compared to others) and within variation (how people vary relative to their own average/trend)

GCLonL <- '
i =~ 1*LonT_w1 + 1*LonT_w2 + 1*LonT_w3 + 1*LonT_w4; 
s =~ 1*LonT_w1 + 2*LonT_w2 + 5*LonT_w3 + 10*LonT_w4;
'
fitL <- growth (GCLonL, estimator='ML', missing = "fiml", data = cov)
summary (fitL, standardized = TRUE, fit.measures = TRUE)


names (cov)

# number of observations overall (n = 1041 in wave 1; )
psych::describe (cov$LonT_w4)

# for visualising the growth curve of loneliness over four time points.
install.packages ("vctrs")
require (vctrs)
require (tidyverse)
Lon <- subset (cov, select = c("pid", "LonT_w1", "LonT_w2", "LonT_w3", "LonT_w4", "Employ_HealthCare"))
count (Lon, "Employ_HealthCare")
table (Lon$Employ_HealthCare, Lon$LonT_w1) 
aggregate (Lon$LonT_w4, list (Lon$Employ_HealthCare), FUN = mean, na.rm = TRUE)

library (tidyverse)
lontbl <- as_tibble (Lon)
View (lontbl)
LonLong <- lontbl %>% pivot_longer (!pid & !Employ_HealthCare, names_to = "time", values_to = "Loneliness")
require(ggplot2)
table (LonLong$Employ_HealthCare)
table (LonLong$Employ_HealthCare, LonLong$time)

Emp <- c(2.39, 2.12, 2.12, 2.05)
Unemp <- c(1.91, 1.73, 1.75, 1.95)
Emplot <- data.frame (Emp, Unemp)

ggplot(Emplot, aes(Emp, Unemp, group = 1)) + 
  geom_line(alpha = 0.01) + # add individual line with transparency
  geom_point() +
  theme_bw() + # nice theme
  labs(x = "Time", y = "Loneliness") 


ggplot (LonLong, aes (time, Loneliness, group = Employ_HealthCare)) + 
  geom_line (aes (group = "Employ_HealthCare")) + 
  geom_point() + 
  stat_summary (
    aes (group = "Yes"),
    fun = mean, 
    size = 1, 
    color = "black"
  ) + 
stat_summary (
  aes (group = "No"),
  fun = mean, 
  size = 1, 
  color = "red"
)
  
  

# predict the two latent variables
pred_lgm <- predict(fitL) 
# average of the intercepts (first column)
mean(pred_lgm[, 1]) 

# average of the slope (second column)
mean(pred_lgm[, 2])
map(1:4, # what to loop over, in this case numbers 0 to 5
    function(x) pred_lgm[, 1] + x * pred_lgm[, 2]) # formula to use



###### create long data for each individual
pred_lgm_long <- map(1:4, # loop over time
                     function(x) pred_lgm[, 1] + x * pred_lgm[, 2]) %>% 
  reduce(cbind) %>% # bring together the wave predictions 
  as.data.frame() %>% # make data frame
  setNames(str_c("time", 1:4)) %>% # give names to variables
  dplyr::mutate(id = row_number() ) %>% # make unique id
  gather(-id, key = time, value = pred) # make long format

# make graph
pred_lgm_long %>% 
  ggplot(aes(time, pred, group = id)) + # what variables to plot?
  geom_line(alpha = 0.05) + # add a transparent line for each person
  stat_summary( # add average line
    aes(group = 1),
    fun = mean,
    geom = "line",
    size = 1.5,
    color = "red"
  ) +
  theme_bw() + # makes graph look nicer
  labs(y = "Loneliness", # labels
       x = "Time")


###### create long data for each individual
pred_lgm_long <- map(1:4, # loop over time
                     function(x) pred_lgm[, 1] + x * pred_lgm[, 2]) %>% 
  reduce(cbind) %>% # bring together the wave predictions 
  as.data.frame() %>% # make data frame
  setNames(str_c("time", 1:4)) %>% # give names to variables
  dplyr::mutate(id = row_number() ) %>% # make unique id
  gather(-id, key = time, value = pred) # make long format

# make graph
pred_lgm_long %>% 
  ggplot(aes(time, pred, group = Employ_HealthCare)) + # what variables to plot?
  geom_line(alpha = 0.05) + # add a transparent line for each person
  stat_summary( # add average line
    aes(group = 1),
    fun = mean,
    geom = "line",
    size = 1.5,
    color = "red"
  ) +
  theme_bw() + # makes graph look nicer
  labs(y = "Loneliness", # labels
       x = "Time")






install.packages ("car")
require (car)
table (cov$C19_Infected)
table (cov$C19n)
cov$C19n <- as.numeric (cov$C19_Infected)
cov$C19nr <- car::recode (cov$C19n, "1=0; 2=0;3=0; 4=1; 5=1= 6=1; 7=1")
table (cov$Self_Esteem)
cov$SEn <- as.numeric (cov$Self_Esteem)
table (cov$SEn)
table(cov$Neighborhood_Belongingness)
cov$NEn <- as.numeric (cov$Neighborhood_Belongingness)
table (cov$NEn)
cov$NE = ordered (cov$Neighborhood_Belongingness)
names (cov)
cov$IncomeN <- as.numeric (cov$IncomeOrdered)
require (lavaan)


table (cov$depw1)
psych::describe (cov$depw1)
cov$depdiv <- (cov$depw1)/10

table (cov$emp2N)

table (cov$emp2N)
cov$sexN2 <- car::recode (cov$sexN, "1=0; 2=1")
cov$EthnN2 <- car::recode (cov$EthnN, "1=0; 2=1")
cov$DxN2 <- car::recode (cov$DxN, "1=0; 2=1")
cov$HCN2 <- car::recode (cov$HCN, "1=0; 2=1")
cov$C19nr [cov$C19nr == "6"] <- NA
cov$emp2N2 <- car::recode (cov$emp2N, "1=0; 2=1")
class (cov$LonT_w4)
prop.table(table(cov$IncomeOrdered))
prop.table(table (cov$Employ_HealthCare))
prop.table(table (cov$C19_Infected))

table (cov$S)
67+62+44+18+15+22
names (cov)
prop.table(table(cov$DxN2))
table ()
table (cov$NEn)
names (cov)
table (cov$RelN)
table (cov$W2_Marital_Status_Binary)
table (cov$Employ_Face2face)
cov$EF <- as.numeric (cov$Employ_Face2face)
table (cov$EF)
cov$EF <- car::recode(cov$EF, "1=0; 2=1")
names (cov)
# also need: lviing status to replace relationship status; working face to face with public to add in too. 
GCLonLcond <- '
i =~ 1*LonT_w1 + 1*LonT_w2 + 1*LonT_w3 + 1*LonT_w4; 
s =~ 1*LonT_w1 + 2*LonT_w2 + 5*LonT_w3 + 10*LonT_w4;
i ~ ageD + sexN2 + LAN + AH + EthnN2 + DxN2 +IncomeN + HCN2 + EF + NEn + C19nr +  SEn + depdiv + emp2N2;
s ~ ageD + sexN2 + LAN + AH + EthnN2 + DxN2 + IncomeN + HCN2 + EF + NEn + C19nr + SEn + depdiv + emp2N2;
'
fitLcond <- growth (GCLonLcond, data = cov)
varTable (fitLcond)
summary (fitLcond, fit.measures = TRUE, standardized = TRUE)
parameterEstimates (fitLcond, se = TRUE, ci = TRUE, fmi = TRUE, boot.ci.type = "bca", output = "data.frame", header= TRUE)


# Michael's point - can we visualise trajectories of loneliness separately for healthcare and non healthcare workers?

names (cov)
require (ggplot2)






#curvilinear model.
GCCLonLcond <- '
i =~ 1*LonT_w1 + 1*LonT_w2 + 1*LonT_w3 + 1*LonT_w4; 
s =~ 1*LonT_w1 + 4*LonT_w2 + 25*LonT_w3 + 100*LonT_w4; # quadratic terms
i ~ ageD + sexN2 + RelN + EthnN2 + DxN2 +IncomeN + HCN2 + NEn + C19nr + W4_SCn +W4SC2n +  SEn + depdiv + emp2N2;
s ~ ageD + sexN2 + RelN + EthnN2 + DxN2 + IncomeN + HCN2 + NEn + C19nr + W4_SCn + W4SC2n + SEn + depdiv + emp2N2;
'
fitLCcond <- growth (GCCLonLcond, data = cov)
varTable (fitLcond)
summary (fitLCcond, fit.measures = TRUE, standardized = TRUE)

# had to remove social contact 

GCLonLcond <- '
i =~ 1*LonT_w1 + 1*LonT_w2 + 1*LonT_w3 + 1*LonT_w4; 
s =~ 1*LonT_w1 + 2*LonT_w2 + 5*LonT_w3 + 10*LonT_w4;
i ~ ageD + sexN2 + RelN + EthnN2 + DxN2 +IncomeN + HCN2 + NEn + C19nr +  SEn + depdiv + emp2N2;
s ~ ageD + sexN2 + RelN + EthnN2 + DxN2 + IncomeN + HCN2 + NEn + C19nr +  SEn + depdiv + emp2N2;
i ~~ s;
'
fitLcond <- growth (GCLonLcond, data = cov)
varTable (fitLcond)
summary (fitLcond, fit.measures = TRUE, standardized = TRUE)




#curvilinear model.
GCCLonLcond <- '
i =~ 1*LonT_w1 + 1*LonT_w2 + 1*LonT_w3 + 1*LonT_w4; 
s =~ 1*LonT_w1 + 4*LonT_w2 + 25*LonT_w3 + 100*LonT_w4; # quadratic terms
i ~ ageD + sexN2 + RelN + EthnN2 + DxN2 +IncomeN + HCN2 + NEn + C19nr + W4_SCn +W4SC2n +  SEn + depdiv + emp2N2;
s ~ ageD + sexN2 + RelN + EthnN2 + DxN2 + IncomeN + HCN2 + NEn + C19nr + W4_SCn + W4SC2n + SEn + depdiv + emp2N2;
'
fitLCcond <- growth (GCCLonLcond, data = cov)
varTable (fitLcond)
summary (fitLCcond, fit.measures = TRUE, standardized = TRUE)
names (cov)
psych::describe (cov$Age_years)
prop.table(table(cov$Sex))
table(cov$Sex)
str (cov)


1793+1391+ 607+376+1837

nrow (cov)
table (cov$EthnN2)

install.packages ("semPlot")
require (semPlot)

# correlates of existential loneliness at wave 4. 
nrow (cov)




ExMod <- '
ExL =~ W4_EXL1n + W4_EXL2n + W4_EXL3n + W4_EXL4n + W4_EXL5n + W4_EXL6n + W4_EXL7n + W4_EXL8n + W4_EXL9n + W4_EXL10n + W4_EXL11n + W4_EXL12n + W4_EXL13n + W4_EXL14n;
Lone =~ Lon1_w4 + Lon2_w4 + Lon3_w4; 
Emp =~ Emp1 + Emp2 + + Emp3 + Emp4 + Emp5 + Emp6 + Emp7 + Emp8 + Emp9;
Trust =~ Trust1 + Trust2 + Trust3 + Trust4 + Trust5 + Trust6 + Trust7 + Trust8;
Soc =~ Soc1 + Soc2 + Soc3 + Soc4 + Soc5 + Soc6 + Soc7 + Soc8;
Dep =~ DEP1w4 + DEP2w4 + DEP3w4 + DEP4w4 + DEP5w4 + DEP6w4 + DEP7w4 + DEP8w4 + DEP9w4;
Anx =~ GAD1w4 + GAD2w4 + GAD3w4 + GAD4w4 + GAD5w4 + GAD6w4 + GAD7w4;
Rel =~ Rel1 + Rel2 + Rel3 + Rel4 + Rel5 + Rel6 + Rel7 + Rel8;
ExL ~ ageD + sexN2 + RelN + SC + EthnN2 + DxN2 + IncomeN + HCN2  + C19nr +  emp2N2 +  Dep + Anx +  Lone + Emp + Soc + Rel + Trust;
Dep ~~ Anx; 
Soc ~~ Lone;
SC ~~ Soc;
RelN ~~ Soc;
Dep ~~ Soc;

Emp ~~ Soc;
Dep ~~ Lone;
Anx ~~ Lone;
Lone ~~ Emp;
ageD ~~ DxN;
ageD ~~ emp2N;
IncomeN ~~ emp2N;
SC ~~ Lone;
DEP9w4 ~~ W4_EXL12n;
DEP6w4 ~~ DEP4w4;
DEP2w4 ~~ DEP9w4;
DEP8w4 ~~ GAD1w4;
DEP8w4 ~~ GAD5w4; 
GAD2w4 ~~ GAD3w4;
GAD1w4 ~~ GAD4w4;
GAD2w4 ~~ GAD4w4;
Dep ~~ emp2N; 
 W4_EXL7n ~~Emp9;
  W4_EXL8n ~~  W4_EXL9n;
  W4_EXL1n ~~ W4_EXL2n;
  W4_EXL1n ~~ W4_EXL3n;
  W4_EXL6n ~~ W4_EXL7n;
  W4_EXL9n ~~ W4_EXL13n; 
  W4_EXL10n ~~ W4_EXL11n;
  W4_EXL10n ~~ W4_EXL12n;
  Emp1 ~~  Emp2;
  Emp1 ~~ Emp3;
  Emp2 ~~  Emp3;
  Emp4 ~~ Emp5;
  Emp4 ~~ Emp6;
  Emp5 ~~ Emp6;
  Emp7 ~~ Emp8;
  Emp7 ~~ Emp9;
   Emp8 ~~ Emp9; 
   Lon3_w4 ~~ Soc8;
   Lon3_w4 ~~ Soc5; 
   Soc1 ~~ Soc2; 
   Soc2 ~~ Soc3;
   Soc1 ~~ Soc3; 
   Soc1 ~~ Soc4; 
   Soc2 ~~ Soc4; 
   Soc3 ~~ Soc4;
   Soc6 ~~ Soc7; 
   Rel1 ~~      Rel2;
   Rel1 ~~      Rel4;
   Rel1 ~~      Rel7;
   Rel2 ~~      Rel4;
   Rel2 ~~      Rel8;
    Rel4 ~~      Rel8;
    Rel6 ~~      Rel8;
    ageD  ~      Lone;
'

ExModFit <- sem (ExMod, data = cov, fixed.x = FALSE,  estimator='ML', missing = "fiml")
varTable (ExModFit)
summary (ExModFit, fit.measures = TRUE, standardized = TRUE)

options (max.print = 1000)

table (cov$emp2N)


ExModdag <- '
ExL =~ W4_EXL1n + W4_EXL2n + W4_EXL3n + W4_EXL4n + W4_EXL5n + W4_EXL6n + W4_EXL7n + W4_EXL8n + W4_EXL9n + W4_EXL10n + W4_EXL11n + W4_EXL12n + W4_EXL13n + W4_EXL14n;
Lone =~ Lon1_w4 + Lon2_w4 + Lon3_w4; 
Trust =~ Trust1 + Trust2 + Trust3 + Trust4 + Trust5 + Trust6 + Trust7 + Trust8;
Emp =~ Emp1 + Emp2 + + Emp3 + Emp4 + Emp5 + Emp6 + Emp7 + Emp8 + Emp9;
Soc =~ Soc1 + Soc2 + Soc3 + Soc4 + Soc5 + Soc6 + Soc7 + Soc8;
Dep =~ DEP1w4 + DEP2w4 + DEP3w4 + DEP4w4 + DEP5w4 + DEP6w4 + DEP7w4 + DEP8w4 + DEP9w4;
Anx =~ GAD1w4 + GAD2w4 + GAD3w4 + GAD4w4 + GAD5w4 + GAD6w4 + GAD7w4;
Rel =~ Rel1 + Rel2 + Rel3 + Rel4 + Rel5 + Rel6 + Rel7 + Rel8;

ExL ~ Anx + Lone + EthnN + HCN + Trust; 
Dep ~ sexN + IncomeN + DxN + ageD; 
Lone ~ Dep + Soc; 
Soc ~ RelN + emp2N + Emp; 


'

ExModFitdag <- sem (ExModdag, data = cov,  estimator='MLR', missing= "fiml")
summary (ExModFitdag, fit.measures = TRUE, standardized = TRUE)





ExModdag <- '
ExL =~ W4_EXL1n + W4_EXL2n + W4_EXL3n + W4_EXL4n + W4_EXL5n + W4_EXL6n + W4_EXL7n + W4_EXL8n + W4_EXL9n + W4_EXL10n + W4_EXL11n + W4_EXL12n + W4_EXL13n + W4_EXL14n;
Lone =~ Lon1_w4 + Lon2_w4 + Lon3_w4; 
Emp =~ Emp1 + Emp2 + + Emp3 + Emp4 + Emp5 + Emp6 + Emp7 + Emp8 + Emp9;
Soc =~ Soc1 + Soc2 + Soc3 + Soc4 + Soc5 + Soc6 + Soc7 + Soc8;
Anx =~ GAD1w4 + GAD2w4 + GAD3w4 + GAD4w4 + GAD5w4 + GAD6w4 + GAD7w4;
Dep =~ DEP1w4 + DEP2w4 + DEP3w4 + DEP4w4 + DEP5w4 + DEP6w4 + DEP7w4 + DEP8w4 + DEP9w4;
ExL ~ Anx + Lone + EthnN2 + HCN2; 
Dep ~ sexN2 + IncomeN + DxN2 + ageD; 
Lone ~ Dep + Soc; 
Soc ~ RelN + emp2N2 + Emp; 



Lone ~~ Soc; Lone ~~ Dep; Anx ~~ Dep;
W4_EXL9n ~~ W4_EXL13n; W4_EXL10n ~~ W4_EXL11n; W4_EXL10n ~~ W4_EXL12n; W4_EXL12n ~~ W4_EXL14n; W4_EXL12n ~~    DEP9w4; W4_EXL14n ~~    DEP9w4; Lon2_w4 ~~   Lon3_w4;  Emp1 ~~      Emp2; Emp1 ~~      Emp6; Emp1 ~~      Emp9;  Emp2 ~~      Emp3; Emp4 ~~      Emp5; Emp4 ~~      Emp7 ; Emp4 ~~      Emp8; Emp4 ~~      Emp9; Emp5 ~~      Emp6; Emp6 ~~      Emp7;  Emp7 ~~      Emp8; Emp8 ~~      Emp9; Soc3 ~~      Soc4; Soc3 ~~      Soc6; Soc3 ~~      Soc7 ;  Soc6 ~~      Soc7 ;  Soc7 ~~      Soc8;   DEP8w4 ~~    DEP9w4; 
'

ExModFitdag <- sem (ExModdag, data = cov, fixed.x = FALSE, estimator='ML', missing = "fiml")

summary (ExModFitdag, fit.measures = TRUE, standardized = TRUE)


modindices (ExModFitdag)
options (max.print = 20000)


ExModdag <- '
ExL =~ W4_EXL1n + W4_EXL2n + W4_EXL3n + W4_EXL4n + W4_EXL5n + W4_EXL6n + W4_EXL7n + W4_EXL8n + W4_EXL9n + W4_EXL10n + W4_EXL11n + W4_EXL12n + W4_EXL13n + W4_EXL14n;
Lone =~ Lon1_w4 + Lon2_w4 + Lon3_w4; 
Emp =~ Emp1 + Emp2 + + Emp3 + Emp4 + Emp5 + Emp6 + Emp7 + Emp8 + Emp9;
Soc =~ Soc1 + Soc2 + Soc3 + Soc4 + Soc5 + Soc6 + Soc7 + Soc8;
Anx =~ GAD1w4 + GAD2w4 + GAD3w4 + GAD4w4 + GAD5w4 + GAD6w4 + GAD7w4;
Dep =~ DEP1w4 + DEP2w4 + DEP3w4 + DEP4w4 + DEP5w4 + DEP6w4 + DEP7w4 + DEP8w4 + DEP9w4;
ExL ~ Anx + Lone + EthnN2 + HCN2 + Dep + sexN2 + IncomeN + ageD + DxN2 + RelN + emp2N2 + Emp; 




Lone ~~ Soc; Lone ~~ Dep; Anx ~~ Dep;
W4_EXL9n ~~ W4_EXL13n; W4_EXL10n ~~ W4_EXL11n; W4_EXL10n ~~ W4_EXL12n; W4_EXL12n ~~ W4_EXL14n; W4_EXL12n ~~    DEP9w4; W4_EXL14n ~~    DEP9w4; Lon2_w4 ~~   Lon3_w4;  Emp1 ~~      Emp2; Emp1 ~~      Emp6; Emp1 ~~      Emp9;  Emp2 ~~      Emp3; Emp4 ~~      Emp5; Emp4 ~~      Emp7 ; Emp4 ~~      Emp8; Emp4 ~~      Emp9; Emp5 ~~      Emp6; Emp6 ~~      Emp7;  Emp7 ~~      Emp8; Emp8 ~~      Emp9; Soc3 ~~      Soc4; Soc3 ~~      Soc6; Soc3 ~~      Soc7 ;  Soc6 ~~      Soc7 ;  Soc7 ~~      Soc8;   DEP8w4 ~~    DEP9w4; 
'

ExModFitdag <- sem (ExModdag, data = cov, fixed.x = FALSE, estimator='ML', missing = "fiml")

summary (ExModFitdag, fit.measures = TRUE, standardized = TRUE)



names (cov)
prop.table(table (cov$Ethnicity_Binary))
prop.table(table (cov$emp2))
psych::describe(cov$LonT_w1)
psych::describe(cov$depw1)
