# Analysis script

### Data curation ----
dt_isaac1 = read.csv("dt/isaac-ai_Report.xlsx - Variables.csv")
dt_isaac2 = read.csv("dt/isaac-ai_Report.xlsx - Answers.csv")
dt_isaac3 = subset(read.csv("dt/isaac-ai_Report.xlsx - Variables2.csv"), select = c(EMail,`X_bloodpressure`))

dt_isaac = merge(dt_isaac1, dt_isaac2, by = "Order.id")

dt_shc1 = read.csv("dt/shc-b2c_Report.xlsx - Variables.csv")
dt_shc2 = read.csv("dt/shc-b2c_Report.xlsx - Answers.csv")

dt_shc = merge(dt_shc1, dt_shc2, by = "Order.id")
dt_shc = subset(dt_shc, I.agree.that.data.collected.about.me.during.this.research.can.be.used.to.improve.the.services.offered.by.SleepHubs.Ltd.and.understand.that.said.data.will.be.anonymised.before.being.submitted.for.publication. == "I agree")

### Remove testing samples ----
dt_shc_removed_test = subset(dt_shc, 
                             Your.first.name != "Chaz")
dt_shc_removed_test = subset(dt_shc_removed_test, 
                             Your.first.name != "chaz")
dt_shc_removed_test = subset(dt_shc_removed_test, 
                             Your.first.name != "TEST")
dt_shc_removed_test = subset(dt_shc_removed_test, 
                             Your.first.name != "TEST KATHRYN")
dt_shc_removed_test = subset(dt_shc_removed_test, 
                             Your.first.name != "Kathryn test")
dt_shc_removed_test = subset(dt_shc_removed_test, 
                             Your.first.name != "Kathryn TEST")
dt_shc_removed_test = subset(dt_shc_removed_test, 
                             Your.first.name != "Kathryn NEW TEST")
dt_shc_removed_test = subset(dt_shc_removed_test, 
                             Your.first.name != "test")
dt_shc_removed_test = subset(dt_shc_removed_test, 
                             Your.first.name != "Chaz test")
dt_shc_removed_test = subset(dt_shc_removed_test, 
                             Your.first.name != "Filip")
dt_shc_removed_test = subset(dt_shc_removed_test, 
                             Your.first.name != "ddddd")
dt_shc_removed_test = subset(dt_shc_removed_test, 
                             Your.first.name != "chaz+1")
dt_shc_removed_test = subset(dt_shc_removed_test, 
                             Your.first.name != "Charlie")

### Select columns ----
dt_shc_sel_col = subset(dt_shc_removed_test, select = 
                               c(EMail,
                                 How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep.,
                                 Are.you.happy.with.your.sleep.,
                                 What.are.your.problems..You.can.select.any.combination.of.these.from.the.most.to.least.frequent...1...Biggest.problem.,
                                 Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.,
                                 Has.anyone.ever.complained.about.your.sleep.,
                                 What.are.the.complaints.about..You.can.select.any.combination.of.these.from.the.most.to.least.frequent...1...Biggest.complaint.,
                                 What.other.sleep.problem.,
                                 What.other.complaints.))
dt_shc_sel_col=dt_shc_sel_col[!duplicated(dt_shc_sel_col), ]
dt_shc_sel_col = dt_shc_sel_col[!duplicated(dt_shc_sel_col$EMail),]

dt_isaac_solvedSHC = subset(dt_isaac, SolvedSHC == "1")
# remove columns with all NAs 
dt_isaac_solvedSHC_noNA = dt_isaac_solvedSHC[colSums(!is.na(dt_isaac_solvedSHC)) > 0]
dt_isaac_solvedSHC_noNA_unique = dt_isaac_solvedSHC_noNA[!duplicated(dt_isaac_solvedSHC_noNA$EMail),]

dt_shc_sel_col_addIsaac = merge(dt_shc_sel_col,
                                dt_isaac_solvedSHC_noNA_unique, by = "EMail")

write.csv(dt_isaac_solvedSHC_noNA_unique,"dt_out/dt_isaac_solvedSHC_noNA_unique_merged_dt.csv", row.names = F)

dt_isaac_solvedSHC_noNA_unique_addBP = merge(dt_shc_sel_col_addIsaac,
                                             dt_isaac3[!duplicated(dt_isaac3), ], by = "EMail")
dt_isaac_solvedSHC_noNA_unique_addBP = dt_isaac_solvedSHC_noNA_unique_addBP[!duplicated(dt_isaac_solvedSHC_noNA_unique_addBP$EMail),]

write.csv(dt_isaac_solvedSHC_noNA_unique_addBP,"dt_out/dt_isaac_solvedSHC_noNA_unique_addBP_deduplicated.csv", row.names = F)

isi_vars = c(
             "In.the.last.2.weeks..please.select.how.many.times.it.has.been.difficult.for.you.to.fall.asleep",
             "In.the.last.2.weeks.please.select.how.difficult.it.has.been.for.you.to.stay.asleep",
             "In.the.last.2.weeks.please.select.how.many.times.you.have.woken.too.early",
             "How.SATISFIED.DISSATISFIED.are.you.with.your.CURRENT.sleep.pattern.",
             "To.what.extent.do.your.sleep.problems.INTERFERE.with.your.daily.functioning.e.g..daytime.fatigue..mood..ability.to.function.at.work.daily.chores..concentration..memory..mood..etc.",
             "How.NOTICEABLE.to.others.do.you.think.your.sleep.problem.is.in.terms.of.impairing.the.quality.of.your.life.",
             "How.WORRIED.DISTRESSED.are.you.about.your.current.sleep.problem.")

SHC_vars = c(
             "How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep.",
             "Are.you.happy.with.your.sleep.",
             "Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.",
             "Has.anyone.ever.complained.about.your.sleep.",
             "What.are.your.problems..You.can.select.any.combination.of.these.from.the.most.to.least.frequent...1...Biggest.problem.",
             "What.other.sleep.problem.",
             "What.are.the.complaints.about..You.can.select.any.combination.of.these.from.the.most.to.least.frequent...1...Biggest.complaint.")
SB_vars = c(
  "Do.you.snore.",
  "How.often.do.you.feel.tired.or.fatigued.after.your.sleep.",
  "Has.anyone.ever.noticed.that.you.stop.breathing.during.your.sleep.",
  "Age",
  "Gender",
  "BMI",
  "X_bloodpressure",
  "Is.your.neck.size.shirt.collar.16.inches.41cm.or.larger")

