Coding

library(foreign)
library(tidyverse)
library(plyr)
library(psych)
library(nlme)
library(plotly)
library(ggplot2)
library(ggthemes)
library(dplyr)
survey_data <- readRDS("~/Desktop/homes_survey_data_processed_2022-12-18 copy.rds")

survey_data <- survey_data |> 
  mutate(
 #loneliness variables 
    
    #Lack companionship 
    hlth_lack_cmpnship_freq7 = case_when(
      (hlth_lack_cmpnship_freq7) == "Hardly ever" ~ 0,
      (hlth_lack_cmpnship_freq7) == "Once in a while" ~ 1,
      (hlth_lack_cmpnship_freq7) == "Sometimes" ~ 2,
      (hlth_lack_cmpnship_freq7) == "Often" ~ 3,
      (hlth_lack_cmpnship_freq7) == "Most of the time" ~ 4,
      TRUE ~ 0),
    
    #Feel left out
    hlth_feel_left_out_freq7 = case_when(
      (hlth_feel_left_out_freq7) == "Hardly ever" ~ 0,
      (hlth_feel_left_out_freq7) == "Once in a while" ~ 1,
      (hlth_feel_left_out_freq7) == "Sometimes" ~ 2,
      (hlth_feel_left_out_freq7) == "Often" ~ 3,
      (hlth_feel_left_out_freq7) == "Most of the time" ~ 4,
      TRUE ~ 0),
    
    #Feel Isolated
    hlth_feel_isolated_freq7 = case_when(
      (hlth_feel_isolated_freq7) == "Hardly ever" ~ 0,
      (hlth_feel_isolated_freq7) == "Once in a while" ~ 1,
      (hlth_feel_isolated_freq7) == "Sometimes" ~ 2,
      (hlth_feel_isolated_freq7) == "Often" ~ 3,
      (hlth_feel_isolated_freq7) == "Most of the time" ~ 4,
      TRUE ~ 0),
    
   
     # before covid - Lack companionship 

    cvd_before_lack_cmpnshp_freq3   = case_when(
      (cvd_before_lack_cmpnshp_freq3) == "Never" ~ 0,
      (cvd_before_lack_cmpnshp_freq3) == "Rarely" ~ 1,
      (cvd_before_lack_cmpnshp_freq3) == "Some of the time" ~ 2,
      (cvd_before_lack_cmpnshp_freq3) == "Most of the time" ~ 3,
      (cvd_before_lack_cmpnshp_freq3) == "Always" ~ 4,
      TRUE ~ 0),
    
      # before covid - Feel left out

    cvd_before_felt_left_out_freq3 = case_when(
      (cvd_before_felt_left_out_freq3) == "Never" ~ 0,
      (cvd_before_felt_left_out_freq3) == "Rarely" ~ 1,
      (cvd_before_felt_left_out_freq3) == "Some of the time" ~ 2,
      (cvd_before_felt_left_out_freq3) == "Most of the time" ~ 3,
      (cvd_before_felt_left_out_freq3) == "Always" ~ 4,
      TRUE ~ 0),    
        
     # before covid - Feel isolted 

    cvd_before_felt_isolated_freq3 = case_when(
      (cvd_before_felt_isolated_freq3) == "Never" ~ 0,
      (cvd_before_felt_isolated_freq3) == "Rarely" ~ 1,
      (cvd_before_felt_isolated_freq3) == "Some of the time" ~ 2,
      (cvd_before_felt_isolated_freq3) == "Most of the time" ~ 3,
      (cvd_before_felt_isolated_freq3) == "Always" ~ 4,
      TRUE ~ 0),        
     
    # after covid - Lack companionship 

    cvd_after_frn_lack_cmpnshp_freq3 = case_when(
      (cvd_after_frn_lack_cmpnshp_freq3) == "Never" ~ 0,
      (cvd_after_frn_lack_cmpnshp_freq3) == "Rarely" ~ 1,
      (cvd_after_frn_lack_cmpnshp_freq3) == "Some of the time" ~ 2,
      (cvd_after_frn_lack_cmpnshp_freq3) == "Most of the time" ~ 3,
      (cvd_after_frn_lack_cmpnshp_freq3) == "Always" ~ 4,
      TRUE ~ 0),            
    
     # after covid - Feel left out
    
    cvd_after_felt_left_out_freq3   = case_when(
      (cvd_after_felt_left_out_freq3) == "Never" ~ 0,
      (cvd_after_felt_left_out_freq3) == "Rarely" ~ 1,
      (cvd_after_felt_left_out_freq3) == "Some of the time" ~ 2,
      (cvd_after_felt_left_out_freq3) == "Most of the time" ~ 3,
      (cvd_after_felt_left_out_freq3) == "Always" ~ 4,
      TRUE ~ 0),            
    
      # after covid - Feel isolted 

    cvd_after_felt_isolated_freq3   = case_when(
      (cvd_after_felt_isolated_freq3) == "Never" ~ 0,
      (cvd_after_felt_isolated_freq3) == "Rarely" ~ 1,
      (cvd_after_felt_isolated_freq3) == "Some of the time" ~ 2,
      (cvd_after_felt_isolated_freq3) == "Most of the time" ~ 3,
      (cvd_after_felt_isolated_freq3) == "Always" ~ 4,
      TRUE ~ 0),    

    # mental health 
    hlth_dsblty_strg_mntl_cb = case_when(
      (hlth_dsblty_strg_mntl_cb) == "I sometimes struggle with mental health issues" ~ 3,
TRUE ~ 0),  
   
    # emotional disability

    hlth_dsblty_bhvrl_mtnl_cb = case_when(
      (hlth_dsblty_bhvrl_mtnl_cb) == "I have a behavioral or emotional disability" ~ 5,
      TRUE ~ 0),    
    
    #feel tense
    hlth_tense_dwn_low_freq3= case_when(
      (hlth_tense_dwn_low_freq3) == "Never" ~ 0,
      (hlth_tense_dwn_low_freq3) == "Rarely" ~ 1,
      (hlth_tense_dwn_low_freq3) == "Some of the time" ~ 2,
      (hlth_tense_dwn_low_freq3) == "Most of the time" ~ 3,
      (hlth_tense_dwn_low_freq3) == "Always" ~ 4,
          TRUE ~ 0),    
    

 # Belong to LGBTQ+/SGL community - friends

 lgbtq_cmmty_frnds_grps_ordscl1 = case_when(
      (lgbtq_cmmty_frnds_grps_ordscl1) == "Not at all" ~ 4,
      (lgbtq_cmmty_frnds_grps_ordscl1) == "Somewhat" ~ 3,
      (lgbtq_cmmty_frnds_grps_ordscl1) == "A fair amount" ~ 2,
      (lgbtq_cmmty_frnds_grps_ordscl1) == "Quite a bit" ~ 1,
      (lgbtq_cmmty_frnds_grps_ordscl1) == "A greate deal" ~ 0,
      TRUE ~ 0),
    
 # Belong to LGBTQ+/SGL community - support group

    lgbtq_cmmty_spprt_grps_ordscl1 = case_when(
      (lgbtq_cmmty_spprt_grps_ordscl1) == "Not at all" ~ 4,
      (lgbtq_cmmty_spprt_grps_ordscl1) == "Somewhat" ~ 3,
      (lgbtq_cmmty_spprt_grps_ordscl1) == "A fair amount" ~ 2,
      (lgbtq_cmmty_spprt_grps_ordscl1) == "Quite a bit" ~ 1,
      (lgbtq_cmmty_spprt_grps_ordscl1) == "A greate deal" ~ 0,
      TRUE ~ 0),
    
    # satisfaction 
    
    lgbtq_cmmty_semi_sfcn3 = case_when(
      (lgbtq_cmmty_semi_sfcn3) == "Not satisfied" ~ 4,
      (lgbtq_cmmty_semi_sfcn3) == "Slightly satisfied" ~ 3,
      (lgbtq_cmmty_semi_sfcn3) == "Moderately satisfied" ~ 2,
      (lgbtq_cmmty_semi_sfcn3) == "Very statisfied" ~ 1,
      (lgbtq_cmmty_semi_sfcn3) == "Extremely satisfied" ~ 0,
      TRUE ~ 0),

    # ADL 
    
    hlth_hlp_care_self_freq3    = case_when(
      (hlth_hlp_care_self_freq3) == "Never" ~ 0,
      (hlth_hlp_care_self_freq3) == "Rarely" ~ 1,
      (hlth_hlp_care_self_freq3) == "Some of the time" ~ 2,
      (hlth_hlp_care_self_freq3) == "Most of the time" ~ 3,
      (hlth_hlp_care_self_freq3) == "Always" ~ 4,
      TRUE ~ 0),

# IADL
    hlth_asstnc_hhld_freq3  = case_when(
      (hlth_asstnc_hhld_freq3) == "Never" ~ 0,
      (hlth_asstnc_hhld_freq3) == "Rarely" ~ 1,
      (hlth_asstnc_hhld_freq3) == "Some of the time" ~ 2,
      (hlth_asstnc_hhld_freq3) == "Most of the time" ~ 3,
      (hlth_asstnc_hhld_freq3) == "Always" ~ 4,
      TRUE ~ 0),

#  Actively participate in community activities

  srv_lgbtq_prgm_alrdy_prtc_ynfreq1 = case_when(
      (srv_lgbtq_prgm_alrdy_prtc_ynfreq1) == "Yes, frequently" ~ 0,
      (srv_lgbtq_prgm_alrdy_prtc_ynfreq1) == "Yes, sometimes" ~ 1,
      (srv_lgbtq_prgm_alrdy_prtc_ynfreq1) == "Yes, once a while" ~ 2,
      (srv_lgbtq_prgm_alrdy_prtc_ynfreq1) == "No" ~ 3,
      (srv_lgbtq_prgm_alrdy_prtc_ynfreq1) == "Not Sure" ~ 995,
      TRUE ~ 0),

# moved to be near family members

  hsg_mvd_yr_fam_imprt3 = case_when(
      (hsg_mvd_yr_fam_imprt3) == "Extremely important" ~ 0,
      (hsg_mvd_yr_fam_imprt3) == "Very important" ~ 1,
      (hsg_mvd_yr_fam_imprt3) == "Moderately important" ~ 2,
      (hsg_mvd_yr_fam_imprt3) == "Slightly important" ~ 3,
      (hsg_mvd_yr_fam_imprt3) == "Not at all important" ~ 4,
      TRUE ~ 0),
  
  # live alone
    hsg_live_with_noone_cb = case_when(
      (hsg_live_with_noone_cb) == "I live alone" ~ 5,
      TRUE ~ 0))

