select variables

library(ggthemes)
library(ggcorrplot)
library(mclust)
library(psych)
library(GPArotation)
library(tidyverse)

survey_data_HTS <- readRDS("~/Desktop/homes_survey_data_processed_2023-08-15 copy.rds")

survey_data_HTS <- survey_data_HTS %>%
  
   select(
     
# formal lgbtq activities - current -  aging in community
      lgbtq_cmmty_spprt_grps_ordscl1,
      lgbtq_cmmty_one_lcl_org_ordscl1,
      lgbtq_cmmty_many_lcl_org_ordscl1,
      lgbtq_cmmty_intrnt_grps_ordscl1,
      lgbtq_cmmty_natl_ntwrk_ordscl1,
      lgbtq_cmmty_faith_ordscl1,
      srv_lgbtq_ntwrkng_ynm,
      srv_lgbtq_trans_prgmng_ynm,
      
# formal others - current -  aging in community

      srv_lgbtq_tech_buddy_ynm,
      srv_lgbtq_spprt_grps_ynm,
      srv_lgbtq_tele_buddy_ynm,
      srv_lgbtq_lunch_learn_ynm,
      srv_lgbtq_scl_act_ynm,
      srv_lgbtq_vrtl_scl_evnt_ynm,
      srv_lgbtq_multigen_meals_ynm,

# informal - friends - current - aging in community
      cvd_impct_careg_frnds_in_home_freq1,
      cvd_impct_careg_frnds_out_home_freq1,
      cvd_comm_sms_freq2,
      cvd_comm_email_freq2,
      cvd_comm_videoapp_freq2,
      cvd_comm_social_media_freq2,
      cvd_comm_video_device_freq2,
      cvd_after_frnd_info_freq3,
      cvd_after_frnd_spprt_freq3,
      cvd_after_frnd_tlk_prssr_freq3,
      cvd_after_frnd_shr_vews_freq3,
      hsg_live_with_frnd_cb,
      tsp_bfrcvd_othr_prsn_drv_me_freq4,
      tsp_crrnt_othr_prsn_drv_me_freq3,
      tsp_chllng_rely_othr_freq3,
      spprt_to_frnd_non_rltv_yn,


# informal - family - current - aging in community
      cvd_impct_careg_kids_in_home_freq1,
      cvd_impct_careg_kids_out_home_freq1,
      hsg_live_with_partner_spouse_cb,
      hsg_live_with_adlt_chld_cb,
      hsg_live_with_adlscnt_chld_cb,
      hsg_live_with_youth_chld_cb,
      spprt_othr_tsp_cb,
      spprt_othr_fncl_cb,
      spprt_othr_food_cb,
      spprt_othr_tech_adv_cb,
      spprt_othr_emtnl_spprt_cb,
      spprt_othr_crgvng_cb,
      spprt_othr_shppng_cb,
      spprt_othr_hhld_wrk_cb,
      spprt_to_spouse_prtnr_yn,
      spprt_to_chldrn_yn,
      spprt_to_grndchldrn_yn,

# loneliness and mental health
      hlth_lack_cmpnship_freq7,
      hlth_feel_left_out_freq7,
      hlth_feel_isolated_freq7,
      hlth_dsblty_strg_mntl_cb,
      hlth_dsblty_bhvrl_mtnl_cb,
      hlth_tense_dwn_low_freq3
      
    ) 

coding for loneliness and mental health

survey_data_HTS <- survey_data_HTS |> 
  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),

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

    hlth_dsblty_bhvrl_mtnl_cb = case_when(
      (hlth_dsblty_bhvrl_mtnl_cb) == "I have a behavioral or emotional disability" ~ 1, #    
      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))

coding for formal lgbtq+ - current - aging in communiuty

library(ggthemes)
library(ggcorrplot)
library(mclust)
library(psych)
library(GPArotation)
library(tidyverse)


survey_data_HTS <- survey_data_HTS |> 
  mutate(
 ##formal - LGBTQ+
    
# Support group(s)
    lgbtq_cmmty_spprt_grps_ordscl1 = case_when(
      (lgbtq_cmmty_spprt_grps_ordscl1) == "Not at all" ~ 0,
      (lgbtq_cmmty_spprt_grps_ordscl1) == "Somewhat" ~ 1,
      (lgbtq_cmmty_spprt_grps_ordscl1) == "A fair amount" ~ 2,
      (lgbtq_cmmty_spprt_grps_ordscl1) == "Quite a bit" ~ 3,
      (lgbtq_cmmty_spprt_grps_ordscl1) == "A great deal " ~ 4,
      (lgbtq_cmmty_spprt_grps_ordscl1) == "-999" ~ 0,
      (lgbtq_cmmty_spprt_grps_ordscl1) == "-998" ~ 0,
      TRUE ~ 0),
    
#One local LGBTQ+ organization
lgbtq_cmmty_one_lcl_org_ordscl1 = case_when(
      (lgbtq_cmmty_one_lcl_org_ordscl1) == "Not at all" ~ 0,
      (lgbtq_cmmty_one_lcl_org_ordscl1) == "Somewhat" ~ 1,
      (lgbtq_cmmty_one_lcl_org_ordscl1) == "A fair amount" ~ 2,
      (lgbtq_cmmty_one_lcl_org_ordscl1) == "Quite a bit" ~ 3,
      (lgbtq_cmmty_one_lcl_org_ordscl1) == "A great deal " ~ 4,
      (lgbtq_cmmty_one_lcl_org_ordscl1) == "-999" ~ 0,
      (lgbtq_cmmty_one_lcl_org_ordscl1) == "-998" ~ 0,
      TRUE ~ 0),


#More than one local LGBTQ+ organization
lgbtq_cmmty_many_lcl_org_ordscl1 = case_when(
      (lgbtq_cmmty_many_lcl_org_ordscl1) == "Not at all" ~ 0,
      (lgbtq_cmmty_many_lcl_org_ordscl1) == "Somewhat" ~ 1,
      (lgbtq_cmmty_many_lcl_org_ordscl1) == "A fair amount" ~ 2,
      (lgbtq_cmmty_many_lcl_org_ordscl1) == "Quite a bit" ~ 3,
      (lgbtq_cmmty_many_lcl_org_ordscl1) == "A great deal " ~ 4,
      (lgbtq_cmmty_many_lcl_org_ordscl1) == "-999" ~ 0,
      (lgbtq_cmmty_many_lcl_org_ordscl1) == "-998" ~ 0,
      TRUE ~ 0),

# LGBTQ+ Internet group(s)
lgbtq_cmmty_intrnt_grps_ordscl1 = case_when(
      (lgbtq_cmmty_intrnt_grps_ordscl1) == "Not at all" ~ 0,
      (lgbtq_cmmty_intrnt_grps_ordscl1) == "Somewhat" ~ 1,
      (lgbtq_cmmty_intrnt_grps_ordscl1) == "A fair amount" ~ 2,
      (lgbtq_cmmty_intrnt_grps_ordscl1) == "Quite a bit" ~ 3,
      (lgbtq_cmmty_intrnt_grps_ordscl1) == "A great deal " ~ 4,
      (lgbtq_cmmty_intrnt_grps_ordscl1) == "-999" ~ 0,
      (lgbtq_cmmty_intrnt_grps_ordscl1) == "-998" ~ 0,
      TRUE ~ 0),

# National LGBTQ+ network
lgbtq_cmmty_natl_ntwrk_ordscl1 = case_when(
      (lgbtq_cmmty_natl_ntwrk_ordscl1) == "Not at all" ~ 0,
      (lgbtq_cmmty_natl_ntwrk_ordscl1) == "Somewhat" ~ 1,
      (lgbtq_cmmty_natl_ntwrk_ordscl1) == "A fair amount" ~ 2,
      (lgbtq_cmmty_natl_ntwrk_ordscl1) == "Quite a bit" ~ 3,
      (lgbtq_cmmty_natl_ntwrk_ordscl1) == "A great deal " ~ 4,
      (lgbtq_cmmty_natl_ntwrk_ordscl1) == "-999" ~ 0,
      (lgbtq_cmmty_natl_ntwrk_ordscl1) == "-998" ~ 0,
      TRUE ~ 0),

# LGBTQ+ faith-based community / church
lgbtq_cmmty_faith_ordscl1 = case_when(
      (lgbtq_cmmty_faith_ordscl1) == "Not at all" ~ 0,
      (lgbtq_cmmty_faith_ordscl1) == "Somewhat" ~ 1,
      (lgbtq_cmmty_faith_ordscl1) == "A fair amount" ~ 2,
      (lgbtq_cmmty_faith_ordscl1) == "Quite a bit" ~ 3,
      (lgbtq_cmmty_faith_ordscl1) == "A great deal " ~ 4,
      (lgbtq_cmmty_faith_ordscl1) == "-999" ~ 0,
      (lgbtq_cmmty_faith_ordscl1) == "-998" ~ 0,
      TRUE ~ 0),

#  Networking / information event targeting LGBTQ+ older adults
srv_lgbtq_ntwrkng_ynm = case_when(
      (srv_lgbtq_ntwrkng_ynm) == "No" ~ 0,
      (srv_lgbtq_ntwrkng_ynm) == "Maybe" ~ 0,
      (srv_lgbtq_ntwrkng_ynm) == "Yes" ~ 1,
      (srv_lgbtq_ntwrkng_ynm) == "-999" ~ 0,
      (srv_lgbtq_ntwrkng_ynm) == "-998" ~ 0,
      TRUE ~ 0),

# Targeted programming for transgender older adults
srv_lgbtq_trans_prgmng_ynm = case_when(
      (srv_lgbtq_trans_prgmng_ynm) == "No" ~ 0,
      (srv_lgbtq_trans_prgmng_ynm) == "Maybe" ~ 0,
      (srv_lgbtq_trans_prgmng_ynm) == "Yes" ~ 1,
      (srv_lgbtq_trans_prgmng_ynm) == "-999" ~ 0,
      (srv_lgbtq_trans_prgmng_ynm) == "-998" ~ 0,
      TRUE ~ 0)

)