MAPI_vars = c("Loud.snoring",
                "Snorting.or.gasping.for.breath",
                "Your.breathing.stops.or.you.choke.or.struggle",
              "Do.you.snore.",
              "BMI",'Age',"Gender")
# Get dataset for all combined variables

all_vars = c("EMail",isi_vars,SB_vars,SHC_vars,MAPI_vars)
dt_isaac_solvedSHC_noNA_unique_addBP$X_bloodpressure[is.na(dt_isaac_solvedSHC_noNA_unique_addBP$X_bloodpressure)] = 0

all_dt_target_variables = subset(dt_isaac_solvedSHC_noNA_unique_addBP, select = all_vars)

# Create a function: rotate through all mapping variables, find the correct one, then replace

ISI_pattern = c("","Never","Rarely (less than once a week)",
                "Sometimes (1-2 times a week)",
                "Frequently (3-4 times a week)",
                "Always (5-7 times a week)",
                "Very Satisfied","Satisfied",
                "Moderately Satisfied",
                "Dissatisfied","Very Dissatisfied",
                "Not at all Worried","A Little","Somewhat","Very","Extremely",
                "Not at all Noticeable",
                "Not at all Interfering")
MAPI_pattern = c("No","Never", "Don't know",
                 "Rarely (less than once a week)","Yes",
                 "Sometimes (1-2 times a week)",
                 "Frequently (3-4 times a week)",
                 "Always (5-7 times a week)")
SB_pattern = c("Don't know","Never or nearly never",
               "1-2 times per month",
               "1-2 times per week",
               "3-4 times per week",
               "Nearly every day",
               "Nearly every night")

SHC_pattern = c("Always","Regularly","Sometimes","Never")

ISI_pattern_replace = c(NA,0,1,2,3,4,
                        0,1,2,3,4,
                        0,1,2,3,4,
                        0,
                        0)
MAPI_pattern_replace = c(0,0,0,
                         1,1,
                         2,
                         3,
                         4)
SB_pattern_replace = c(0,0,1,2,3,4,4)
SHC_pattern_replace = c(3,2,1,0)

all_test_pattern = c(ISI_pattern,SB_pattern,MAPI_pattern,SHC_pattern)
all_test_replace = c(ISI_pattern_replace,SB_pattern_replace,MAPI_pattern_replace,SHC_pattern_replace)

curate_all_test_vars = Vectorize(function(df_element){
  for (pattern_i in 1:length(all_test_pattern)){
    if (df_element == all_test_pattern[pattern_i]){
      replacement = all_test_replace[pattern_i]
      return(replacement)
    }
    else {replacement = NULL}
  }
  ifelse(is.null(replacement), yes =  return(df_element), no = NA)
})

all_dt_target_variables_curated = all_dt_target_variables
  
all_dt_target_variables_curated[] = sapply(all_dt_target_variables_curated,curate_all_test_vars)

write.csv(all_dt_target_variables_curated,"dt_out/all_dt_target_variables_curated.csv",row.names = F)

#Generate MAPI score 

all_dt_target_variables_curated$index1 = (as.numeric(all_dt_target_variables_curated$Loud.snoring) + 
                                            as.numeric(all_dt_target_variables_curated$Snorting.or.gasping.for.breath) +
                                                         as.numeric(all_dt_target_variables_curated$Your.breathing.stops.or.you.choke.or.struggle))/3

all_dt_target_variables_curated$index1[all_dt_target_variables_curated$Do.you.snore.==0] <- 0

all_dt_target_variables_curated$MAPI = exp(
  -8.16+(1.299 * all_dt_target_variables_curated$index1)+
  (0.163 * as.numeric(all_dt_target_variables_curated$BMI)) - 0.028 *all_dt_target_variables_curated$index1 * as.numeric(all_dt_target_variables_curated$BMI) + 
  0.032 * as.numeric(all_dt_target_variables_curated$Age) + 
    1.278 * as.numeric(all_dt_target_variables_curated$Gender))/(1+exp(-8.16+(1.299 * all_dt_target_variables_curated$index1)+
                                                                         (0.163 * as.numeric(all_dt_target_variables_curated$BMI)) - 
                                                                         0.028 *all_dt_target_variables_curated$index1 * 
                                                                         as.numeric(all_dt_target_variables_curated$BMI) + 
                                                                         0.032 * as.numeric(all_dt_target_variables_curated$Age) + 
                                                                         1.278 * as.numeric(all_dt_target_variables_curated$Gender)))*100
# Generate STOPBANG score
all_dt_target_variables_curated$Has.anyone.ever.noticed.that.you.stop.breathing.during.your.sleep.[is.na(all_dt_target_variables_curated$Has.anyone.ever.noticed.that.you.stop.breathing.during.your.sleep.)] <- 0

# no one replied yes to Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.

all_dt_target_variables_curated$STOPBANG = 
  as.numeric(all_dt_target_variables_curated$Do.you.snore.) + 
  #as.numeric(all_dt_target_variables_curated$Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.) + 
  as.numeric(all_dt_target_variables_curated$Has.anyone.ever.noticed.that.you.stop.breathing.during.your.sleep.) + 
  as.numeric(all_dt_target_variables_curated$X_bloodpressure) + 
  ifelse(as.numeric(all_dt_target_variables_curated$BMI) > 35,1,0) + 
  as.numeric(all_dt_target_variables_curated$Is.your.neck.size.shirt.collar.16.inches.41cm.or.larger) + 
  as.numeric(all_dt_target_variables_curated$Gender)

# Generate ISI score 
all_dt_target_variables_curated$ISI = 
  as.numeric(all_dt_target_variables_curated$How.SATISFIED.DISSATISFIED.are.you.with.your.CURRENT.sleep.pattern.) + 
  as.numeric(all_dt_target_variables_curated$To.what.extent.do.your.sleep.problems.INTERFERE.with.your.daily.functioning.e.g..daytime.fatigue..mood..ability.to.function.at.work.daily.chores..concentration..memory..mood..etc.) + 
  as.numeric(all_dt_target_variables_curated$How.NOTICEABLE.to.others.do.you.think.your.sleep.problem.is.in.terms.of.impairing.the.quality.of.your.life.) + 
  as.numeric(all_dt_target_variables_curated$How.WORRIED.DISTRESSED.are.you.about.your.current.sleep.problem.) + 
  as.numeric(all_dt_target_variables_curated$In.the.last.2.weeks..please.select.how.many.times.it.has.been.difficult.for.you.to.fall.asleep) + 
  as.numeric(all_dt_target_variables_curated$In.the.last.2.weeks.please.select.how.many.times.you.have.woken.too.early) + 
  as.numeric(all_dt_target_variables_curated$In.the.last.2.weeks.please.select.how.difficult.it.has.been.for.you.to.stay.asleep)