New varibales and plot

survey_data <- survey_data %>% 
   select(hlth_lack_cmpnship_freq7,
          hlth_feel_left_out_freq7,
          hlth_feel_isolated_freq7,
          cvd_before_lack_cmpnshp_freq3,
          cvd_before_felt_left_out_freq3,
          cvd_before_felt_isolated_freq3,
          cvd_after_frn_lack_cmpnshp_freq3,
          cvd_after_felt_left_out_freq3,
          cvd_after_felt_isolated_freq3)%>%
  mutate(survey_data, differ_left_out = cvd_after_felt_left_out_freq3 - cvd_before_felt_left_out_freq3)|>
  mutate(survey_data, differ_feel_isol = cvd_after_felt_isolated_freq3 - cvd_before_felt_isolated_freq3)|>
  mutate(survey_data, differ_lack_cmpnship = cvd_after_frn_lack_cmpnshp_freq3 - cvd_before_lack_cmpnshp_freq3)

Plot of new variables

Most of them show no difference between before and after covid

ggplot(survey_data,
       aes(x=differ_left_out))+
  geom_bar()

ggplot(survey_data,
       aes(x=differ_feel_isol))+
  geom_bar()

ggplot(survey_data,
       aes(x=differ_lack_cmpnship))+
  geom_bar()