coding for formal others - current - aging in communiuty

library(ggthemes)
library(ggcorrplot)
library(mclust)
library(psych)
library(GPArotation)
library(tidyverse)
survey_data_HTS <- survey_data_HTS |> 
  mutate(


## currently using
    
#- Technology buddy program
srv_lgbtq_tech_buddy_ynm = case_when(
      (srv_lgbtq_tech_buddy_ynm) == "No" ~ 0,
      (srv_lgbtq_tech_buddy_ynm) == "Maybe" ~ 1,
      (srv_lgbtq_tech_buddy_ynm) == "Yes" ~ 2,
      (srv_lgbtq_tech_buddy_ynm) == "-999" ~ 0,
      (srv_lgbtq_tech_buddy_ynm) == "-998" ~ 0,
      TRUE ~ 0),

#- Discussion / Support groups
srv_lgbtq_spprt_grps_ynm = case_when(
      (srv_lgbtq_spprt_grps_ynm) == "No" ~ 0,
      (srv_lgbtq_spprt_grps_ynm) == "Maybe" ~ 1,
      (srv_lgbtq_spprt_grps_ynm) == "Yes" ~ 2,
      (srv_lgbtq_spprt_grps_ynm) == "-999" ~ 0,
      (srv_lgbtq_spprt_grps_ynm) == "-998" ~ 0,
      TRUE ~ 0),

#- Telephone buddy program
srv_lgbtq_tele_buddy_ynm = case_when(
      (srv_lgbtq_tele_buddy_ynm) == "No" ~ 0,
      (srv_lgbtq_tele_buddy_ynm) == "Maybe" ~ 1,
      (srv_lgbtq_tele_buddy_ynm) == "Yes" ~ 2,
      (srv_lgbtq_tele_buddy_ynm) == "-999" ~ 0,
      (srv_lgbtq_tele_buddy_ynm) == "-998" ~ 0,
      TRUE ~ 0),


#- Lunch and learns
srv_lgbtq_lunch_learn_ynm = case_when(
      (srv_lgbtq_lunch_learn_ynm) == "No" ~ 0,
      (srv_lgbtq_lunch_learn_ynm) == "Maybe" ~ 1,
      (srv_lgbtq_lunch_learn_ynm) == "Yes" ~ 2,
      (srv_lgbtq_lunch_learn_ynm) == "-999" ~ 0,
      (srv_lgbtq_lunch_learn_ynm) == "-998" ~ 0,
      TRUE ~ 0),

#- Social outings/activities
srv_lgbtq_scl_act_ynm = case_when(
      (srv_lgbtq_scl_act_ynm) == "No" ~ 0,
      (srv_lgbtq_scl_act_ynm) == "Maybe" ~ 1,
      (srv_lgbtq_scl_act_ynm) == "Yes" ~ 2,
      (srv_lgbtq_scl_act_ynm) == "-999" ~ 0,
      (srv_lgbtq_scl_act_ynm) == "-998" ~ 0,
      TRUE ~ 0),


#- Virtual social events
srv_lgbtq_vrtl_scl_evnt_ynm = case_when(
      (srv_lgbtq_vrtl_scl_evnt_ynm) == "No" ~ 0,
      (srv_lgbtq_vrtl_scl_evnt_ynm) == "Maybe" ~ 1,
      (srv_lgbtq_vrtl_scl_evnt_ynm) == "Yes" ~ 2,
      (srv_lgbtq_vrtl_scl_evnt_ynm) == "-999" ~ 0,
      (srv_lgbtq_vrtl_scl_evnt_ynm) == "-998" ~ 0,
      TRUE ~ 0),


#- Multi-generational meals
srv_lgbtq_multigen_meals_ynm = case_when(
      (srv_lgbtq_multigen_meals_ynm) == "No" ~ 0,
      (srv_lgbtq_multigen_meals_ynm) == "Maybe" ~ 1,
      (srv_lgbtq_multigen_meals_ynm) == "Yes" ~ 2,
      (srv_lgbtq_multigen_meals_ynm) == "-999" ~ 0,
      (srv_lgbtq_multigen_meals_ynm) == "-998" ~ 0,
      TRUE ~ 0))

coding for informal - friends - current - aging in community

library(ggthemes)
library(ggcorrplot)
library(mclust)
library(psych)
library(GPArotation)
library(tidyverse)