write.csv(all_dt_target_variables_curated,"dt_out/all_dt_target_variables_curated_added_tests.csv",row.names = F)

### Make plotting fuction ----

yy_plt_forest_lm = function(lm_model, savePDF = TRUE,saveCSV = TRUE){
  coef_mat = as.data.frame(summary(lm_model)$coefficients)
  coef_mat = na.omit(coef_mat)
  conf = na.omit(confint.default(lm_model, level = 0.95))
  coef_mat$lci = conf[,1]
  coef_mat$uci = conf[,2]
  coef_mat$label = row.names(coef_mat)
  coef_mat = subset(coef_mat, label != "(Intercept)")
  library(ggplot2)
  out_plt = ggplot(data=coef_mat, aes(x=label,y=Estimate, fill = `Pr(>|t|)` < 0.05,
                            color = `Pr(>|t|)` < 0.05)) + 
    geom_errorbar(aes(ymin=lci, ymax=uci),
                  width=0,                    # Width of the error bars
                  position=position_dodge(.9), color = "grey", size = 1.5) +
    geom_point(shape=21, size = 2.5)+
    #geom_pointrange(aes(fill = `Pr(>|t|)` < 0.05)) + 
    geom_hline(yintercept=0, lty=2) +  # add a dotted line at x=1 after flip
    coord_flip() +  # flip coordinates (puts labels on y axis)
    xlab("") + ylab("Effect (95% CI)") +
    scale_fill_manual(values = hcl.colors(18, "ag_Sunset"))+
    scale_color_manual(values = hcl.colors(18, "ag_Sunset"))+
    theme_classic()+
    theme(axis.text.x=element_text(colour="black"),
          axis.text.y=element_text(colour="black")) + 
    theme(text=element_text(family="Arial", size=14))
  out_plt
  
  if(isTRUE(savePDF)) {
    save_name_pdf = paste0("fig/",deparse(substitute(lm_model)),"_figure.pdf")
    ggsave(save_name_pdf,plot = out_plt,device = cairo_pdf, width = 6, height = 4)
  } 
  if(isTRUE(saveCSV)) {
    save_name_csv = paste0("dt_out/",deparse(substitute(lm_model)),"_summary.csv")
    write.csv(coef_mat,save_name_csv)
  } 
}


### Analysis - General scores ----

curated_SB_MAPI_ISI_scaled = subset(all_dt_target_variables_curated,
                                                      select = c(MAPI, STOPBANG, ISI, Age, Gender))
curated_SB_MAPI_ISI_scaled = as.data.frame(scale(dplyr::mutate_all(curated_SB_MAPI_ISI_scaled, function(x) as.numeric(as.character(x)))))

MAPI_ISI_lm = lm(data =  curated_SB_MAPI_ISI_scaled,
                 MAPI ~ ISI+Age+Gender)
summary(MAPI_ISI_lm)
## 
## Call:
## lm(formula = MAPI ~ ISI + Age + Gender, data = curated_SB_MAPI_ISI_scaled)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.6684 -0.4918 -0.2221  0.2599  3.6742 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.002066   0.022371   0.092    0.926    
## ISI         0.119056   0.022740   5.235 1.92e-07 ***
## Age         0.280332   0.023808  11.775  < 2e-16 ***
## Gender      0.467090   0.024008  19.456  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7978 on 1268 degrees of freedom
##   (84 observations deleted due to missingness)
## Multiple R-squared:  0.3713, Adjusted R-squared:  0.3698 
## F-statistic: 249.6 on 3 and 1268 DF,  p-value: < 2.2e-16
yy_plt_forest_lm(MAPI_ISI_lm)


ISI_MAPI_lm = lm(data =  curated_SB_MAPI_ISI_scaled,
                 ISI ~ MAPI+as.numeric(Age)+Gender)
summary(ISI_MAPI_lm)
## 
## Call:
## lm(formula = ISI ~ MAPI + as.numeric(Age) + Gender, data = curated_SB_MAPI_ISI_scaled)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.70267 -0.69807  0.01231  0.71538  2.48375 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      0.0006377  0.0273326   0.023    0.981    
## MAPI             0.1777283  0.0339469   5.235 1.92e-07 ***
## as.numeric(Age) -0.0498340  0.0306054  -1.628    0.104    
## Gender          -0.2564307  0.0326405  -7.856 8.39e-15 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9748 on 1268 degrees of freedom
##   (84 observations deleted due to missingness)
## Multiple R-squared:  0.05205,    Adjusted R-squared:  0.04981 
## F-statistic: 23.21 on 3 and 1268 DF,  p-value: 1.26e-14
yy_plt_forest_lm(ISI_MAPI_lm)


STOPBANG_ISI_lm = lm(data =  curated_SB_MAPI_ISI_scaled,
                 STOPBANG ~ ISI+as.numeric(Age)+Gender)
summary(STOPBANG_ISI_lm)
## 
## Call:
## lm(formula = STOPBANG ~ ISI + as.numeric(Age) + Gender, data = curated_SB_MAPI_ISI_scaled)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.4076 -0.6650 -0.2727  0.2361  4.8802 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      0.30540    0.08484   3.600 0.000336 ***
## ISI              0.12092    0.03312   3.651 0.000277 ***
## as.numeric(Age)  0.16538    0.03648   4.533 6.59e-06 ***
## Gender           0.47875    0.13045   3.670 0.000257 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9728 on 908 degrees of freedom
##   (444 observations deleted due to missingness)
## Multiple R-squared:  0.05426,    Adjusted R-squared:  0.05114 
## F-statistic: 17.37 on 3 and 908 DF,  p-value: 5.71e-11
yy_plt_forest_lm(STOPBANG_ISI_lm)

ISI_STOPBANG_lm = lm(data =  curated_SB_MAPI_ISI_scaled,
                 ISI ~ STOPBANG+as.numeric(Age)+Gender)