Factor analysis - loneliness

Suggests to combine 3 new variables

survey_data_lonely1_scaled <- survey_data %>% 
  select(hlth_lack_cmpnship_freq7,
          hlth_feel_left_out_freq7,
          hlth_feel_isolated_freq7,
          cvd_before_lack_cmpnshp_freq3,
          cvd_before_felt_left_out_freq3,
          cvd_before_felt_isolated_freq3,
          differ_left_out,
          differ_feel_isol,
          differ_lack_cmpnship) |>
  mutate(across(.cols = everything(), .fns = ~ scale(.) %>% as.vector))

fit_hsg <- fa(
  r = survey_data_lonely1_scaled, nfactors = 2, 
  fm = "gls", rotate = "oblimin", 
  scores = "tenBerge", oblique.scores = TRUE,
  alpha = 0.05, p = 0.05
)
psych::print.psych(fit_hsg, digits = 2, cut = 0.4, sort = TRUE, signif = !NULL)
## Factor Analysis using method =  gls
## Call: fa(r = survey_data_lonely1_scaled, nfactors = 2, rotate = "oblimin", 
##     scores = "tenBerge", fm = "gls", alpha = 0.05, p = 0.05, 
##     oblique.scores = TRUE)
## Standardized loadings (pattern matrix) based upon correlation matrix
##                                item  GLS1  GLS2   h2   u2 com
## hlth_feel_isolated_freq7          3  0.90       0.77 0.23 1.1
## hlth_feel_left_out_freq7          2  0.89       0.76 0.24 1.0
## hlth_lack_cmpnship_freq7          1  0.86       0.72 0.28 1.0
## cvd_before_lack_cmpnshp_freq3     4  0.71       0.69 0.31 1.4
## cvd_before_felt_left_out_freq3    5  0.70       0.65 0.35 1.3
## cvd_before_felt_isolated_freq3    6  0.67       0.65 0.35 1.4
## differ_feel_isol                  8        0.81 0.65 0.35 1.0
## differ_lack_cmpnship              9        0.74 0.55 0.45 1.0
## differ_left_out                   7        0.72 0.51 0.49 1.0
## 
##                       GLS1 GLS2
## SS loadings           3.85 2.10
## Proportion Var        0.43 0.23
## Cumulative Var        0.43 0.66
## Proportion Explained  0.65 0.35
## Cumulative Proportion 0.65 1.00
## 
##  With factor correlations of 
##       GLS1  GLS2
## GLS1  1.00 -0.22
## GLS2 -0.22  1.00
## 
## Mean item complexity =  1.1
## Test of the hypothesis that 2 factors are sufficient.
## 
## The degrees of freedom for the null model are  36  and the objective function was  6.46 with Chi Square of  1674.65
## The degrees of freedom for the model are 19  and the objective function was  1.12 
## 
## The root mean square of the residuals (RMSR) is  0.05 
## The df corrected root mean square of the residuals is  0.07 
## 
## The harmonic number of observations is  264 with the empirical chi square  55.79  with prob <  1.8e-05 
## The total number of observations was  264  with Likelihood Chi Square =  287.78  with prob <  6.3e-50 
## 
## Tucker Lewis Index of factoring reliability =  0.688
## RMSEA index =  0.231  and the 95 % confidence intervals are  0.204 0.261
## BIC =  181.84
## Fit based upon off diagonal values = 0.99
## Measures of factor score adequacy             
##                                                   GLS1 GLS2
## Correlation of (regression) scores with factors   0.97 0.92
## Multiple R square of scores with factors          0.95 0.85
## Minimum correlation of possible factor scores     0.89 0.69
loadings_hsg <- fit_hsg$loadings
fa.diagram(loadings_hsg, digits = 2, cut = 0.4)