survey_data_HTS <- survey_data_HTS |> 
  mutate(
##informal - friends
#- I had friends that I can turn to for information, after
    cvd_after_frnd_info_freq3 = case_when(
      (cvd_after_frnd_info_freq3) == "Never" ~ 0,
      (cvd_after_frnd_info_freq3) == "Rarely" ~ 1,
      (cvd_after_frnd_info_freq3) == "Some of the time" ~ 2,
      (cvd_after_frnd_info_freq3) == "Most of the time" ~ 3,
      (cvd_after_frnd_info_freq3) == "Always" ~ 4,
      (cvd_after_frnd_info_freq3) == "-999" ~ 0,
      (cvd_after_frnd_info_freq3) == "-998" ~ 0,
      TRUE ~ 0),

#- I could depend on my friends for help or support , after
    cvd_after_frnd_spprt_freq3 = case_when(
      (cvd_after_frnd_spprt_freq3) == "Never" ~ 0,
      (cvd_after_frnd_spprt_freq3) == "Rarely" ~ 1,
      (cvd_after_frnd_spprt_freq3) == "Some of the time" ~ 2,
      (cvd_after_frnd_spprt_freq3) == "Most of the time" ~ 3,
      (cvd_after_frnd_spprt_freq3) == "Always" ~ 4,
      (cvd_after_frnd_spprt_freq3) == "-999" ~ 0,
      (cvd_after_frnd_spprt_freq3) == "-998" ~ 0,
      TRUE ~ 0),

#- I had frineds to whom I could talk about the pressures in my life, after
    cvd_after_frnd_tlk_prssr_freq3 = case_when(
      (cvd_after_frnd_tlk_prssr_freq3) == "Never" ~ 0,
      (cvd_after_frnd_tlk_prssr_freq3) == "Rarely" ~ 1,
      (cvd_after_frnd_tlk_prssr_freq3) == "Some of the time" ~ 2,
      (cvd_after_frnd_tlk_prssr_freq3) == "Most of the time" ~ 3,
      (cvd_after_frnd_tlk_prssr_freq3) == "Always" ~ 4,
      (cvd_after_frnd_tlk_prssr_freq3) == "-999" ~ 0,
      (cvd_after_frnd_tlk_prssr_freq3) == "-998" ~ 0,
      TRUE ~ 0),

#- I had friends with whom I could share my views, after
    cvd_after_frnd_shr_vews_freq3 = case_when(
      (cvd_after_frnd_shr_vews_freq3) == "Never" ~ 0,
      (cvd_after_frnd_shr_vews_freq3) == "Rarely" ~ 1,
      (cvd_after_frnd_shr_vews_freq3) == "Some of the time" ~ 2,
      (cvd_after_frnd_shr_vews_freq3) == "Most of the time" ~ 3,
      (cvd_after_frnd_shr_vews_freq3) == "Always" ~ 4,
      (cvd_after_frnd_shr_vews_freq3) == "-999" ~ 0,
      (cvd_after_frnd_shr_vews_freq3) == "-998" ~ 0,
      TRUE ~ 0),

#live with friends      
hsg_live_with_frnd_cb = case_when(
      (hsg_live_with_frnd_cb) == "Friends" ~ 1,
      (hsg_live_with_frnd_cb) == "-999" ~ 0,
      (hsg_live_with_frnd_cb) == "-998" ~ 0,
      TRUE ~ 0),

# - Someone drives me

       tsp_bfrcvd_othr_prsn_drv_me_freq4 = case_when(
      (tsp_bfrcvd_othr_prsn_drv_me_freq4) == "Never" ~ 0,
      (tsp_bfrcvd_othr_prsn_drv_me_freq4) == "Several times a year" ~ 1,
      (tsp_bfrcvd_othr_prsn_drv_me_freq4) == "Monthly" ~ 2,
      (tsp_bfrcvd_othr_prsn_drv_me_freq4) == "Weekly" ~ 3,
      (tsp_bfrcvd_othr_prsn_drv_me_freq4) == "Daily" ~ 4,
      (tsp_bfrcvd_othr_prsn_drv_me_freq4) == "-999" ~ 0,
      (tsp_bfrcvd_othr_prsn_drv_me_freq4) == "-998" ~ 0,
      TRUE ~ 0),   
 
#How safe do you feel when someone drives me     
      tsp_crrnt_othr_prsn_drv_me_freq3 = case_when(
      (tsp_crrnt_othr_prsn_drv_me_freq3) == "Never" ~ 0,
      (tsp_crrnt_othr_prsn_drv_me_freq3) == "Rarely" ~ 1,
      (tsp_crrnt_othr_prsn_drv_me_freq3) == "Some of the time" ~ 2,
      (tsp_crrnt_othr_prsn_drv_me_freq3) == "Most of the time" ~ 3,
      (tsp_crrnt_othr_prsn_drv_me_freq3) == "Always" ~ 4,
      (tsp_crrnt_othr_prsn_drv_me_freq3) == "-999" ~ 0,
      (tsp_crrnt_othr_prsn_drv_me_freq3) == "-998" ~ 0,
      TRUE ~ 0),

#Reliance on others (never or rarely would indicate supportive connections) 
      tsp_chllng_rely_othr_freq3 = case_when(
      (tsp_chllng_rely_othr_freq3) == "Never" ~ 0,
      (tsp_chllng_rely_othr_freq3) == "Rarely" ~ 1,
      (tsp_chllng_rely_othr_freq3) == "Some of the time" ~ 2,
      (tsp_chllng_rely_othr_freq3) == "Most of the time" ~ 3,
      (tsp_chllng_rely_othr_freq3) == "Always" ~ 4,
      (tsp_chllng_rely_othr_freq3) == "-999" ~ 0,
      (tsp_chllng_rely_othr_freq3) == "-998" ~ 0,
      TRUE ~ 0),
       
# you assisted with the previous tasks
spprt_to_frnd_non_rltv_yn = case_when(
      (spprt_to_frnd_non_rltv_yn) == "No" ~ 0,
      (spprt_to_frnd_non_rltv_yn) == "Yes" ~ 1,
      (spprt_to_frnd_non_rltv_yn) == "-999" ~ 0,
      (spprt_to_frnd_non_rltv_yn) == "-998" ~ 0,
      TRUE ~ 0),

# use these modes of communication with friends or family during the COVID-19 -  Text
      cvd_comm_sms_freq2 = case_when(
      (cvd_comm_sms_freq2) == "Never" ~ 0,
      (cvd_comm_sms_freq2) == "Less than monthly " ~ 1,
      (cvd_comm_sms_freq2) == "Monthly" ~ 2,
      (cvd_comm_sms_freq2) == "Several times a month" ~ 3,
      (cvd_comm_sms_freq2) == "Weekly" ~ 4,
      (cvd_comm_sms_freq2) == "Several times a week" ~ 5,
      (cvd_comm_sms_freq2) == "Every day" ~ 6,
      (cvd_comm_sms_freq2) == "-999" ~ 0,
      (cvd_comm_sms_freq2) == "-998" ~ 0,
      TRUE ~ 0),

# use these modes of communication with friends or family during the COVID-19- Email
      cvd_comm_email_freq2 = case_when(
      (cvd_comm_email_freq2) == "Never" ~ 0,
      (cvd_comm_email_freq2) == "Less than monthly " ~ 1,
      (cvd_comm_email_freq2) == "Monthly" ~ 2,
      (cvd_comm_email_freq2) == "Several times a month" ~ 3,
      (cvd_comm_email_freq2) == "Weekly" ~ 4,
      (cvd_comm_email_freq2) == "Several times a week" ~ 5,
      (cvd_comm_email_freq2) == "Every day" ~ 6,
      (cvd_comm_email_freq2) == "-999" ~ 0,
      (cvd_comm_email_freq2) == "-998" ~ 0,
      TRUE ~ 0),

# use these modes of communication with friends or family during the COVID-19- Video application (e.g., WhatsApp, FaceTime, Zoom)
      cvd_comm_videoapp_freq2 = case_when(
      (cvd_comm_videoapp_freq2) == "Never" ~ 0,
      (cvd_comm_videoapp_freq2) == "Less than monthly " ~ 1,
      (cvd_comm_videoapp_freq2) == "Monthly" ~ 2,
      (cvd_comm_videoapp_freq2) == "Several times a month" ~ 3,
      (cvd_comm_videoapp_freq2) == "Weekly" ~ 4,
      (cvd_comm_videoapp_freq2) == "Several times a week" ~ 5,
      (cvd_comm_videoapp_freq2) == "Every day" ~ 6,
      (cvd_comm_videoapp_freq2) == "-999" ~ 0,
      (cvd_comm_videoapp_freq2) == "-998" ~ 0,
      TRUE ~ 0),

# use these modes of communication with friends or family during the COVID-19- Social media (e.g., Facebook, Instagram)
      cvd_comm_social_media_freq2 = case_when(
      (cvd_comm_social_media_freq2) == "Never" ~ 0,
      (cvd_comm_social_media_freq2) == "Less than monthly " ~ 1,
      (cvd_comm_social_media_freq2) == "Monthly" ~ 2,
      (cvd_comm_social_media_freq2) == "Several times a month" ~ 3,
      (cvd_comm_social_media_freq2) == "Weekly" ~ 4,
      (cvd_comm_social_media_freq2) == "Several times a week" ~ 5,
      (cvd_comm_social_media_freq2) == "Every day" ~ 6,
      (cvd_comm_social_media_freq2) == "-999" ~ 0,
      (cvd_comm_social_media_freq2) == "-998" ~ 0,
      TRUE ~ 0),

# use these modes of communication with friends or family during the COVID-19- Stand-alone video device (e.g., Portal, Echo Show)
      cvd_comm_video_device_freq2 = case_when(
      (cvd_comm_video_device_freq2) == "Never" ~ 0,
      (cvd_comm_video_device_freq2) == "Less than monthly " ~ 1,
      (cvd_comm_video_device_freq2) == "Monthly" ~ 2,
      (cvd_comm_video_device_freq2) == "Several times a month" ~ 3,
      (cvd_comm_video_device_freq2) == "Weekly" ~ 4,
      (cvd_comm_video_device_freq2) == "Several times a week" ~ 5,
      (cvd_comm_video_device_freq2) == "Every day" ~ 6,
      (cvd_comm_video_device_freq2) == "-999" ~ 0,
      (cvd_comm_video_device_freq2) == "-998" ~ 0,
      TRUE ~ 0),


#- Had to assume new or greater caregiving responsibilities for children/grandchildren insider my house
   cvd_impct_careg_frnds_in_home_freq1 = case_when(
      (cvd_impct_careg_frnds_in_home_freq1) == "Not at all" ~ 0,
      (cvd_impct_careg_frnds_in_home_freq1) == "Rarely" ~ 1,
      (cvd_impct_careg_frnds_in_home_freq1) == "Sometimes" ~ 2,
      (cvd_impct_careg_frnds_in_home_freq1) == "Often" ~ 3,
      (cvd_impct_careg_frnds_in_home_freq1) == "Very Frequently" ~ 4,
      (cvd_impct_careg_frnds_in_home_freq1) == "-999" ~ 0,
      (cvd_impct_careg_frnds_in_home_freq1) == "-998" ~ 0,
      TRUE ~ 0),
#- Had to assume new or greater caregiving responsibilities for children/grandchildren outside my house
   cvd_impct_careg_frnds_out_home_freq1 = case_when(
      (cvd_impct_careg_frnds_out_home_freq1) == "Not at all" ~ 0,
      (cvd_impct_careg_frnds_out_home_freq1) == "Rarely" ~ 1,
      (cvd_impct_careg_frnds_out_home_freq1) == "Sometimes" ~ 2,
      (cvd_impct_careg_frnds_out_home_freq1) == "Often" ~ 3,
      (cvd_impct_careg_frnds_out_home_freq1) == "Very Frequently" ~ 4,
      (cvd_impct_careg_frnds_out_home_freq1) == "-999" ~ 0,
      (cvd_impct_careg_frnds_out_home_freq1) == "-998" ~ 0,
      TRUE ~ 0)

)