summary(ISI_STOPBANG_lm)
## 
## Call:
## lm(formula = ISI ~ STOPBANG + as.numeric(Age) + Gender, data = curated_SB_MAPI_ISI_scaled)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.45252 -0.67110 -0.01415  0.70499  2.55655 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     -0.17761    0.08479  -2.095 0.036469 *  
## STOPBANG         0.11963    0.03277   3.651 0.000277 ***
## as.numeric(Age)  0.03974    0.03667   1.084 0.278750    
## Gender          -0.47348    0.12977  -3.649 0.000279 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9676 on 908 degrees of freedom
##   (444 observations deleted due to missingness)
## Multiple R-squared:  0.0273, Adjusted R-squared:  0.02408 
## F-statistic: 8.494 on 3 and 908 DF,  p-value: 1.44e-05
yy_plt_forest_lm(ISI_STOPBANG_lm)

### Analysis - Sub-levels ----

sublevels_vars = c(MAPI_vars,isi_vars,SB_vars, "Age", "Gender", "ISI","STOPBANG","MAPI")

curated_SB_MAPI_ISI_sublevels_scaled = subset(all_dt_target_variables_curated,
                                    select = sublevels_vars)
curated_SB_MAPI_ISI_sublevels_scaled = as.data.frame(scale(dplyr::mutate_all(curated_SB_MAPI_ISI_sublevels_scaled, function(x) as.numeric(as.character(x)))))

# apnoea variables on insomnia
ISI_MAPISB_all_vars_lm_formula = paste("ISI ~", paste(MAPI_vars,collapse=" + "), " + ",  paste(SB_vars,collapse=" + "))

ISI_MAPISB_all_vars_lm = lm(data =  curated_SB_MAPI_ISI_sublevels_scaled,
                 formula = ISI_MAPISB_all_vars_lm_formula)
summary(ISI_MAPISB_all_vars_lm)
## 
## Call:
## lm(formula = ISI_MAPISB_all_vars_lm_formula, data = curated_SB_MAPI_ISI_sublevels_scaled)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.69352 -0.56156  0.03546  0.51656  2.22449 
## 
## Coefficients: (1 not defined because of singularities)
##                                                                     Estimate
## (Intercept)                                                         0.028980
## Loud.snoring                                                        0.010658
## Snorting.or.gasping.for.breath                                      0.050293
## Your.breathing.stops.or.you.choke.or.struggle                       0.061559
## Do.you.snore.                                                             NA
## BMI                                                                 0.141539
## Age                                                                 0.062302
## Gender                                                             -0.044614
## How.often.do.you.feel.tired.or.fatigued.after.your.sleep.           0.540074
## Has.anyone.ever.noticed.that.you.stop.breathing.during.your.sleep. -0.035540
## X_bloodpressure                                                    -0.005045
## Is.your.neck.size.shirt.collar.16.inches.41cm.or.larger            -0.122339
##                                                                    Std. Error
## (Intercept)                                                          0.098211
## Loud.snoring                                                         0.042365
## Snorting.or.gasping.for.breath                                       0.056377
## Your.breathing.stops.or.you.choke.or.struggle                        0.061368
## Do.you.snore.                                                              NA
## BMI                                                                  0.039627
## Age                                                                  0.049314
## Gender                                                               0.148437
## How.often.do.you.feel.tired.or.fatigued.after.your.sleep.            0.044749
## Has.anyone.ever.noticed.that.you.stop.breathing.during.your.sleep.   0.053823
## X_bloodpressure                                                      0.043167
## Is.your.neck.size.shirt.collar.16.inches.41cm.or.larger              0.039132
##                                                                    t value
## (Intercept)                                                          0.295
## Loud.snoring                                                         0.252
## Snorting.or.gasping.for.breath                                       0.892
## Your.breathing.stops.or.you.choke.or.struggle                        1.003
## Do.you.snore.                                                           NA
## BMI                                                                  3.572
## Age                                                                  1.263
## Gender                                                              -0.301
## How.often.do.you.feel.tired.or.fatigued.after.your.sleep.           12.069
## Has.anyone.ever.noticed.that.you.stop.breathing.during.your.sleep.  -0.660
## X_bloodpressure                                                     -0.117
## Is.your.neck.size.shirt.collar.16.inches.41cm.or.larger             -3.126
##                                                                    Pr(>|t|)    
## (Intercept)                                                        0.768085    
## Loud.snoring                                                       0.801502    
## Snorting.or.gasping.for.breath                                     0.372891    
## Your.breathing.stops.or.you.choke.or.struggle                      0.316413    
## Do.you.snore.                                                            NA    
## BMI                                                                0.000398 ***
## Age                                                                0.207195    
## Gender                                                             0.763910    
## How.often.do.you.feel.tired.or.fatigued.after.your.sleep.           < 2e-16 ***
## Has.anyone.ever.noticed.that.you.stop.breathing.during.your.sleep. 0.509439    
## X_bloodpressure                                                    0.907026    
## Is.your.neck.size.shirt.collar.16.inches.41cm.or.larger            0.001900 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8177 on 397 degrees of freedom
##   (948 observations deleted due to missingness)
## Multiple R-squared:  0.3263, Adjusted R-squared:  0.3093 
## F-statistic: 19.23 on 10 and 397 DF,  p-value: < 2.2e-16
yy_plt_forest_lm(ISI_MAPISB_all_vars_lm)

#insomnia variables on stopbang and mapi

MAPI_ISIall_vars_lm_formula = paste("MAPI ~", paste(isi_vars,collapse=" + "), " + Age + Gender")

MAPI_ISIall_vars_lm = lm(data =  curated_SB_MAPI_ISI_sublevels_scaled,
                            formula = MAPI_ISIall_vars_lm_formula)