fit_econ <- fa(r = survey_data_lonely1_scaled, nfactors = 4, fm = "gls", rotate = "oblimin", scores = "tenBerge")

Factor analysis - mental health

It seems like feel tense doesn’e belong to any of factors?

survey_data_mlh_scaled <- survey_data %>% 
  select(    hlth_dsblty_strg_mntl_cb,
             hlth_dsblty_bhvrl_mtnl_cb,
             hlth_tense_dwn_low_freq3) |> 
  mutate(across(.cols = everything(), .fns = ~ scale(.) %>% as.vector))


fa.parallel(survey_data_mlh_scaled, fa = "fa", fm = "gls")

## Parallel analysis suggests that the number of factors =  1  and the number of components =  NA
fit_hsg <- fa(
  r = survey_data_mlh_scaled, nfactors = 1, 
  fm = "gls", rotate = "oblimin", 
  scores = "tenBerge", oblique.scores = TRUE,
  alpha = 0.05, p = 0.05
)
psych::print.psych(fit_hsg, digits = 2, cut = 0.4, sort = TRUE, signif = !NULL)
## Factor Analysis using method =  gls
## Call: fa(r = survey_data_mlh_scaled, nfactors = 1, rotate = "oblimin", 
##     scores = "tenBerge", fm = "gls", alpha = 0.05, p = 0.05, 
##     oblique.scores = TRUE)
## Standardized loadings (pattern matrix) based upon correlation matrix
##                           V GLS1    h2    u2 com
## hlth_dsblty_bhvrl_mtnl_cb 2 0.96 0.913 0.087   1
## hlth_tense_dwn_low_freq3  3      0.056 0.944   1
## hlth_dsblty_strg_mntl_cb  1      0.048 0.952   1
## 
##                GLS1
## SS loadings    1.02
## Proportion Var 0.34
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## The degrees of freedom for the null model are  3  and the objective function was  0.13 with Chi Square of  32.92
## The degrees of freedom for the model are 0  and the objective function was  0.1 
## 
## The root mean square of the residuals (RMSR) is  0.16 
## The df corrected root mean square of the residuals is  NA 
## 
## The harmonic number of observations is  264 with the empirical chi square  40.46  with prob <  NA 
## The total number of observations was  264  with Likelihood Chi Square =  26.1  with prob <  NA 
## 
## Tucker Lewis Index of factoring reliability =  -Inf
## Fit based upon off diagonal values = 0.41
## Measures of factor score adequacy             
##                                                   GLS1
## Correlation of (regression) scores with factors   0.96
## Multiple R square of scores with factors          0.93
## Minimum correlation of possible factor scores     0.86
loadings_hsg <- fit_hsg$loadings
fa.diagram(loadings_hsg, digits = 2, cut = 0.4)