coding - informal - family

library(ggthemes)
library(ggcorrplot)
library(mclust)
library(psych)
library(GPArotation)
library(tidyverse)

survey_data_HTS <- survey_data_HTS |> 
  mutate(
##informal - family
    
# - Partner/Spouse
    hsg_live_with_partner_spouse_cb = case_when(
      (hsg_live_with_partner_spouse_cb) == "Partner / spouse" ~ 1,
      (hsg_live_with_partner_spouse_cb) == "-999" ~ 0,
      (hsg_live_with_partner_spouse_cb) == "-998" ~ 0,
      TRUE ~ 0),

#- Adult child / children (over 18)
hsg_live_with_adlt_chld_cb = case_when(
      (hsg_live_with_adlt_chld_cb) == "Adult child / children (over 18)" ~ 1,
      (hsg_live_with_adlt_chld_cb) == "-999" ~ 0,
      (hsg_live_with_adlt_chld_cb) == "-998" ~ 0,
      TRUE ~ 0),
# - Minor age child / children (11-17 years old)
hsg_live_with_adlscnt_chld_cb = case_when(
      (hsg_live_with_adlscnt_chld_cb) == "Minor age child / children (11-17 years old)" ~ 1,
      (hsg_live_with_adlscnt_chld_cb) == "-999" ~ 0,
      (hsg_live_with_adlscnt_chld_cb) == "-998" ~ 0,
      TRUE ~ 0),
# - Minor child / children (under age 10)
hsg_live_with_youth_chld_cb = case_when(
      (hsg_live_with_youth_chld_cb) == "Minor child / children (under age 10)" ~ 1,
      (hsg_live_with_youth_chld_cb) == "-999" ~ 0,
      (hsg_live_with_youth_chld_cb) == "-998" ~ 0,
      TRUE ~ 0),

#- Had to assume new or greater caregiving responsibilities for children/grandchildren insider my house
   cvd_impct_careg_kids_in_home_freq1 = case_when(
      (cvd_impct_careg_kids_in_home_freq1) == "Not at all" ~ 0,
      (cvd_impct_careg_kids_in_home_freq1) == "Rarely" ~ 1,
      (cvd_impct_careg_kids_in_home_freq1) == "Sometimes" ~ 2,
      (cvd_impct_careg_kids_in_home_freq1) == "Often" ~ 3,
      (cvd_impct_careg_kids_in_home_freq1) == "Very Frequently" ~ 4,
      (cvd_impct_careg_kids_in_home_freq1) == "-999" ~ 0,
      (cvd_impct_careg_kids_in_home_freq1) == "-998" ~ 0,
      TRUE ~ 0),

#- Had to assume new or greater caregiving responsibilities for children/grandchildren outside my house
   cvd_impct_careg_kids_out_home_freq1 = case_when(
      (cvd_impct_careg_kids_out_home_freq1) == "Not at all" ~ 0,
      (cvd_impct_careg_kids_out_home_freq1) == "Rarely" ~ 1,
      (cvd_impct_careg_kids_out_home_freq1) == "Sometimes" ~ 2,
      (cvd_impct_careg_kids_out_home_freq1) == "Often" ~ 3,
      (cvd_impct_careg_kids_out_home_freq1) == "Very Frequently" ~ 4,
      (cvd_impct_careg_kids_out_home_freq1) == "-999" ~ 0,
      (cvd_impct_careg_kids_out_home_freq1) == "-998" ~ 0,
      TRUE ~ 0),

# help other - Transportation (e.g. rides to appointments / grocery store)
spprt_othr_tsp_cb = case_when(
      (spprt_othr_tsp_cb) == "ransportation (e.g. rides to appointments / grocery store)" ~ 1,
      (spprt_othr_tsp_cb) == "-999" ~ 0,
      (spprt_othr_tsp_cb) == "-998" ~ 0,
      TRUE ~ 0),

# help other - Financial support
spprt_othr_fncl_cb = case_when(
      (spprt_othr_fncl_cb) == "Financial support" ~ 1,
      (spprt_othr_fncl_cb) == "-999" ~ 0,
      (spprt_othr_fncl_cb) == "-998" ~ 0,
      TRUE ~ 0),

# help other - Groceries / food
spprt_othr_food_cb = case_when(
      (spprt_othr_food_cb) == "Groceries / food" ~ 1,
      (spprt_othr_food_cb) == "-999" ~ 0,
      (spprt_othr_food_cb) == "-998" ~ 0,
      TRUE ~ 0),

# help other - Technology support (e.g. help with using Zoom, Skype, new tablet, etc)
spprt_othr_tech_adv_cb = case_when(
      (spprt_othr_tech_adv_cb) == "Technology support (e.g. help with using Zoom, Skype, new tablet, etc)" ~ 1,
      (spprt_othr_tech_adv_cb) == "-999" ~ 0,
      (spprt_othr_tech_adv_cb) == "-998" ~ 0,
      TRUE ~ 0),

# help other - Emotional support
spprt_othr_emtnl_spprt_cb = case_when(
      (spprt_othr_emtnl_spprt_cb) == "Emotional support" ~ 1,
      (spprt_othr_emtnl_spprt_cb) == "-999" ~ 0,
      (spprt_othr_emtnl_spprt_cb) == "-998" ~ 0,
      TRUE ~ 0),

# help other - Assistance with bathing, dressing, grooming, toileting, walking, climbing stairs, transferring to a bed/chair/car, eating
spprt_othr_crgvng_cb = case_when(
      (spprt_othr_crgvng_cb) == "Assistance with bathing, dressing, grooming, toileting, walking, climbing stairs, transferring to a bed/chair/car, eating" ~ 1,
      (spprt_othr_crgvng_cb) == "-999" ~ 0,
      (spprt_othr_crgvng_cb) == "-998" ~ 0,
      TRUE ~ 0),

# help other - Assistance with shopping, cooking, managing medications, using the phone / looking up numbers
spprt_othr_shppng_cb = case_when(
      (spprt_othr_shppng_cb) == "Assistance with shopping, cooking, managing medications, using the phone / looking up numbers" ~ 1,
      (spprt_othr_shppng_cb) == "-999" ~ 0,
      (spprt_othr_shppng_cb) == "-998" ~ 0,
      TRUE ~ 0),

# help other - Assistance with housework, laundry, driving/using public transportation, or managing finances
spprt_othr_hhld_wrk_cb = case_when(
      (spprt_othr_hhld_wrk_cb) == "Assistance with shopping, cooking, managing medications, using the phone/looking up numbers, housework, laundry, driving/using public transportation, or managing finances" ~ 1,
      (spprt_othr_hhld_wrk_cb) == "-999" ~ 0,
      (spprt_othr_hhld_wrk_cb) == "-998" ~ 0,
      TRUE ~ 0),
 
# you assisted with the previous tasks - Spouse / partner
spprt_to_spouse_prtnr_yn = case_when(
      (spprt_to_spouse_prtnr_yn) == "Yes" ~ 1,
      (spprt_to_spouse_prtnr_yn) == "No" ~ 0,
      (spprt_to_spouse_prtnr_yn) == "-999" ~ 0,
      (spprt_to_spouse_prtnr_yn) == "-998" ~ 0,
      TRUE ~ 0),

# you assisted with the previous tasks - Child / children
spprt_to_chldrn_yn = case_when(
      (spprt_to_chldrn_yn) == "Yes" ~ 1,
      (spprt_to_chldrn_yn) == "No" ~ 0,
      (spprt_to_chldrn_yn) == "-999" ~ 0,
      (spprt_to_chldrn_yn) == "-998" ~ 0,
      TRUE ~ 0),

# you assisted with the previous tasks - Grandchild or grandchildren
spprt_to_grndchldrn_yn = case_when(
      (spprt_to_grndchldrn_yn) == "Yes" ~ 1,
      (spprt_to_grndchldrn_yn) == "No" ~ 0,
      (spprt_to_grndchldrn_yn) == "-999" ~ 0,
      (spprt_to_grndchldrn_yn) == "-998" ~ 0,
      TRUE ~ 0)

)