summary(MAPI_ISIall_vars_lm)
## 
## Call:
## lm(formula = MAPI_ISIall_vars_lm_formula, data = curated_SB_MAPI_ISI_sublevels_scaled)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.6298 -0.4770 -0.2195  0.2550  3.6577 
## 
## Coefficients:
##                                                                                                                                                                                      Estimate
## (Intercept)                                                                                                                                                                          0.002071
## In.the.last.2.weeks..please.select.how.many.times.it.has.been.difficult.for.you.to.fall.asleep                                                                                      -0.046853
## In.the.last.2.weeks.please.select.how.difficult.it.has.been.for.you.to.stay.asleep                                                                                                  -0.012850
## In.the.last.2.weeks.please.select.how.many.times.you.have.woken.too.early                                                                                                            0.009296
## How.SATISFIED.DISSATISFIED.are.you.with.your.CURRENT.sleep.pattern.                                                                                                                  0.025225
## To.what.extent.do.your.sleep.problems.INTERFERE.with.your.daily.functioning.e.g..daytime.fatigue..mood..ability.to.function.at.work.daily.chores..concentration..memory..mood..etc.  0.027702
## How.NOTICEABLE.to.others.do.you.think.your.sleep.problem.is.in.terms.of.impairing.the.quality.of.your.life.                                                                          0.134136
## How.WORRIED.DISTRESSED.are.you.about.your.current.sleep.problem.                                                                                                                     0.005690
## Age                                                                                                                                                                                  0.292904
## Gender                                                                                                                                                                               0.448448
##                                                                                                                                                                                     Std. Error
## (Intercept)                                                                                                                                                                           0.022236
## In.the.last.2.weeks..please.select.how.many.times.it.has.been.difficult.for.you.to.fall.asleep                                                                                        0.027496
## In.the.last.2.weeks.please.select.how.difficult.it.has.been.for.you.to.stay.asleep                                                                                                    0.031521
## In.the.last.2.weeks.please.select.how.many.times.you.have.woken.too.early                                                                                                             0.028032
## How.SATISFIED.DISSATISFIED.are.you.with.your.CURRENT.sleep.pattern.                                                                                                                   0.036229
## To.what.extent.do.your.sleep.problems.INTERFERE.with.your.daily.functioning.e.g..daytime.fatigue..mood..ability.to.function.at.work.daily.chores..concentration..memory..mood..etc.   0.037208
## How.NOTICEABLE.to.others.do.you.think.your.sleep.problem.is.in.terms.of.impairing.the.quality.of.your.life.                                                                           0.035042
## How.WORRIED.DISTRESSED.are.you.about.your.current.sleep.problem.                                                                                                                      0.040167
## Age                                                                                                                                                                                   0.024691
## Gender                                                                                                                                                                                0.024452
##                                                                                                                                                                                     t value
## (Intercept)                                                                                                                                                                           0.093
## In.the.last.2.weeks..please.select.how.many.times.it.has.been.difficult.for.you.to.fall.asleep                                                                                       -1.704
## In.the.last.2.weeks.please.select.how.difficult.it.has.been.for.you.to.stay.asleep                                                                                                   -0.408
## In.the.last.2.weeks.please.select.how.many.times.you.have.woken.too.early                                                                                                             0.332
## How.SATISFIED.DISSATISFIED.are.you.with.your.CURRENT.sleep.pattern.                                                                                                                   0.696
## To.what.extent.do.your.sleep.problems.INTERFERE.with.your.daily.functioning.e.g..daytime.fatigue..mood..ability.to.function.at.work.daily.chores..concentration..memory..mood..etc.   0.745
## How.NOTICEABLE.to.others.do.you.think.your.sleep.problem.is.in.terms.of.impairing.the.quality.of.your.life.                                                                           3.828
## How.WORRIED.DISTRESSED.are.you.about.your.current.sleep.problem.                                                                                                                      0.142
## Age                                                                                                                                                                                  11.863
## Gender                                                                                                                                                                               18.340
##                                                                                                                                                                                     Pr(>|t|)
## (Intercept)                                                                                                                                                                         0.925805
## In.the.last.2.weeks..please.select.how.many.times.it.has.been.difficult.for.you.to.fall.asleep                                                                                      0.088635
## In.the.last.2.weeks.please.select.how.difficult.it.has.been.for.you.to.stay.asleep                                                                                                  0.683599
## In.the.last.2.weeks.please.select.how.many.times.you.have.woken.too.early                                                                                                           0.740216
## How.SATISFIED.DISSATISFIED.are.you.with.your.CURRENT.sleep.pattern.                                                                                                                 0.486386
## To.what.extent.do.your.sleep.problems.INTERFERE.with.your.daily.functioning.e.g..daytime.fatigue..mood..ability.to.function.at.work.daily.chores..concentration..memory..mood..etc. 0.456695
## How.NOTICEABLE.to.others.do.you.think.your.sleep.problem.is.in.terms.of.impairing.the.quality.of.your.life.                                                                         0.000136
## How.WORRIED.DISTRESSED.are.you.about.your.current.sleep.problem.                                                                                                                    0.887373
## Age                                                                                                                                                                                  < 2e-16
## Gender                                                                                                                                                                               < 2e-16
##                                                                                                                                                                                        
## (Intercept)                                                                                                                                                                            
## In.the.last.2.weeks..please.select.how.many.times.it.has.been.difficult.for.you.to.fall.asleep                                                                                      .  
## In.the.last.2.weeks.please.select.how.difficult.it.has.been.for.you.to.stay.asleep                                                                                                     
## In.the.last.2.weeks.please.select.how.many.times.you.have.woken.too.early                                                                                                              
## How.SATISFIED.DISSATISFIED.are.you.with.your.CURRENT.sleep.pattern.                                                                                                                    
## To.what.extent.do.your.sleep.problems.INTERFERE.with.your.daily.functioning.e.g..daytime.fatigue..mood..ability.to.function.at.work.daily.chores..concentration..memory..mood..etc.    
## How.NOTICEABLE.to.others.do.you.think.your.sleep.problem.is.in.terms.of.impairing.the.quality.of.your.life.                                                                         ***
## How.WORRIED.DISTRESSED.are.you.about.your.current.sleep.problem.                                                                                                                       
## Age                                                                                                                                                                                 ***
## Gender                                                                                                                                                                              ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.793 on 1262 degrees of freedom
##   (84 observations deleted due to missingness)
## Multiple R-squared:  0.3817, Adjusted R-squared:  0.3773 
## F-statistic: 86.58 on 9 and 1262 DF,  p-value: < 2.2e-16
yy_plt_forest_lm(MAPI_ISIall_vars_lm)

ISI_SBall_vars_lm_formula = paste("STOPBANG ~", paste(isi_vars,collapse=" + "), " + Age + Gender")

ISI_SBall_vars_lm = lm(data =  curated_SB_MAPI_ISI_sublevels_scaled,
                            formula = ISI_SBall_vars_lm_formula)