fit_econ <- fa(r = survey_data_mlh_scaled, nfactors = 2, fm = "gls", rotate = "oblimin", scores = "tenBerge")

Factor analysis - Connectedness

These include living arrangement, ADL, IADL, frequency and statisfaction of participanting in community activities, but not sure why srv_lgbtq_prgm_alrdy_prtc_ynfreq1 has negative score, and the rest of two weren’t assigned any factors?

survey_data_1connect_scaled <- survey_data %>% 
  select(lgbtq_cmmty_frnds_grps_ordscl1,
         lgbtq_cmmty_spprt_grps_ordscl1,
         lgbtq_cmmty_semi_sfcn3,
         srv_lgbtq_prgm_alrdy_prtc_ynfreq1,
        hlth_hlp_care_self_freq3,
        hlth_asstnc_hhld_freq3) |>
  mutate(across(.cols = everything(), .fns = ~ scale(.) %>% as.vector))

fa.parallel(survey_data_1connect_scaled, fa = "fa", fm = "gls")

## Parallel analysis suggests that the number of factors =  2  and the number of components =  NA
fit_hsg <- fa(
  r = survey_data_1connect_scaled, nfactors = 2, 
  fm = "gls", rotate = "oblimin", 
  scores = "tenBerge", oblique.scores = TRUE,
  alpha = 0.05, p = 0.05
)
psych::print.psych(fit_hsg, digits = 2, cut = 0.4, sort = TRUE, signif = !NULL)
## Factor Analysis using method =  gls
## Call: fa(r = survey_data_1connect_scaled, nfactors = 2, rotate = "oblimin", 
##     scores = "tenBerge", fm = "gls", alpha = 0.05, p = 0.05, 
##     oblique.scores = TRUE)
## Standardized loadings (pattern matrix) based upon correlation matrix
##                                   item GLS1  GLS2   h2   u2 com
## lgbtq_cmmty_semi_sfcn3               3 0.71       0.50 0.50 1.0
## lgbtq_cmmty_frnds_grps_ordscl1       1 0.70       0.53 0.47 1.1
## lgbtq_cmmty_spprt_grps_ordscl1       2 0.70       0.52 0.48 1.1
## hlth_hlp_care_self_freq3             5       0.66 0.44 0.56 1.0
## hlth_asstnc_hhld_freq3               6       0.66 0.44 0.56 1.0
## srv_lgbtq_prgm_alrdy_prtc_ynfreq1    4      -0.51 0.29 0.71 1.3
## 
##                       GLS1 GLS2
## SS loadings           1.52 1.19
## Proportion Var        0.25 0.20
## Cumulative Var        0.25 0.45
## Proportion Explained  0.56 0.44
## Cumulative Proportion 0.56 1.00
## 
##  With factor correlations of 
##       GLS1  GLS2
## GLS1  1.00 -0.01
## GLS2 -0.01  1.00
## 
## Mean item complexity =  1.1
## Test of the hypothesis that 2 factors are sufficient.
## 
## The degrees of freedom for the null model are  15  and the objective function was  1.11 with Chi Square of  288.66
## The degrees of freedom for the model are 4  and the objective function was  0.23 
## 
## The root mean square of the residuals (RMSR) is  0.11 
## The df corrected root mean square of the residuals is  0.21 
## 
## The harmonic number of observations is  264 with the empirical chi square  94.03  with prob <  1.8e-19 
## The total number of observations was  264  with Likelihood Chi Square =  59.71  with prob <  3.3e-12 
## 
## Tucker Lewis Index of factoring reliability =  0.232
## RMSEA index =  0.23  and the 95 % confidence intervals are  0.171 0.293
## BIC =  37.4
## Fit based upon off diagonal values = 0.83
## Measures of factor score adequacy             
##                                                   GLS1 GLS2
## Correlation of (regression) scores with factors   0.89 0.87
## Multiple R square of scores with factors          0.79 0.76
## Minimum correlation of possible factor scores     0.59 0.52
loadings_hsg <- fit_hsg$loadings
fa.diagram(loadings_hsg, digits = 2, cut = 0.4)

fit_econ <- fa(r = survey_data_1connect_scaled, nfactors = 4, fm = "gls", rotate = "oblimin", scores = "tenBerge")