FA - Loneliness

survey_data_lonely1_scaled <- survey_data_HTS %>% 
  select(hlth_lack_cmpnship_freq7,
          hlth_feel_left_out_freq7,
          hlth_feel_isolated_freq7) |>
  mutate(across(.cols = everything(), .fns = ~ scale(.) %>% as.vector))


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

## Parallel analysis suggests that the number of factors =  1  and the number of components =  NA
fit_lonely <- 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_lonely, 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.91      0.83 0.17   1
## hlth_feel_left_out_freq7    2 0.91      0.83 0.17   1
## hlth_lack_cmpnship_freq7    1      0.95 0.89 0.11   1
## 
##                       GLS1 GLS2
## SS loadings           1.66 0.89
## Proportion Var        0.55 0.30
## Cumulative Var        0.55 0.85
## Proportion Explained  0.65 0.35
## Cumulative Proportion 0.65 1.00
## 
##  With factor correlations of 
##      GLS1 GLS2
## GLS1 1.00 0.88
## GLS2 0.88 1.00
## 
## Mean item complexity =  1
## Test of the hypothesis that 2 factors are sufficient.
## 
## df null model =  3  with the objective function =  2.14 with Chi Square =  559.71
## df of  the model are -2  and the objective function was  0 
## 
## The root mean square of the residuals (RMSR) is  0 
## The df corrected root mean square of the residuals is  NA 
## 
## The harmonic n.obs is  264 with the empirical chi square  0  with prob <  NA 
## The total n.obs was  264  with Likelihood Chi Square =  0  with prob <  NA 
## 
## Tucker Lewis Index of factoring reliability =  1.005
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy             
##                                                   GLS1 GLS2
## Correlation of (regression) scores with factors   0.96 0.96
## Multiple R square of scores with factors          0.92 0.92
## Minimum correlation of possible factor scores     0.85 0.83
loadings_loneliness <- fit_lonely$loadings
fa.diagram(loadings_loneliness, digits = 2, cut = 0.4)

# suggested interpretation: 1 = feel being alone; 2 = lack companionship

lonely_scores <- psych::factor.scores(x = survey_data_lonely1_scaled, f = fit_lonely, method = "tenBerge")

survey_data_HTS  <- survey_data_HTS %>% 
  mutate(
    L_fa1_feel_being_alone = lonely_scores$scores[,1],
    L_fa2_lack_compnship = lonely_scores$scores[,2]
  )

FA - formal lgbtq activities

survey_data_formal_lgbtq_scaled <- survey_data_HTS %>% 
  select(lgbtq_cmmty_spprt_grps_ordscl1,
      lgbtq_cmmty_one_lcl_org_ordscl1,
      lgbtq_cmmty_many_lcl_org_ordscl1,
      lgbtq_cmmty_intrnt_grps_ordscl1,
      lgbtq_cmmty_natl_ntwrk_ordscl1,
      lgbtq_cmmty_faith_ordscl1,
      srv_lgbtq_ntwrkng_ynm,
      srv_lgbtq_trans_prgmng_ynm
         )|>
  mutate(across(.cols = everything(), .fns = ~ scale(.) %>% as.vector))

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

## Parallel analysis suggests that the number of factors =  2  and the number of components =  NA
fit_formal_lgbt <- fa(
  r = survey_data_formal_lgbtq_scaled, nfactors = 2, 
  fm = "gls", rotate = "oblimin", 
  scores = "tenBerge", oblique.scores = TRUE,
  alpha = 0.05, p = 0.05
)
psych::print.psych(fit_formal_lgbt, digits = 2, cut = 0.4, sort = TRUE, signif = !NULL)
## Factor Analysis using method =  gls
## Call: fa(r = survey_data_formal_lgbtq_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_many_lcl_org_ordscl1    3  0.77       0.60 0.40 1.0
## lgbtq_cmmty_spprt_grps_ordscl1      1  0.73       0.52 0.48 1.0
## lgbtq_cmmty_natl_ntwrk_ordscl1      5  0.60       0.37 0.63 1.0
## lgbtq_cmmty_one_lcl_org_ordscl1     2  0.59       0.35 0.65 1.0
## lgbtq_cmmty_faith_ordscl1           6  0.57       0.33 0.67 1.0
## lgbtq_cmmty_intrnt_grps_ordscl1     4  0.43       0.21 0.79 1.2
## srv_lgbtq_ntwrkng_ynm               7  0.41       0.30 0.70 1.9
## srv_lgbtq_trans_prgmng_ynm          8        0.90 0.81 0.19 1.0
## 
##                       GLS1 GLS2
## SS loadings           2.53 0.97
## Proportion Var        0.32 0.12
## Cumulative Var        0.32 0.44
## Proportion Explained  0.72 0.28
## Cumulative Proportion 0.72 1.00
## 
##  With factor correlations of 
##      GLS1 GLS2
## GLS1 1.00 0.09
## GLS2 0.09 1.00
## 
## Mean item complexity =  1.1
## Test of the hypothesis that 2 factors are sufficient.
## 
## df null model =  28  with the objective function =  1.92 with Chi Square =  499.44
## df of  the model are 13  and the objective function was  0.19 
## 
## The root mean square of the residuals (RMSR) is  0.06 
## The df corrected root mean square of the residuals is  0.08 
## 
## The harmonic n.obs is  264 with the empirical chi square  47.02  with prob <  9.6e-06 
## The total n.obs was  264  with Likelihood Chi Square =  48.58  with prob <  5.2e-06 
## 
## Tucker Lewis Index of factoring reliability =  0.837
## RMSEA index =  0.102  and the 95 % confidence intervals are  0.066 0.139
## BIC =  -23.91
## Fit based upon off diagonal values = 0.97
## Measures of factor score adequacy             
##                                                   GLS1 GLS2
## Correlation of (regression) scores with factors   0.91 0.91
## Multiple R square of scores with factors          0.83 0.83
## Minimum correlation of possible factor scores     0.66 0.65
loadings_formal_lgbt <- fit_formal_lgbt$loadings
fa.diagram(loadings_formal_lgbt, digits = 2, cut = 0.4)