summary(ISI_SBall_vars_lm)
## 
## Call:
## lm(formula = ISI_SBall_vars_lm_formula, data = curated_SB_MAPI_ISI_sublevels_scaled)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.4180 -0.6253 -0.2837  0.3039  4.8514 
## 
## Coefficients:
##                                                                                                                                                                                      Estimate
## (Intercept)                                                                                                                                                                          0.319269
## In.the.last.2.weeks..please.select.how.many.times.it.has.been.difficult.for.you.to.fall.asleep                                                                                       0.005442
## In.the.last.2.weeks.please.select.how.difficult.it.has.been.for.you.to.stay.asleep                                                                                                  -0.059532
## In.the.last.2.weeks.please.select.how.many.times.you.have.woken.too.early                                                                                                           -0.007877
## How.SATISFIED.DISSATISFIED.are.you.with.your.CURRENT.sleep.pattern.                                                                                                                  0.043155
## To.what.extent.do.your.sleep.problems.INTERFERE.with.your.daily.functioning.e.g..daytime.fatigue..mood..ability.to.function.at.work.daily.chores..concentration..memory..mood..etc.  0.041509
## How.NOTICEABLE.to.others.do.you.think.your.sleep.problem.is.in.terms.of.impairing.the.quality.of.your.life.                                                                          0.195312
## How.WORRIED.DISTRESSED.are.you.about.your.current.sleep.problem.                                                                                                                    -0.067450
## Age                                                                                                                                                                                  0.202841
## Gender                                                                                                                                                                               0.478452
##                                                                                                                                                                                     Std. Error
## (Intercept)                                                                                                                                                                           0.084517
## In.the.last.2.weeks..please.select.how.many.times.it.has.been.difficult.for.you.to.fall.asleep                                                                                        0.039581
## In.the.last.2.weeks.please.select.how.difficult.it.has.been.for.you.to.stay.asleep                                                                                                    0.044682
## In.the.last.2.weeks.please.select.how.many.times.you.have.woken.too.early                                                                                                             0.039331
## How.SATISFIED.DISSATISFIED.are.you.with.your.CURRENT.sleep.pattern.                                                                                                                   0.053735
## To.what.extent.do.your.sleep.problems.INTERFERE.with.your.daily.functioning.e.g..daytime.fatigue..mood..ability.to.function.at.work.daily.chores..concentration..memory..mood..etc.   0.052138
## How.NOTICEABLE.to.others.do.you.think.your.sleep.problem.is.in.terms.of.impairing.the.quality.of.your.life.                                                                           0.049872
## How.WORRIED.DISTRESSED.are.you.about.your.current.sleep.problem.                                                                                                                      0.055871
## Age                                                                                                                                                                                   0.037516
## Gender                                                                                                                                                                                0.130204
##                                                                                                                                                                                     t value
## (Intercept)                                                                                                                                                                           3.778
## In.the.last.2.weeks..please.select.how.many.times.it.has.been.difficult.for.you.to.fall.asleep                                                                                        0.137
## In.the.last.2.weeks.please.select.how.difficult.it.has.been.for.you.to.stay.asleep                                                                                                   -1.332
## In.the.last.2.weeks.please.select.how.many.times.you.have.woken.too.early                                                                                                            -0.200
## How.SATISFIED.DISSATISFIED.are.you.with.your.CURRENT.sleep.pattern.                                                                                                                   0.803
## To.what.extent.do.your.sleep.problems.INTERFERE.with.your.daily.functioning.e.g..daytime.fatigue..mood..ability.to.function.at.work.daily.chores..concentration..memory..mood..etc.   0.796
## How.NOTICEABLE.to.others.do.you.think.your.sleep.problem.is.in.terms.of.impairing.the.quality.of.your.life.                                                                           3.916
## How.WORRIED.DISTRESSED.are.you.about.your.current.sleep.problem.                                                                                                                     -1.207
## Age                                                                                                                                                                                   5.407
## Gender                                                                                                                                                                                3.675
##                                                                                                                                                                                     Pr(>|t|)
## (Intercept)                                                                                                                                                                         0.000169
## In.the.last.2.weeks..please.select.how.many.times.it.has.been.difficult.for.you.to.fall.asleep                                                                                      0.890683
## In.the.last.2.weeks.please.select.how.difficult.it.has.been.for.you.to.stay.asleep                                                                                                  0.183083
## In.the.last.2.weeks.please.select.how.many.times.you.have.woken.too.early                                                                                                           0.841321
## How.SATISFIED.DISSATISFIED.are.you.with.your.CURRENT.sleep.pattern.                                                                                                                 0.422131
## To.what.extent.do.your.sleep.problems.INTERFERE.with.your.daily.functioning.e.g..daytime.fatigue..mood..ability.to.function.at.work.daily.chores..concentration..memory..mood..etc. 0.426167
## How.NOTICEABLE.to.others.do.you.think.your.sleep.problem.is.in.terms.of.impairing.the.quality.of.your.life.                                                                         9.67e-05
## How.WORRIED.DISTRESSED.are.you.about.your.current.sleep.problem.                                                                                                                    0.227660
## Age                                                                                                                                                                                 8.22e-08
## Gender                                                                                                                                                                              0.000252
##                                                                                                                                                                                        
## (Intercept)                                                                                                                                                                         ***
## In.the.last.2.weeks..please.select.how.many.times.it.has.been.difficult.for.you.to.fall.asleep                                                                                         
## In.the.last.2.weeks.please.select.how.difficult.it.has.been.for.you.to.stay.asleep                                                                                                     
## In.the.last.2.weeks.please.select.how.many.times.you.have.woken.too.early                                                                                                              
## How.SATISFIED.DISSATISFIED.are.you.with.your.CURRENT.sleep.pattern.                                                                                                                    
## To.what.extent.do.your.sleep.problems.INTERFERE.with.your.daily.functioning.e.g..daytime.fatigue..mood..ability.to.function.at.work.daily.chores..concentration..memory..mood..etc.    
## How.NOTICEABLE.to.others.do.you.think.your.sleep.problem.is.in.terms.of.impairing.the.quality.of.your.life.                                                                         ***
## How.WORRIED.DISTRESSED.are.you.about.your.current.sleep.problem.                                                                                                                       
## Age                                                                                                                                                                                 ***
## Gender                                                                                                                                                                              ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9641 on 902 degrees of freedom
##   (444 observations deleted due to missingness)
## Multiple R-squared:  0.07716,    Adjusted R-squared:  0.06795 
## F-statistic: 8.379 on 9 and 902 DF,  p-value: 4.476e-12
yy_plt_forest_lm(ISI_SBall_vars_lm)