# suggested interpretation: 1 = through lgbtq community activity; 2 = use targeted_service

formal_lgbtq_scores <- psych::factor.scores(x = survey_data_formal_lgbtq_scaled, f = fit_formal_lgbt, method = "tenBerge")

survey_data_HTS <- survey_data_HTS %>% 
  mutate(
    C_lgbtq_community = formal_lgbtq_scores$scores[,1],
    C_lgbtq_targeted_service = formal_lgbtq_scores$scores[,2]
  )

FA - formal others

survey_data_formal_others_scaled <- survey_data_HTS %>% 
  select(srv_lgbtq_tech_buddy_ynm,
         srv_lgbtq_spprt_grps_ynm,
         srv_lgbtq_tele_buddy_ynm,
         srv_lgbtq_lunch_learn_ynm,
         srv_lgbtq_scl_act_ynm,
         srv_lgbtq_vrtl_scl_evnt_ynm,
         srv_lgbtq_multigen_meals_ynm) |>
  mutate(across(.cols = everything(), .fns = ~ scale(.) %>% as.vector))

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

## Parallel analysis suggests that the number of factors =  2  and the number of components =  NA
fit_formal_others <- fa(
  r = survey_data_formal_others_scaled, nfactors = 2, 
  fm = "gls", rotate = "oblimin", 
  scores = "tenBerge", oblique.scores = TRUE,
  alpha = 0.05, p = 0.05
)
psych::print.psych(fit_formal_others, digits = 2, cut = 0.4, sort = TRUE, signif = !NULL)
## Factor Analysis using method =  gls
## Call: fa(r = survey_data_formal_others_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
## srv_lgbtq_scl_act_ynm           5  0.85       0.62 0.38 1.0
## srv_lgbtq_vrtl_scl_evnt_ynm     6  0.73       0.51 0.49 1.0
## srv_lgbtq_multigen_meals_ynm    7  0.62       0.51 0.49 1.1
## srv_lgbtq_lunch_learn_ynm       4  0.54       0.52 0.48 1.4
## srv_lgbtq_spprt_grps_ynm        2  0.52       0.47 0.53 1.3
## srv_lgbtq_tech_buddy_ynm        1        0.93 0.82 0.18 1.0
## srv_lgbtq_tele_buddy_ynm        3        0.76 0.70 0.30 1.0
## 
##                       GLS1 GLS2
## SS loadings           2.40 1.76
## Proportion Var        0.34 0.25
## Cumulative Var        0.34 0.59
## Proportion Explained  0.58 0.42
## Cumulative Proportion 0.58 1.00
## 
##  With factor correlations of 
##      GLS1 GLS2
## GLS1 1.00 0.66
## GLS2 0.66 1.00
## 
## Mean item complexity =  1.1
## Test of the hypothesis that 2 factors are sufficient.
## 
## df null model =  21  with the objective function =  3.34 with Chi Square =  867.06
## df of  the model are 8  and the objective function was  0.15 
## 
## The root mean square of the residuals (RMSR) is  0.04 
## The df corrected root mean square of the residuals is  0.07 
## 
## The harmonic n.obs is  264 with the empirical chi square  18.01  with prob <  0.021 
## The total n.obs was  264  with Likelihood Chi Square =  38.27  with prob <  6.7e-06 
## 
## Tucker Lewis Index of factoring reliability =  0.906
## RMSEA index =  0.12  and the 95 % confidence intervals are  0.076 0.166
## BIC =  -6.34
## Fit based upon off diagonal values = 0.99
## Measures of factor score adequacy             
##                                                   GLS1 GLS2
## Correlation of (regression) scores with factors   0.92 0.94
## Multiple R square of scores with factors          0.85 0.89
## Minimum correlation of possible factor scores     0.70 0.77
loadings_formal_others <- fit_formal_others$loadings
fa.diagram(loadings_formal_others, digits = 2, cut = 0.4)

# suggested interpretation: 1 = social ; 2 = buddy

formal_others_scores <- psych::factor.scores(x = survey_data_formal_others_scaled, f = fit_formal_others, method = "tenBerge")

survey_data_HTS <- survey_data_HTS %>% 
  mutate(
    E_social_activities = formal_others_scores$scores[,1],
    E_buddy_program = formal_others_scores$scores[,2]
  )

FA - friends

survey_data_informal_friends_scaled <- survey_data_HTS %>% 
  select(
        cvd_comm_email_freq2,
        cvd_comm_videoapp_freq2,
        cvd_comm_social_media_freq2,
        cvd_comm_video_device_freq2,
        cvd_after_frnd_info_freq3,
        cvd_after_frnd_spprt_freq3,
        cvd_after_frnd_tlk_prssr_freq3,
        cvd_after_frnd_shr_vews_freq3,
        hsg_live_with_frnd_cb,
        tsp_bfrcvd_othr_prsn_drv_me_freq4,
        tsp_crrnt_othr_prsn_drv_me_freq3,
        tsp_chllng_rely_othr_freq3,
        spprt_to_frnd_non_rltv_yn) |>
  mutate(across(.cols = everything(), .fns = ~ scale(.) %>% as.vector))

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

## Parallel analysis suggests that the number of factors =  4  and the number of components =  NA
fit_informal_friends <- fa(
  r = survey_data_informal_friends_scaled, nfactors = 4, 
  fm = "gls", rotate = "oblimin", 
  scores = "tenBerge", oblique.scores = TRUE,
  alpha = 0.05, p = 0.05
)
psych::print.psych(fit_informal_friends, digits = 2, cut = 0.4, sort = TRUE, signif = !NULL)
## Factor Analysis using method =  gls
## Call: fa(r = survey_data_informal_friends_scaled, nfactors = 4, 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  GLS4  GLS3   h2   u2 com
## cvd_after_frnd_tlk_prssr_freq3       7  0.89                   0.78 0.22 1.0
## cvd_after_frnd_shr_vews_freq3        8  0.86                   0.76 0.24 1.0
## cvd_after_frnd_info_freq3            5  0.82                   0.68 0.32 1.0
## cvd_after_frnd_spprt_freq3           6  0.81                   0.66 0.34 1.0
## cvd_comm_videoapp_freq2              2        0.78             0.61 0.39 1.0
## cvd_comm_email_freq2                 1        0.59             0.31 0.69 1.1
## cvd_comm_video_device_freq2          4        0.53             0.51 0.49 1.7
## cvd_comm_social_media_freq2          3        0.50             0.28 0.72 1.0
## tsp_bfrcvd_othr_prsn_drv_me_freq4   10              0.74       0.57 0.43 1.0
## tsp_chllng_rely_othr_freq3          12              0.59       0.39 0.61 1.1
## tsp_crrnt_othr_prsn_drv_me_freq3    11              0.55       0.27 0.73 1.3
## spprt_to_frnd_non_rltv_yn           13                    0.78 0.61 0.39 1.0
## hsg_live_with_frnd_cb                9                    0.69 0.52 0.48 1.1
## 
##                       GLS1 GLS2 GLS4 GLS3
## SS loadings           2.91 1.57 1.37 1.11
## Proportion Var        0.22 0.12 0.11 0.09
## Cumulative Var        0.22 0.34 0.45 0.54
## Proportion Explained  0.42 0.23 0.20 0.16
## Cumulative Proportion 0.42 0.64 0.84 1.00
## 
##  With factor correlations of 
##       GLS1 GLS2  GLS4 GLS3
## GLS1  1.00 0.06 -0.09 0.07
## GLS2  0.06 1.00  0.39 0.03
## GLS4 -0.09 0.39  1.00 0.07
## GLS3  0.07 0.03  0.07 1.00
## 
## Mean item complexity =  1.1
## Test of the hypothesis that 4 factors are sufficient.
## 
## df null model =  78  with the objective function =  5 with Chi Square =  1289.84
## df of  the model are 32  and the objective function was  0.57 
## 
## The root mean square of the residuals (RMSR) is  0.05 
## The df corrected root mean square of the residuals is  0.08 
## 
## The harmonic n.obs is  264 with the empirical chi square  104.28  with prob <  1.4e-09 
## The total n.obs was  264  with Likelihood Chi Square =  146.59  with prob <  1.3e-16 
## 
## Tucker Lewis Index of factoring reliability =  0.767
## RMSEA index =  0.116  and the 95 % confidence intervals are  0.094 0.14
## BIC =  -31.84
## Fit based upon off diagonal values = 0.97
## Measures of factor score adequacy             
##                                                   GLS1 GLS2 GLS4 GLS3
## Correlation of (regression) scores with factors   0.96 0.86 0.83 0.93
## Multiple R square of scores with factors          0.92 0.74 0.69 0.87
## Minimum correlation of possible factor scores     0.83 0.48 0.37 0.74
loadings_informal_friends <- fit_informal_friends$loadings
fa.diagram(loadings_informal_friends, digits = 2, cut = 0.4)

# suggested interpretation: 1 = psychosocial support; 2 = virtual connection; 3 = living arrangement/ daily connection; 4 = practical support

informal_friends_scores <- psych::factor.scores(x = survey_data_informal_friends_scaled, f = fit_informal_friends, method = "tenBerge")

survey_data_HTS <- survey_data_HTS %>% 
  mutate(
    Friend_psychosocial_support = informal_friends_scores$scores[,1],
    Friend_virtual_connection = informal_friends_scores$scores[,2],
    Friend_live_support_to = informal_friends_scores$scores[,3],
    Friend_partical_support = informal_friends_scores$scores[,4]
    
  )

FA - family

survey_data_informal_family_scaled <- survey_data_HTS %>% 
  select(
        cvd_impct_careg_kids_in_home_freq1,
        cvd_impct_careg_kids_out_home_freq1,
        hsg_live_with_partner_spouse_cb,
        hsg_live_with_adlt_chld_cb,
        hsg_live_with_adlscnt_chld_cb,
        hsg_live_with_youth_chld_cb,
        spprt_othr_tsp_cb,
        spprt_othr_fncl_cb,
        spprt_othr_food_cb,
        spprt_othr_tech_adv_cb,
        spprt_othr_emtnl_spprt_cb,
        spprt_othr_crgvng_cb,
        spprt_othr_shppng_cb,
        spprt_othr_hhld_wrk_cb,
        spprt_to_spouse_prtnr_yn,
        spprt_to_chldrn_yn,
        spprt_to_grndchldrn_yn
       ) |>
  mutate(across(.cols = everything(), .fns = ~ scale(.) %>% as.vector))

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

## Parallel analysis suggests that the number of factors =  3  and the number of components =  NA
fit_informal_family <- fa(
  r = survey_data_informal_family_scaled, nfactors = 3, 
  fm = "gls", rotate = "oblimin", 
  scores = "tenBerge", oblique.scores = TRUE,
  alpha = 0.05, p = 0.05
)
psych::print.psych(fit_informal_family, digits = 2, cut = 0.4, sort = TRUE, signif = !NULL)
## Factor Analysis using method =  gls
## Call: fa(r = survey_data_informal_family_scaled, nfactors = 3, 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  GLS3   h2   u2 com
## spprt_othr_shppng_cb                  13  0.84             0.68 0.32 1.0
## spprt_othr_hhld_wrk_cb                14  0.75             0.56 0.44 1.0
## spprt_othr_tsp_cb                      7  0.64             0.47 0.53 1.2
## spprt_othr_food_cb                     9  0.60             0.43 0.57 1.1
## spprt_othr_tech_adv_cb                10  0.58             0.41 0.59 1.3
## spprt_othr_crgvng_cb                  12  0.50             0.28 0.72 1.1
## spprt_othr_emtnl_spprt_cb             11  0.50             0.41 0.59 1.8
## spprt_othr_fncl_cb                     8  0.48             0.40 0.60 1.8
## hsg_live_with_youth_chld_cb            6        0.77       0.58 0.42 1.2
## hsg_live_with_adlt_chld_cb             4        0.71       0.49 0.51 1.1
## cvd_impct_careg_kids_in_home_freq1     1        0.69       0.60 0.40 1.2
## hsg_live_with_adlscnt_chld_cb          5        0.66       0.47 0.53 1.1
## spprt_to_chldrn_yn                    16        0.66       0.52 0.48 1.3
## cvd_impct_careg_kids_out_home_freq1    2        0.48       0.23 0.77 1.0
## spprt_to_grndchldrn_yn                17        0.46       0.21 0.79 1.3
## hsg_live_with_partner_spouse_cb        3              0.77 0.61 0.39 1.0
## spprt_to_spouse_prtnr_yn              15              0.70 0.50 0.50 1.0
## 
##                       GLS1 GLS2 GLS3
## SS loadings           3.36 3.19 1.31
## Proportion Var        0.20 0.19 0.08
## Cumulative Var        0.20 0.39 0.46
## Proportion Explained  0.43 0.41 0.17
## Cumulative Proportion 0.43 0.83 1.00
## 
##  With factor correlations of 
##      GLS1 GLS2 GLS3
## GLS1 1.00 0.20 0.12
## GLS2 0.20 1.00 0.08
## GLS3 0.12 0.08 1.00
## 
## Mean item complexity =  1.2
## Test of the hypothesis that 3 factors are sufficient.
## 
## df null model =  136  with the objective function =  6.78 with Chi Square =  1739.6
## df of  the model are 88  and the objective function was  1.18 
## 
## The root mean square of the residuals (RMSR) is  0.05 
## The df corrected root mean square of the residuals is  0.06 
## 
## The harmonic n.obs is  264 with the empirical chi square  185.43  with prob <  6.4e-09 
## The total n.obs was  264  with Likelihood Chi Square =  300.3  with prob <  5.6e-25 
## 
## Tucker Lewis Index of factoring reliability =  0.794
## RMSEA index =  0.096  and the 95 % confidence intervals are  0.082 0.11
## BIC =  -190.38
## Fit based upon off diagonal values = 0.97
## Measures of factor score adequacy             
##                                                   GLS1 GLS2 GLS3
## Correlation of (regression) scores with factors   0.94 0.93 0.86
## Multiple R square of scores with factors          0.88 0.87 0.74
## Minimum correlation of possible factor scores     0.76 0.73 0.49
loadings_informal_family <- fit_informal_family$loadings
fa.diagram(loadings_informal_family, digits = 2, cut = 0.4)

# suggested interpretation: 1 = general support to family ; 2= live and support child; 3 = live and support partner

informal_family_scores <- psych::factor.scores(x = survey_data_informal_family_scaled, f = fit_informal_family, method = "tenBerge")