stats::cor.test(curated_SB_MAPI_ISI_sublevels_scaled$STOPBANG,curated_SB_MAPI_ISI_sublevels_scaled$ISI, method = "spearman")
## Warning in cor.test.default(curated_SB_MAPI_ISI_sublevels_scaled$STOPBANG, :
## Cannot compute exact p-value with ties
## 
##  Spearman's rank correlation rho
## 
## data:  curated_SB_MAPI_ISI_sublevels_scaled$STOPBANG and curated_SB_MAPI_ISI_sublevels_scaled$ISI
## S = 112607793, p-value = 0.0009467
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
##       rho 
## 0.1092913
ggplot(curated_SB_MAPI_ISI_sublevels_scaled,aes(x=ISI,y=STOPBANG)) + 
  #geom_point()+
  geom_smooth(method = "lm")+
  theme_classic()
## `geom_smooth()` using formula 'y ~ x'
## Warning: Removed 444 rows containing non-finite values (stat_smooth).

# network plot 

network1_vars = unique(c(MAPI_vars,isi_vars,SB_vars))
network1_vars = network1_vars[network1_vars != "Do.you.snore."]

curated_SB_MAPI_ISI_network_scaled = subset(all_dt_target_variables_curated,
                                              select = network1_vars)
curated_SB_MAPI_ISI_network_scaled = as.data.frame(scale(dplyr::mutate_all(curated_SB_MAPI_ISI_network_scaled, function(x) as.numeric(as.character(x)))))


cor_matrix_network1 = psych::corr.test(na.omit(curated_SB_MAPI_ISI_network_scaled), adjust = "none", method = "spearman")
cor_matrix_network1_diag = cor_matrix_network1$p
cor_matrix_network1_diag[cor_matrix_network1_diag >= 0.05/length(cor_matrix_network1$p)] <- NA
cor_matrix_network1_diag[cor_matrix_network1_diag == 0] <- min(cor_matrix_network1_diag)
cor_matrix_network1_diag = -log2(as.matrix(cor_matrix_network1_diag))

circlize::chordDiagram(cor_matrix_network1_diag, annotationTrack = c("name","grid"), grid.col =hcl.colors(ncol(cor_matrix_network1_diag), "ag_Sunset"),annotationTrackHeight = c(0.04, 0.02))

library(corrr)
network_plot(correlate(curated_SB_MAPI_ISI_network_scaled, method = "spearman"),min_cor = .5)
## Correlation computed with
## • Method: 'spearman'
## • Missing treated using: 'pairwise.complete.obs'

### Analysis of SHC variables to additionally predict ISI and MAPI/STOPBANG ----

SHC_vars_sim = c(
  "How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep.",
  "Are.you.happy.with.your.sleep.",
  "Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.",
  "Has.anyone.ever.complained.about.your.sleep.")

sublevels_SHC_vars = unique(c(MAPI_vars,isi_vars,SB_vars, SHC_vars_sim, "ISI","STOPBANG","MAPI"))

curated_SB_MAPI_ISI_SHC_scaled = subset(all_dt_target_variables_curated,
                                              select = sublevels_SHC_vars)
curated_SB_MAPI_ISI_SHC_scaled = as.data.frame(scale(dplyr::mutate_all(curated_SB_MAPI_ISI_SHC_scaled, function(x) as.numeric(as.character(x)))))

ISI_SHCvars_lm_formula = paste("ISI ~", paste(SHC_vars_sim,collapse=" + "), " + Age + Gender")
SB_SHCvars_lm_formula = paste("STOPBANG ~", paste(SHC_vars_sim,collapse=" + "), " + Age + Gender")
MAPI_SHCvars_lm_formula = paste("MAPI ~", paste(SHC_vars_sim,collapse=" + "), " + Age + Gender")

ISI_SHCvars_lm = lm(data =  curated_SB_MAPI_ISI_SHC_scaled,
                       formula = ISI_SHCvars_lm_formula)
summary(ISI_SHCvars_lm)
## 
## Call:
## lm(formula = ISI_SHCvars_lm_formula, data = curated_SB_MAPI_ISI_SHC_scaled)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.98418 -0.44861 -0.00163  0.44331  3.01731 
## 
## Coefficients:
##                                                                                                                                                                                                         Estimate
## (Intercept)                                                                                                                                                                                             0.003239
## How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep.  0.186845
## Are.you.happy.with.your.sleep.                                                                                                                                                                         -0.573854
## Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.                                                                                                                            0.021476
## Has.anyone.ever.complained.about.your.sleep.                                                                                                                                                            0.126026
## Age                                                                                                                                                                                                    -0.007167
## Gender                                                                                                                                                                                                 -0.056722
##                                                                                                                                                                                                        Std. Error
## (Intercept)                                                                                                                                                                                              0.018972
## How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep.   0.022162
## Are.you.happy.with.your.sleep.                                                                                                                                                                           0.021910
## Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.                                                                                                                             0.020547
## Has.anyone.ever.complained.about.your.sleep.                                                                                                                                                             0.020270
## Age                                                                                                                                                                                                      0.020423
## Gender                                                                                                                                                                                                   0.020720
##                                                                                                                                                                                                        t value
## (Intercept)                                                                                                                                                                                              0.171
## How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep.   8.431
## Are.you.happy.with.your.sleep.                                                                                                                                                                         -26.191
## Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.                                                                                                                             1.045
## Has.anyone.ever.complained.about.your.sleep.                                                                                                                                                             6.217
## Age                                                                                                                                                                                                     -0.351
## Gender                                                                                                                                                                                                  -2.737
##                                                                                                                                                                                                        Pr(>|t|)
## (Intercept)                                                                                                                                                                                             0.86447
## How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep.  < 2e-16
## Are.you.happy.with.your.sleep.                                                                                                                                                                          < 2e-16
## Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.                                                                                                                            0.29613
## Has.anyone.ever.complained.about.your.sleep.                                                                                                                                                           6.85e-10
## Age                                                                                                                                                                                                     0.72568
## Gender                                                                                                                                                                                                  0.00628
##                                                                                                                                                                                                           
## (Intercept)                                                                                                                                                                                               
## How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep. ***
## Are.you.happy.with.your.sleep.                                                                                                                                                                         ***
## Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.                                                                                                                              
## Has.anyone.ever.complained.about.your.sleep.                                                                                                                                                           ***
## Age                                                                                                                                                                                                       
## Gender                                                                                                                                                                                                 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.6765 on 1265 degrees of freedom
##   (84 observations deleted due to missingness)
## Multiple R-squared:  0.5445, Adjusted R-squared:  0.5423 
## F-statistic:   252 on 6 and 1265 DF,  p-value: < 2.2e-16
yy_plt_forest_lm(ISI_SHCvars_lm)