survey_data_HTS <- survey_data_HTS %>% 
  mutate(
    Family_support_general = informal_family_scores$scores[,1],
    Family_support_child = informal_family_scores$scores[,2],
    Family_support_partner = informal_family_scores$scores[,3]
  )

#LPA

need_index_vars_four <- c(
  "C_lgbtq_community",
  "C_lgbtq_targeted_service",
  "E_social_activities",
  "E_buddy_program",
  "Family_support_general",
  "Family_support_child",
  "Family_support_partner",
  "Friend_partical_support",
  "Friend_psychosocial_support",
  "Friend_virtual_connection",
  "Friend_live_support_to",
  "hlth_dsblty_strg_mntl_cb",
  "hlth_dsblty_bhvrl_mtnl_cb",
  "hlth_tense_dwn_low_freq3",
  "L_fa1_feel_being_alone",
  "L_fa2_lack_compnship"
  )

survey_data_updated_need_vars_scaled <- survey_data_HTS %>%
  select(all_of(need_index_vars_four)) %>%
  mutate(across(.cols = everything(), .fns = ~ scale(.) %>% as.vector))


# view correlations among factor-score-based composite need-index variables
survey_data_updated_need_vars_scaled %>%
  corrr::correlate() %>%
  corrr::rearrange(absolute = FALSE) %>%
  corrr::shave() %>%
  corrr::as_matrix() %>%
ggcorrplot(
  outline.color = "white", type = "lower",
  ggtheme = ggplot2::theme_gray,
  colors = c("#E46726", "white", "#6D9EC1"),
  lab = TRUE,
  digits = 1
)

# seletcion criteria - BIC and ICL estimation may take some time to run
BIC <- mclust::mclustBIC(survey_data_updated_need_vars_scaled)
plot(BIC)

mod1 <- Mclust(survey_data_updated_need_vars_scaled, modelNames = "VEI", G = 6, x = BIC)
summary(mod1)
## ---------------------------------------------------- 
## Gaussian finite mixture model fitted by EM algorithm 
## ---------------------------------------------------- 
## 
## Mclust VEI (diagonal, equal shape) model with 6 components: 
## 
##  log-likelihood   n  df       BIC       ICL
##       -4532.371 264 122 -9745.008 -9745.008
## 
## Clustering table:
##   1   2   3   4   5   6 
## 143  41  47  11  16   6
mod2 <- Mclust(survey_data_updated_need_vars_scaled, modelNames = "VEI", G = 5, x = BIC)
summary(mod2)
## ---------------------------------------------------- 
## Gaussian finite mixture model fitted by EM algorithm 
## ---------------------------------------------------- 
## 
## Mclust VEI (diagonal, equal shape) model with 5 components: 
## 
##  log-likelihood   n  df       BIC       ICL
##       -4750.046 264 104 -10079.99 -10080.09
## 
## Clustering table:
##   1   2   3   4   5 
## 143  47  47  11  16
mod3 <- Mclust(survey_data_updated_need_vars_scaled, modelNames = "VEI", G = 4, x = BIC)
summary(mod3)
## ---------------------------------------------------- 
## Gaussian finite mixture model fitted by EM algorithm 
## ---------------------------------------------------- 
## 
## Mclust VEI (diagonal, equal shape) model with 4 components: 
## 
##  log-likelihood   n df       BIC       ICL
##       -5209.615 264 86 -10898.76 -10906.15
## 
## Clustering table:
##   1   2   3   4 
## 124 113  11  16
mod4 <- Mclust(survey_data_updated_need_vars_scaled, modelNames = "EEV", G = 3, x = BIC)
summary(mod4)
## ---------------------------------------------------- 
## Gaussian finite mixture model fitted by EM algorithm 
## ---------------------------------------------------- 
## 
## Mclust EEV (ellipsoidal, equal volume and shape) model with 3 components: 
## 
##  log-likelihood   n  df       BIC       ICL
##       -4415.697 264 426 -11206.75 -11206.77
## 
## Clustering table:
##   1   2   3 
## 142  85  37
mod5 <- Mclust(survey_data_updated_need_vars_scaled, modelNames = "VII", G = 7, x = BIC)
summary(mod5)
## ---------------------------------------------------- 
## Gaussian finite mixture model fitted by EM algorithm 
## ---------------------------------------------------- 
## 
## Mclust VII (spherical, varying volume) model with 7 components: 
## 
##  log-likelihood   n  df       BIC       ICL
##       -4980.999 264 125 -10658.99 -10688.95
## 
## Clustering table:
##   1   2   3   4   5   6   7 
## 127  30  63  11  11  16   6
# 7 groups 
means_7 <- data.frame(mod5$parameters$mean) %>%
  tibble::rownames_to_column() %>%
  rename(Experience = rowname) %>%
  pivot_longer(cols = c(X1, X2, X3, X4, X5, X6, X7), names_to = "Profile", values_to = "Mean") %>%
  mutate(
    Mean = round(Mean, 2),
  )

LPA_plot_7 <- means_7 %>%
  ggplot(aes(Experience, Mean, group = Profile, color = Profile)) +
  geom_point(size = 2.25) +
  geom_line(size = 1.25) +
  # labs(x = NULL, y = "Standardized mean interest") +
  theme_bw(base_size = 14) +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.position = "top"
  )

LPA_plot_7

# 6 groups
means_6 <- data.frame(mod1$parameters$mean) %>%
  tibble::rownames_to_column() %>%
  rename(Experience = rowname) %>%
  pivot_longer(cols = c(X1, X2, X3, X4, X5, X6), names_to = "Profile", values_to = "Mean") %>%
  mutate(
    Mean = round(Mean, 2),
  )

LPA_plot_6 <- means_6 %>%
  ggplot(aes(Experience, Mean, group = Profile, color = Profile)) +
  geom_point(size = 2.25) +
  geom_line(size = 1.25) +
  # labs(x = NULL, y = "Standardized mean interest") +
  theme_bw(base_size = 14) +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.position = "top"
  )

LPA_plot_6

# 5 groups

means_5 <- data.frame(mod2$parameters$mean) %>%
  tibble::rownames_to_column() %>%
  rename(Experience = rowname) %>%
  pivot_longer(cols = c(X1, X2, X3, X4, X5), names_to = "Profile", values_to = "Mean") %>%
  mutate(
    Mean = round(Mean, 2),
  )

LPA_plot_5 <- means_5 %>%
  ggplot(aes(Experience, Mean, group = Profile, color = Profile)) +
  geom_point(size = 2.25) +
  geom_line(size = 1.25) +
  # labs(x = NULL, y = "Standardized mean interest") +
  theme_bw(base_size = 14) +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.position = "top"
  )
LPA_plot_5

# 4 groups

means_4 <- data.frame(mod2$parameters$mean) %>%
  tibble::rownames_to_column() %>%
  rename(Experience = rowname) %>%
  pivot_longer(cols = c(X1, X2, X3, X4), names_to = "Profile", values_to = "Mean") %>%
  mutate(
    Mean = round(Mean, 2),
  )

LPA_plot_4 <- means_4 %>%
  ggplot(aes(Experience, Mean, group = Profile, color = Profile)) +
  geom_point(size = 2.25) +
  geom_line(size = 1.25) +
  # labs(x = NULL, y = "Standardized mean interest") +
  theme_bw(base_size = 14) +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.position = "top"
  )

LPA_plot_4

# 3 groups

means_3 <- data.frame(mod4$parameters$mean) %>%
  tibble::rownames_to_column() %>%
  rename(Experience = rowname) %>%
  pivot_longer(cols = c(X1, X2, X3), names_to = "Profile", values_to = "Mean") %>%
  mutate(
    Mean = round(Mean, 2),
  )

LPA_plot_3 <- means_3 %>%
  ggplot(aes(Experience, Mean, group = Profile, color = Profile)) +
  geom_point(size = 2.25) +
  geom_line(size = 1.25) +
  # labs(x = NULL, y = "Standardized mean interest") +
  theme_bw(base_size = 14) +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.position = "top"
  )

LPA_plot_3