SB_SHCvars_lm = lm(data =  curated_SB_MAPI_ISI_SHC_scaled,
                    formula = SB_SHCvars_lm_formula)
summary(SB_SHCvars_lm)
## 
## Call:
## lm(formula = SB_SHCvars_lm_formula, data = curated_SB_MAPI_ISI_SHC_scaled)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.3222 -0.5195 -0.2315  0.3677  4.7871 
## 
## Coefficients:
##                                                                                                                                                                                                        Estimate
## (Intercept)                                                                                                                                                                                             0.38238
## How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep.  0.09372
## Are.you.happy.with.your.sleep.                                                                                                                                                                          0.05029
## Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.                                                                                                                            0.13162
## Has.anyone.ever.complained.about.your.sleep.                                                                                                                                                            0.33303
## Age                                                                                                                                                                                                     0.20027
## Gender                                                                                                                                                                                                  0.53497
##                                                                                                                                                                                                        Std. Error
## (Intercept)                                                                                                                                                                                               0.07836
## How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep.    0.03415
## Are.you.happy.with.your.sleep.                                                                                                                                                                            0.03447
## Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.                                                                                                                              0.03041
## Has.anyone.ever.complained.about.your.sleep.                                                                                                                                                              0.03095
## Age                                                                                                                                                                                                       0.03287
## Gender                                                                                                                                                                                                    0.12071
##                                                                                                                                                                                                        t value
## (Intercept)                                                                                                                                                                                              4.880
## How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep.   2.744
## Are.you.happy.with.your.sleep.                                                                                                                                                                           1.459
## Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.                                                                                                                             4.328
## Has.anyone.ever.complained.about.your.sleep.                                                                                                                                                            10.760
## Age                                                                                                                                                                                                      6.092
## Gender                                                                                                                                                                                                   4.432
##                                                                                                                                                                                                        Pr(>|t|)
## (Intercept)                                                                                                                                                                                            1.25e-06
## How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep.  0.00618
## Are.you.happy.with.your.sleep.                                                                                                                                                                          0.14491
## Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.                                                                                                                           1.67e-05
## Has.anyone.ever.complained.about.your.sleep.                                                                                                                                                            < 2e-16
## Age                                                                                                                                                                                                    1.62e-09
## Gender                                                                                                                                                                                                 1.04e-05
##                                                                                                                                                                                                           
## (Intercept)                                                                                                                                                                                            ***
## How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep. ** 
## Are.you.happy.with.your.sleep.                                                                                                                                                                            
## Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.                                                                                                                           ***
## Has.anyone.ever.complained.about.your.sleep.                                                                                                                                                           ***
## Age                                                                                                                                                                                                    ***
## Gender                                                                                                                                                                                                 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.897 on 953 degrees of freedom
##   (396 observations deleted due to missingness)
## Multiple R-squared:  0.2005, Adjusted R-squared:  0.1954 
## F-statistic: 39.82 on 6 and 953 DF,  p-value: < 2.2e-16
yy_plt_forest_lm(SB_SHCvars_lm)

MAPI_SHCvars_lm = lm(data =  curated_SB_MAPI_ISI_SHC_scaled,
                    formula = MAPI_SHCvars_lm_formula)
summary(MAPI_SHCvars_lm)
## 
## Call:
## lm(formula = MAPI_SHCvars_lm_formula, data = curated_SB_MAPI_ISI_SHC_scaled)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.0381 -0.4630 -0.1714  0.2457  3.4830 
## 
## Coefficients:
##                                                                                                                                                                                                         Estimate
## (Intercept)                                                                                                                                                                                            0.0007624
## How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep. 0.0851261
## Are.you.happy.with.your.sleep.                                                                                                                                                                         0.0103611
## Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.                                                                                                                           0.1034736
## Has.anyone.ever.complained.about.your.sleep.                                                                                                                                                           0.2019033
## Age                                                                                                                                                                                                    0.2950866
## Gender                                                                                                                                                                                                 0.4322187
##                                                                                                                                                                                                        Std. Error
## (Intercept)                                                                                                                                                                                             0.0206630
## How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep.  0.0242117
## Are.you.happy.with.your.sleep.                                                                                                                                                                          0.0237721
## Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.                                                                                                                            0.0223140
## Has.anyone.ever.complained.about.your.sleep.                                                                                                                                                            0.0220572
## Age                                                                                                                                                                                                     0.0220955
## Gender                                                                                                                                                                                                  0.0226409
##                                                                                                                                                                                                        t value
## (Intercept)                                                                                                                                                                                              0.037
## How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep.   3.516
## Are.you.happy.with.your.sleep.                                                                                                                                                                           0.436
## Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.                                                                                                                             4.637
## Has.anyone.ever.complained.about.your.sleep.                                                                                                                                                             9.154
## Age                                                                                                                                                                                                     13.355
## Gender                                                                                                                                                                                                  19.090
##                                                                                                                                                                                                        Pr(>|t|)
## (Intercept)                                                                                                                                                                                            0.970571
## How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep. 0.000453
## Are.you.happy.with.your.sleep.                                                                                                                                                                         0.663015
## Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.                                                                                                                           3.88e-06
## Has.anyone.ever.complained.about.your.sleep.                                                                                                                                                            < 2e-16
## Age                                                                                                                                                                                                     < 2e-16
## Gender                                                                                                                                                                                                  < 2e-16
##                                                                                                                                                                                                           
## (Intercept)                                                                                                                                                                                               
## How.do.you.feel.during.the.day..Thinking.over.the.past.week..how.tired.have.you.felt.during.the.day..1...extremely.alert..full.of.energy..9...very.sleepy..great.effort.to.stay.awake..fighting.sleep. ***
## Are.you.happy.with.your.sleep.                                                                                                                                                                            
## Do.you..nod.off...doze.or.fall.asleep.during.the.day.without.obvious.reason.                                                                                                                           ***
## Has.anyone.ever.complained.about.your.sleep.                                                                                                                                                           ***
## Age                                                                                                                                                                                                    ***
## Gender                                                                                                                                                                                                 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7544 on 1326 degrees of freedom
##   (23 observations deleted due to missingness)
## Multiple R-squared:  0.4335, Adjusted R-squared:  0.4309 
## F-statistic: 169.1 on 6 and 1326 DF,  p-value: < 2.2e-16
yy_plt_forest_lm(MAPI_SHCvars_lm)