circadian_col<- "brown2"
alertness_col<- "#00BFC4"
efficiency_col<- "#619CFF"
duration_col<- "#FF61CC"
non_insom_col<- "goldenrod"
regularity_col<- "seagreen"
satisf_col<- "mediumpurple1"
  
fact_colors<- c("Circadian Preference"=circadian_col, "Alertness"=alertness_col, "Efficiency"=efficiency_col, "Duration"=duration_col, "Insomnia"=non_insom_col, "Regularity"=regularity_col, "Satisfaction"=satisf_col)

study 1

adapted from sleep_resub.Rmd

### rGS

rgs<- fread("/Users/claire//Documents/IBG Lab/sleep_psychopathology//gsemfinal/figures/fullmod_factor_rGs.csv", header=T)
rgs$CI<- rgs$se*1.96
rgs$Trait<- factor(rgs$Trait, levels = rgs$Trait[4:1])
head(rgs)
##                          Trait               Trait2         se           rG
## 1:               Internalizing Circadian Preference 0.02532728 -0.067158388
## 2:               Externalizing Circadian Preference 0.03184248  0.001522026
## 3:  Psychosis Thought Disorder Circadian Preference 0.02938923 -0.091886082
## 4: Compulsive Thought Disorder Circadian Preference 0.04462180  0.012189672
## 5:               Internalizing           Efficiency 0.03990335 -0.117882786
## 6:               Externalizing           Efficiency 0.04227557 -0.119289908
##            CI
## 1: 0.04964146
## 2: 0.06241126
## 3: 0.05760290
## 4: 0.08745872
## 5: 0.07821056
## 6: 0.08286011
rgs <- rgs %>% mutate(Trait2=fct_relevel(Trait2, c("Circadian Preference", "Non Insomnia", "Regularity", "Alertness", "Efficiency", "Duration")))

rgs$sig<- ifelse(rgs$rG>0, ifelse(rgs$rG-rgs$CI<0, 0, 1), ifelse(rgs$rG<0, ifelse(rgs$rG+rgs$CI>0, 0, 1), NA))

head(rgs)
##                          Trait               Trait2         se           rG
## 1:               Internalizing Circadian Preference 0.02532728 -0.067158388
## 2:               Externalizing Circadian Preference 0.03184248  0.001522026
## 3:  Psychosis Thought Disorder Circadian Preference 0.02938923 -0.091886082
## 4: Compulsive Thought Disorder Circadian Preference 0.04462180  0.012189672
## 5:               Internalizing           Efficiency 0.03990335 -0.117882786
## 6:               Externalizing           Efficiency 0.04227557 -0.119289908
##            CI sig
## 1: 0.04964146   1
## 2: 0.06241126   0
## 3: 0.05760290   1
## 4: 0.08745872   0
## 5: 0.07821056   1
## 6: 0.08286011   1
rgs$rG<- ifelse(rgs$sig==1, rgs$rG, NA)

rgs<- rgs %>% filter(sig==1)

rgs<- rgs %>% rename(Psych=Trait,
                     Sleep=Trait2,
                     beta=rG) %>%
  select(Sleep, Psych, beta, CI) %>%
  mutate(study="LDSC GSEM")


### betas

betas<- fread("/Users/claire//Documents/IBG Lab/sleep_psychopathology//gsemfinal/figures/sleep_psych_betas.csv", header=T)
betas$CI<- betas$se*1.96
betas$Trait<- factor(betas$Trait, levels = betas$Trait[4:1])
head(betas)
##                          Trait               Trait2         se         Beta
## 1:               Internalizing Circadian Preference 0.03479997 -0.065009619
## 2:               Externalizing Circadian Preference 0.04622502  0.045453564
## 3:  Psychosis Thought Disorder Circadian Preference 0.03657704 -0.035939306
## 4: Compulsive Thought Disorder Circadian Preference 0.06543967 -0.033732757
## 5:               Internalizing           Efficiency 0.03963201  0.006214299
## 6:               Externalizing           Efficiency 0.05298536  0.008887780
##            CI
## 1: 0.06820794
## 2: 0.09060105
## 3: 0.07169101
## 4: 0.12826175
## 5: 0.07767874
## 6: 0.10385130
betas <- betas %>% mutate(Trait2=fct_relevel(Trait2, c("Circadian Preference", "Non Insomnia", "Regularity", "Alertness", "Efficiency", "Duration")))

nrow(betas)
## [1] 24
betas$sig<- ifelse(betas$Beta>0, ifelse(betas$Beta-betas$CI<0, 0, 1), ifelse(betas$Beta<0, ifelse(betas$Beta+betas$CI>0, 0, 1), NA))

betas %>% filter(sig==1)
##                           Trait       Trait2         se        Beta         CI
##  1:               Internalizing Non Insomnia 0.04161866 -0.47774924 0.08157257
##  2:               Externalizing Non Insomnia 0.04959695 -0.35059501 0.09721002
##  3:  Psychosis Thought Disorder Non Insomnia 0.04085325 -0.12392923 0.08007236
##  4:               Internalizing    Alertness 0.03509011 -0.12868582 0.06877662
##  5:               Externalizing    Alertness 0.04531730 -0.14717926 0.08882190
##  6:  Psychosis Thought Disorder    Alertness 0.03739053 -0.07527827 0.07328544
##  7: Compulsive Thought Disorder    Alertness 0.06335602  0.15504394 0.12417780
##  8:               Internalizing   Regularity 0.06169999 -0.12219715 0.12093199
##  9:               Externalizing   Regularity 0.09447645 -0.27616041 0.18517384
## 10:  Psychosis Thought Disorder   Regularity 0.08354386 -0.19310154 0.16374596
## 11:               Internalizing     Duration 0.04050410  0.11186614 0.07938803
## 12:               Externalizing     Duration 0.05413053  0.11451422 0.10609583
## 13:  Psychosis Thought Disorder     Duration 0.04765568  0.28237499 0.09340512
##     sig
##  1:   1
##  2:   1
##  3:   1
##  4:   1
##  5:   1
##  6:   1
##  7:   1
##  8:   1
##  9:   1
## 10:   1
## 11:   1
## 12:   1
## 13:   1
betas<- betas %>% rename(Psych=Trait,
                     Sleep=Trait2,
                     beta=Beta) %>%
  select(Sleep, Psych, beta, CI) %>%
  mutate(study="Multiple Regression GSEM")

study 2

adapted from sleep GWAS.Rmd

phen<- read.csv("/Users/claire/Desktop/sleepGWAS/phenotypes_massive.csv", header=T)
nrow(phen)
## [1] 181
head(phen)
##                  phenotype    category
## 1         Accumbens volume       Brain
## 2                     ADHD Psychiatric
## 3       Adopted as a child  Lifecourse
## 4  Age at first live birth  Lifecourse
## 5 Alanine aminotransferase Blood Assay
## 6                  Albumin Blood Assay
### new bonf = 181*3

bonF<- .05/(nrow(phen)*6)

# circ
circ_mass<- fread("/Users/claire/Desktop/sleepGWAS/CTG-VL/MASSIVE/circadian_ldsc.csv", header=T, data.table=F)
circ_mass$p<- as.numeric(circ_mass$p)
## Warning: NAs introduced by coercion
circ_mass<- circ_mass %>% right_join(phen) %>%
  filter(p<bonF) %>%
  mutate(factor="Circadian Preference") %>%
  select(rg, se, p, phenotype, factor, category)
## Joining with `by = join_by(phenotype)`
## Warning in right_join(., phen): Each row in `x` is expected to match at most 1 row in `y`.
## ℹ Row 18 of `x` matches multiple rows.
## ℹ If multiple matches are expected, set `multiple = "all"` to silence this
##   warning.
nrow(circ_mass) # 17 sig associations
## [1] 17
# eff
eff_mass<- fread("/Users/claire/Desktop/sleepGWAS/CTG-VL/MASSIVE/efficiency_ldsc.csv", header=T, data.table=F)
eff_mass$p<- as.numeric(eff_mass$p)
## Warning: NAs introduced by coercion
eff_mass<- eff_mass %>% right_join(phen) %>%
  filter(p<bonF)%>%
  mutate(factor="Efficiency") %>%
  select(rg, se, p, phenotype, factor, category) 
## Joining with `by = join_by(phenotype)`
## Warning in right_join(., phen): Each row in `x` is expected to match at most 1 row in `y`.
## ℹ Row 18 of `x` matches multiple rows.
## ℹ If multiple matches are expected, set `multiple = "all"` to silence this
##   warning.
nrow(eff_mass) # 7 sig associations
## [1] 7
# alert
alert_mass<- fread("/Users/claire/Desktop//sleepGWAS/CTG-VL/MASSIVE/alertness_ldsc.csv", header=T, data.table=F)
alert_mass$p<- as.numeric(alert_mass$p)
## Warning: NAs introduced by coercion
alert_mass<- alert_mass %>% right_join(phen) %>%
  filter(p<bonF) %>%
  mutate(factor="Alertness") %>%
  select(rg, se, p, phenotype, factor, category)
## Joining with `by = join_by(phenotype)`
## Warning in right_join(., phen): Each row in `x` is expected to match at most 1 row in `y`.
## ℹ Row 18 of `x` matches multiple rows.
## ℹ If multiple matches are expected, set `multiple = "all"` to silence this
##   warning.
nrow(alert_mass)# 55 sig associations
## [1] 55
# dur
dur_mass<- fread("/Users/claire/Desktop/sleepGWAS/CTG-VL/MASSIVE/duration_ldsc.csv", header=T, data.table=F)
dur_mass$p<- as.numeric(dur_mass$p)
## Warning: NAs introduced by coercion
dur_mass<- dur_mass %>% right_join(phen) %>%
  filter(p<bonF) %>%
  mutate(factor="Duration") %>%
  select(rg, se, p, phenotype, factor, category) 
## Joining with `by = join_by(phenotype)`
## Warning in right_join(., phen): Each row in `x` is expected to match at most 1 row in `y`.
## ℹ Row 18 of `x` matches multiple rows.
## ℹ If multiple matches are expected, set `multiple = "all"` to silence this
##   warning.
nrow(dur_mass)# 23 sig associations
## [1] 23
# var
var_mass<- fread("/Users/claire/Desktop/sleepGWAS/CTG-VL/MASSIVE/variability_ldsc.csv", header=T, data.table=F)
var_mass$p<- as.numeric(var_mass$p)
## Warning: NAs introduced by coercion
var_mass<- var_mass %>% right_join(phen) %>%
  filter(p<bonF) %>%
  mutate(factor="Regularity") %>%
  select(rg, se, p, phenotype, factor, category) 
## Joining with `by = join_by(phenotype)`
## Warning in right_join(., phen): Each row in `x` is expected to match at most 1 row in `y`.
## ℹ Row 18 of `x` matches multiple rows.
## ℹ If multiple matches are expected, set `multiple = "all"` to silence this
##   warning.
nrow(var_mass)# 18 sig associations
## [1] 18
# insom
insom_mass<- fread("/Users/claire/Desktop//sleepGWAS/CTG-VL/MASSIVE/insomnia_ldsc.csv", header=T, data.table=F)
insom_mass$p<- as.numeric(insom_mass$p)
## Warning: NAs introduced by coercion
insom_mass<- insom_mass %>% right_join(phen) %>%
  filter(p<bonF) %>%
  mutate(factor="Non-Insomnia") %>%
  select(rg, se, p, phenotype, factor, category)
## Joining with `by = join_by(phenotype)`
## Warning in right_join(., phen): Each row in `x` is expected to match at most 1 row in `y`.
## ℹ Row 18 of `x` matches multiple rows.
## ℹ If multiple matches are expected, set `multiple = "all"` to silence this
##   warning.
nrow(insom_mass)# 68 sig associations
## [1] 68
# was only plotting phenos with more than 1 sig factor but let's just plot all sig


# prob want to write this and edit names
mass<- rbind(circ_mass, eff_mass, alert_mass, dur_mass, insom_mass, var_mass)

mass<- mass %>% filter(p<0.05)

## the only ones that are really way too long are in psychiatric

# maybe get rid of "Non-cancer illness code, " and "Mental health problems ever diagnosed by a professional: "


mass$phenotype[mass$phenotype=="Activities undertaken to treat anxiety: Talking therapies, such as psychotherapy, counselling, group therapy or CBT"]<- "Talking therapy to treat depression"
mass$phenotype[mass$phenotype=="Activities undertaken to treat anxiety: Talking therapies, such as psychotherapy, counselling, group therapy or CBT"]<- "Talking therapy to treat anxiety"
mass$phenotype[mass$phenotype=="Activities undertaken to treat depression: Talking therapies, such as psychotherapy, counselling, group therapy or CBT"]<- "Talking therapy to treat depression"
mass$phenotype[mass$phenotype=="Activities undertaken to treat depression: Talking therapies, such as psychotherapy, counselling, group therapy or CBT"]<- "Talking therapy to treat depression"
mass$phenotype[mass$phenotype=="Seen doctor (GP) for nerves, anxiety, tension or depression"]<- "Seen doctor (GP) for anxiety or depression"
mass$phenotype[mass$phenotype=="Seen a psychiatrist for nerves, anxiety, tension or depression"]<- "Seen psychiatrist for anxiety or depression"

table(mass$category)
## 
##       Activity Anthropometric    Blood Assay      Education  Environmental 
##             20             21             27              8              6 
##         Health     Lifecourse    Psychiatric          Sleep  Substance Use 
##             17             11             55              5             18
# mass<- mass %>% filter(category=="Psychiatric")


mass$factor <- factor(mass$factor, levels = c("Circadian Preference", "Alertness", "Efficiency", "Duration", "Non-Insomnia", "Regularity"))


mass$rg<- as.numeric(mass$rg)
mass$se<- as.numeric(mass$se)
mass$CI<- mass$se*(1.96)


head(mass)
##        rg      se         p
## 1 -0.1061 0.02536 2.849e-05
## 2 -0.1277 0.02034 3.443e-10
## 3 -0.2464 0.04533 5.502e-08
## 4 -0.2330 0.03030 1.489e-14
## 5 -0.1158 0.02429 1.891e-06
## 6 -0.1737 0.03086 1.813e-08
##                                                   phenotype
## 1                 Townsend deprivation index at recruitment
## 2                                  Fluid intelligence score
## 3 Particulate matter air pollution (pm2.5) absorbance; 2010
## 4                      Nitrogen dioxide air pollution; 2007
## 5                                   Age at first live birth
## 6                           Alcohol drinker status: Current
##                 factor      category        CI
## 1 Circadian Preference    Lifecourse 0.0497056
## 2 Circadian Preference     Education 0.0398664
## 3 Circadian Preference Environmental 0.0888468
## 4 Circadian Preference Environmental 0.0593880
## 5 Circadian Preference    Lifecourse 0.0476084
## 6 Circadian Preference Substance Use 0.0604856
mass<- mass %>% rename(Sleep=factor,
                       Psych=phenotype,
                       beta=rg) %>%
  select(Sleep, Psych, beta, CI) %>%
  mutate(study="Batch LDSC")

study 3

adapted from CTC_MR_plots.Rmd

### co twin control

results<- fread("/Users/claire/Desktop/dissertation/cotwin_mendelian/ctc_results.csv", header=T, data.table=F)

results<- results %>% filter(beta_p<0.05)

### for causal plot
within<- results %>% filter(!model=="Between" & !model=="Phenotypic")
head(within)
##   sleep_trait_specific      sleep   psychiatric psychiatric_trait_specific
## 1     Weekday Duration   Duration     Psychosis                         NA
## 2             Insomnia   Insomnia Internalizing                         NA
## 3        Social Jetlag Chronotype Internalizing                         NA
## 4             Insomnia   Insomnia Externalizing                         NA
## 5             Insomnia   Insomnia Internalizing                         NA
## 6           Chronotype Chronotype Internalizing                         NA
##        beta   beta_se  beta_p      model   sample n_SNPs
## 1 -0.158859 0.0623126 0.01100  Within MZ     ABCD     NA
## 2  0.120000 0.0484300 0.01200     Within Colorado     NA
## 3  0.445940 0.1845900 0.01599 Within sib     ABCD     NA
## 4  0.170000 0.0706800 0.01900  Within MZ Colorado     NA
## 5  0.170000 0.0721920 0.01600  Within MZ Colorado     NA
## 6 -0.020000 0.0084080 0.04000  Within DZ Colorado     NA
within$CI<- within$beta_se*1.96

within<- within %>% rename(Sleep=sleep_trait_specific,
                             Psych=psychiatric) %>%
  select(Sleep, Psych, beta, CI) %>%
  mutate(study="Co-twin")

plot just pheno associations

circadian_col<- "brown2"
alertness_col<- "#00BFC4"
efficiency_col<- "#619CFF"
duration_col<- "#FF61CC"
non_insom_col<- "goldenrod"
regularity_col<- "seagreen"
satisf_col<- "mediumpurple1"
  
fact_colors<- c("Chronotype"=circadian_col, "Alertness"=alertness_col, "Efficiency"=efficiency_col, "Duration"=duration_col, "Insomnia"=non_insom_col, "Variability"=regularity_col, "Satisfaction"=satisf_col)

head(results)
##   sleep_trait_specific        sleep   psychiatric psychiatric_trait_specific
## 1             Insomnia     Insomnia Internalizing                         NA
## 2             Insomnia     Insomnia Internalizing                         NA
## 3         Satisfaction Satisfaction Internalizing                         NA
## 4     Weekday Duration     Duration     Psychosis                         NA
## 5         Satisfaction Satisfaction Internalizing                         NA
## 6         Satisfaction Satisfaction Externalizing                         NA
##        beta   beta_se   beta_p      model   sample n_SNPs
## 1  0.150000 0.0288800 1.20e-07 Phenotypic Colorado     NA
## 2  0.180000 0.0378030 4.40e-06    Between Colorado     NA
## 3 -0.170000 0.0431400 6.55e-05    Between Colorado     NA
## 4 -0.158859 0.0623126 1.10e-02  Within MZ     ABCD     NA
## 5 -0.140000 0.0342400 6.99e-05 Phenotypic Colorado     NA
## 6 -0.090000 0.0331700 1.00e-02 Phenotypic Colorado     NA
pheno<- results %>% filter(model=="Phenotypic") %>%
  filter(beta_p<0.05) 

pheno$CI<- pheno$beta_se*1.96

pheno_plt<- list()

for (i in unique(pheno$psychiatric)) {
  dat<- pheno %>% filter(psychiatric==i)
  p<- ggplot(dat, aes(y=sleep_trait_specific,x=beta, colour=sleep, xmin = beta-CI, xmax = beta+CI)) +
  geom_point(size=5,position=position_dodge(width=0.5)) + 
   geom_errorbarh(aes(x = CI), height=.4, position=position_dodge(width=0.5))+
  xlab("Beta") +
  ylab("Psychiatric Trait") + 
    ggtitle(i)+
  scale_y_discrete(labels = function(x) str_wrap(x, width = 55))+
  geom_vline(xintercept = 0, linetype="dashed")+
  theme(legend.title = element_blank(),
        panel.border = element_blank(),
      panel.grid.major = element_blank(),
         legend.position="bottom",
      panel.grid.minor = element_blank(),
      plot.title = element_text(hjust = 0.5))+
     scale_color_manual(values = fact_colors) 
  # guides(shape=F)
    pheno_plt[[i]]<- p
}
## Warning in geom_errorbarh(aes(x = CI), height = 0.4, position = position_dodge(width = 0.5)): Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
pheno_plt
## $Internalizing

## 
## $Externalizing

## 
## $`Attention Problems`

## 
## $Psychosis

head(results)
##   sleep_trait_specific        sleep   psychiatric psychiatric_trait_specific
## 1             Insomnia     Insomnia Internalizing                         NA
## 2             Insomnia     Insomnia Internalizing                         NA
## 3         Satisfaction Satisfaction Internalizing                         NA
## 4     Weekday Duration     Duration     Psychosis                         NA
## 5         Satisfaction Satisfaction Internalizing                         NA
## 6         Satisfaction Satisfaction Externalizing                         NA
##        beta   beta_se   beta_p      model   sample n_SNPs
## 1  0.150000 0.0288800 1.20e-07 Phenotypic Colorado     NA
## 2  0.180000 0.0378030 4.40e-06    Between Colorado     NA
## 3 -0.170000 0.0431400 6.55e-05    Between Colorado     NA
## 4 -0.158859 0.0623126 1.10e-02  Within MZ     ABCD     NA
## 5 -0.140000 0.0342400 6.99e-05 Phenotypic Colorado     NA
## 6 -0.090000 0.0331700 1.00e-02 Phenotypic Colorado     NA
results$CI<- results$beta_se*1.96

results<- results %>% rename(Sleep=sleep_trait_specific,
                             Psych=psychiatric) %>%
  select(Sleep, Psych, beta, CI) %>%
  mutate(study="Co-twin")


results$sig<- ifelse(results$beta>0, ifelse(results$beta-results$CI<0, 0, 1), ifelse(results$beta<0, ifelse(results$beta+results$CI>0, 0, 1), NA))

results<- results %>% filter(sig==1)

results$sig<- NULL

### MR 

MR_out<- fread("/Users/claire/Desktop/dissertation/cotwin_mendelian/sleepMR/sleep_exposure_MR.csv", header=T, data.table=F)


head(MR_out)
##   id.exposure id.outcome                          outcome
## 1      lVP89E  ieu-b-102 Major depression || id:ieu-b-102
## 2      lVP89E  ieu-b-102 Major depression || id:ieu-b-102
## 3      lVP89E  ieu-b-102 Major depression || id:ieu-b-102
## 4      lVP89E  ieu-b-102 Major depression || id:ieu-b-102
## 5      lVP89E  ieu-b-102 Major depression || id:ieu-b-102
## 6      lVP89E   ieu-b-42     schizophrenia || id:ieu-b-42
##                         exposure                    method nsnp            b
## 1 Sleep duration (unit increase)        Maximum likelihood   44 -0.003790686
## 2 Sleep duration (unit increase)                  MR Egger   44 -0.003997578
## 3 Sleep duration (unit increase)             Weighted mode   44 -0.002878508
## 4 Sleep duration (unit increase)           Weighted median   44 -0.003027430
## 5 Sleep duration (unit increase) Inverse variance weighted   44 -0.003700711
## 6 Sleep duration (unit increase)        Maximum likelihood   44  0.006757675
##             se         pval        lo_ci         up_ci        or  or_lci95
## 1 0.0008569348 9.709624e-06 -0.005470278 -0.0021110938 0.9962165 0.9945447
## 2 0.0043827653 3.669151e-01 -0.012587798  0.0045926424 0.9960104 0.9874911
## 3 0.0020044580 1.582228e-01 -0.006807246  0.0010502292 0.9971256 0.9932159
## 4 0.0013214918 2.196815e-02 -0.005617554 -0.0004373057 0.9969771 0.9943982
## 5 0.0013220044 5.121062e-03 -0.006291840 -0.0011095826 0.9963061 0.9937279
## 6 0.0021850579 1.983609e-03  0.002474961  0.0110403886 1.0067806 1.0024780
##    or_uci95
## 1 0.9978911
## 2 1.0046032
## 3 1.0010508
## 4 0.9995628
## 5 0.9988910
## 6 1.0111016
table(MR_out$outcome)
## 
##            ADHD || id:ieu-a-1183             Anti Social Behavior 
##                               55                               50 
##                          Anxiety                 Bipolar Disorder 
##                               55                               55 
##               Cigarettes per Day                  Drinks per Week 
##                               55                               55 
## Major depression || id:ieu-b-102     schizophrenia || id:ieu-b-42 
##                               55                               55
table(MR_out$exposure)
## 
##        Accelerometer sleep duration                    Alertness Factor 
##                                  40                                  40 
##                          Chronotype         Circadian Preference Factor 
##                                  40                                  40 
## Daytime alertness (sleepiness GWAS)                          Efficiency 
##                                  40                                  35 
##                   Efficiency Factor           No napping (napping GWAS) 
##                                  40                                  40 
##                        Non-Insomnia      Sleep duration (unit increase) 
##                                  40                                  40 
##                      Sleep Episodes 
##                                  40
table(MR_out$method)
## 
## Inverse variance weighted        Maximum likelihood                  MR Egger 
##                        87                        87                        87 
##           Weighted median             Weighted mode 
##                        87                        87
MR_out<- MR_out %>% filter(!method=="Outlier-corrected")

MR_out$exposure[MR_out$exposure=="Daytime nap || id:ebi-a-GCST011494"] <- "Napping"
MR_out$exposure[MR_out$exposure=="Sleep duration (unit increase)"] <- "Self-reported sleep duration"
MR_out$outcome[MR_out$outcome=="Efficiency"] <- "Accelerometer Efficiency"


MR_out$outcome[MR_out$outcome=="ADHD || id:ieu-a-1183"] <- "ADHD"
MR_out$outcome[MR_out$outcome=="schizophrenia || id:ieu-b-42"] <- "Schizophrenia"
MR_out$outcome[MR_out$outcome=="bipolar disorder"] <- "Bipolar Disorder"
MR_out$outcome[MR_out$outcome=="cigarettes per day"] <- "Cigarettes per Day"
MR_out$outcome[MR_out$outcome=="Major depression || id:ieu-b-102"] <- "Depression"

MR_out$id.exposure<- NULL
MR_out$id.outcome<- NULL
MR_out$lo_ci<- NULL
MR_out$up_ci<- NULL

colnames(MR_out)[1:10]<- c("psychiatric_trait_specific", "sleep_trait_specific", "model", "n_SNPs", "beta", "beta_se", "beta_p", "OR", "OR_lo", "OR_hi")


MR_out$sample<- "MR"

MR_out$sleep[MR_out$sleep_trait_specific=="Accelerometer sleep duration" | MR_out$sleep_trait_specific=="Self-reported sleep duration"] <- "Duration"

MR_out$sleep[MR_out$sleep_trait_specific=="No napping (napping GWAS)" | MR_out$sleep_trait_specific=="Alertness Factor" | MR_out$sleep_trait_specific=="Daytime alertness (sleepiness GWAS)"] <- "Alertness"

MR_out$sleep[MR_out$sleep_trait_specific=="Chronotype (unit decrease)" | MR_out$sleep_trait_specific=="Chronotype (unit increase)" | MR_out$sleep_trait_specific=="Circadian Preference Factor"] <- "Chronotype"

MR_out$sleep[MR_out$sleep_trait_specific=="Efficiency" | MR_out$sleep_trait_specific=="Efficiency Factor"] <- "Efficiency"

MR_out$psychiatric[MR_out$psychiatric_trait_specific=="Depression" | MR_out$psychiatric_trait_specific=="Anxiety"] <- "Internalizing"

MR_out$psychiatric[MR_out$psychiatric_trait_specific=="Schizophrenia" | MR_out$psychiatric_trait_specific=="Bipolar Disorder"] <- "Psychosis"

MR_out$psychiatric[MR_out$psychiatric_trait_specific=="ADHD"] <- "Attention Problems"

MR_out$psychiatric[MR_out$psychiatric_trait_specific=="Anti Social Behavior" | MR_out$psychiatric_trait_specific=="Cigarettes per Day" | MR_out$psychiatric_trait_specific=="Drinks per Week"] <- "Externalizing"



head(MR_out)
##   psychiatric_trait_specific         sleep_trait_specific
## 1                 Depression Self-reported sleep duration
## 2                 Depression Self-reported sleep duration
## 3                 Depression Self-reported sleep duration
## 4                 Depression Self-reported sleep duration
## 5                 Depression Self-reported sleep duration
## 6              Schizophrenia Self-reported sleep duration
##                       model n_SNPs         beta      beta_se       beta_p
## 1        Maximum likelihood     44 -0.003790686 0.0008569348 9.709624e-06
## 2                  MR Egger     44 -0.003997578 0.0043827653 3.669151e-01
## 3             Weighted mode     44 -0.002878508 0.0020044580 1.582228e-01
## 4           Weighted median     44 -0.003027430 0.0013214918 2.196815e-02
## 5 Inverse variance weighted     44 -0.003700711 0.0013220044 5.121062e-03
## 6        Maximum likelihood     44  0.006757675 0.0021850579 1.983609e-03
##          OR     OR_lo     OR_hi sample    sleep   psychiatric
## 1 0.9962165 0.9945447 0.9978911     MR Duration Internalizing
## 2 0.9960104 0.9874911 1.0046032     MR Duration Internalizing
## 3 0.9971256 0.9932159 1.0010508     MR Duration Internalizing
## 4 0.9969771 0.9943982 0.9995628     MR Duration Internalizing
## 5 0.9963061 0.9937279 0.9988910     MR Duration Internalizing
## 6 1.0067806 1.0024780 1.0111016     MR Duration     Psychosis
MR_out$CI<- MR_out$beta_se*1.96

MR_out<- MR_out %>% rename(Sleep=sleep_trait_specific,
                             Psych=psychiatric_trait_specific) %>%
  select(Sleep, Psych, beta, CI) %>%
  mutate(study="MR")

MR_out$sig<- ifelse(MR_out$beta>0, ifelse(MR_out$beta-MR_out$CI<0, 0, 1), ifelse(MR_out$beta<0, ifelse(MR_out$beta+MR_out$CI>0, 0, 1), NA))

MR_out<- MR_out %>% filter(sig==1)

MR_out$sig<- NULL

all studies TOTAL ASSOC

all<- rbind(rgs, betas, mass, results, MR_out)
head(all)
##                   Sleep                      Psych        beta         CI
## 1: Circadian Preference              Internalizing -0.06715839 0.04964146
## 2: Circadian Preference Psychosis Thought Disorder -0.09188608 0.05760290
## 3:           Efficiency              Internalizing -0.11788279 0.07821056
## 4:           Efficiency              Externalizing -0.11928991 0.08286011
## 5:         Non Insomnia              Internalizing -0.24399952 0.10085731
## 6:         Non Insomnia              Externalizing -0.39406871 0.05702922
##        study
## 1: LDSC GSEM
## 2: LDSC GSEM
## 3: LDSC GSEM
## 4: LDSC GSEM
## 5: LDSC GSEM
## 6: LDSC GSEM
all$Sleep<- as.character(all$Sleep)
all$Psych<- as.character(all$Psych)


## re categorize sleep

all$Sleep[all$Sleep=="Chronotype" | all$Sleep=="Social Jetlag" | all$Sleep=="Chronotype (unit decrease)" |  all$Sleep=="Chronotype (unit increase)" | all$Sleep=="Circadian Preference Factor"]<- "Circadian Preference"

all$Sleep[all$Sleep=="Alertness Factor" | all$Sleep=="No napping (napping GWAS)" | all$Sleep=="Daytime alertness (sleepiness GWAS)"]<- "Alertness"

all$Sleep[all$Sleep=="Accelerometer Weekend Efficiency" | all$Sleep=="Efficiency Factor" | all$Sleep=="Accelerometer Weekday Efficiency" | all$Sleep=="Sleep Episodes"]<- "Efficiency"

all$Sleep[all$Sleep=="Weekend Duration" | all$Sleep=="Accelerometer Weekday Duration" | all$Sleep=="Self-reported sleep duration" |  all$Sleep=="Weekday Duration" | all$Sleep=="Accelerometer sleep duration" | all$Sleep=="Accelerometer Weekend Duration"]<- "Duration"

all$Sleep[all$Sleep=="Variability"]<- "Regularity"

all$Sleep[all$Sleep=="Non-Insomnia" | all$Sleep=="Non Insomnia"]<- "Insomnia"

## re categorize psych

# int
all$Psych_cat[all$Psych=="Worrier / anxious feelings"]<- "Internalizing"
all$Psych_cat[all$Psych=="Tense / 'highly strung'"]<- "Internalizing"
all$Psych_cat[all$Psych=="Sensitivity / hurt feelings"]<- "Internalizing"
all$Psych_cat[all$Psych=="Seen psychiatrist for anxiety or depression"]<- "Internalizing"
all$Psych_cat[all$Psych=="Seen doctor (GP) for anxiety or depression"]<- "Internalizing"
all$Psych_cat[all$Psych=="Non-cancer illness code, self-reported: depression"]<- "Internalizing"
all$Psych_cat[all$Psych=="Non-cancer illness code, self-reported: anxiety/panic attacks"]<- "Internalizing"
all$Psych_cat[all$Psych=="Neuroticism score"]<- "Internalizing"
all$Psych_cat[all$Psych=="Nervous feelings"]<- "Internalizing"
all$Psych_cat[all$Psych=="Mood swings"]<- "Internalizing"
all$Psych_cat[all$Psych=="Miserableness"]<- "Internalizing"
all$Psych_cat[all$Psych=="Mental health problems ever diagnosed by a professional: Panic attacks"]<- "Internalizing"
all$Psych_cat[all$Psych=="Mental health problems ever diagnosed by a professional: Depression"]<- "Internalizing"
all$Psych_cat[all$Psych=="Mental health problems ever diagnosed by a professional: Anxiety, nerves or generalized anxiety disorder"]<- "Internalizing"
all$Psych_cat[all$Psych=="Major Depresive Disorder"]<- "Internalizing"
all$Psych_cat[all$Psych=="Loneliness, isolation"]<- "Internalizing"
all$Psych_cat[all$Psych=="Irritability"]<- "Internalizing"
all$Psych_cat[all$Psych=="Internalizing"]<- "Internalizing"
all$Psych_cat[all$Psych=="Guilty feelings"]<- "Internalizing"
all$Psych_cat[all$Psych=="Fed-up feelings"]<- "Internalizing"
all$Psych_cat[all$Psych=="Depression"]<- "Internalizing"
all$Psych_cat[all$Psych=="Anxiety"]<- "Internalizing"

# ext
all$Psych_cat[all$Psych=="Anti Social Behavior"]<- "Externalizing"
all$Psych_cat[all$Psych=="Cigarettes per Day"]<- "Externalizing"
all$Psych_cat[all$Psych=="Drinks per Week"]<- "Externalizing"
all$Psych_cat[all$Psych=="Externalizing"]<- "Externalizing"
all$Psych_cat[all$Psych=="Externalizing"]<- "Externalizing"

# att

all$Psych_cat[all$Psych=="ADHD"]<- "Attention Problems"
all$Psych_cat[all$Psych=="Attention Problems"]<- "Attention Problems"

# psych

all$Psych_cat[all$Psych=="Schizophrenia"]<- "Psychosis"
all$Psych_cat[all$Psych=="Psychosis Thought Disorder"]<- "Psychosis"
all$Psych_cat[all$Psych=="Bipolar Disorder"]<- "Psychosis"
all$Psych_cat[all$Psych=="Psychosis"]<- "Psychosis"

# compulsive

all$Psych_cat[all$Psych=="Compulsive Thought Disorder"]<- "Compulsive"

# non psych

all$Psych_cat[all$Psych=="Age at first live birth"]<- "Lifecourse"
all$Psych_cat[all$Psych=="Alanine aminotransferase"]<- "Blood Assay"
all$Psych_cat[all$Psych=="Alcohol drinker status: Current"]<- "Externalizing"
all$Psych_cat[all$Psych=="Alcohol drinker status: Previous"]<- "Externalizing"
all$Psych_cat[all$Psych=="Apolipoprotein A"]<- "Cardiovascular"
all$Psych_cat[all$Psych=="Basal metabolic rate"]<- "Cardiovascular"
all$Psych_cat[all$Psych=="Birth weight"]<- "Anthropometric"
all$Psych_cat[all$Psych=="Body mass index (BMI)"]<- "Anthropometric"
all$Psych_cat[all$Psych=="C-reactive protein"]<- "Blood Assay"
all$Psych_cat[all$Psych=="Cystatin C"]<- "Blood Assay"
all$Psych_cat[all$Psych=="Educational Attainment"]<- "Education"
all$Psych_cat[all$Psych=="Ever smoked"]<- "Externalizing"
all$Psych_cat[all$Psych=="Fluid intelligence score"]<- "Education"
all$Psych_cat[all$Psych=="Gamma glutamyltransferase"]<- "Blood Assay"
all$Psych_cat[all$Psych=="Glucose"]<- "Blood Assay"
all$Psych_cat[all$Psych=="Glycated haemoglobin (HbA1c)"]<- "Blood Assay"
all$Psych_cat[all$Psych=="HDL cholesterol"]<- "Cardiovascular"
all$Psych_cat[all$Psych=="Leisure/social activities: Adult education class"]<- "Activity"
all$Psych_cat[all$Psych=="Leisure/social activities: None of the above"]<- "Activity"
all$Psych_cat[all$Psych=="Leisure/social activities: Pub or social club"]<- "Activity"
all$Psych_cat[all$Psych=="Leisure/social activities: Religious group"]<- "Activity"
all$Psych_cat[all$Psych=="Leisure/social activities: Sports club or gym"]<- "Activity"
all$Psych_cat[all$Psych=="Maternal smoking around birth"]<- "Environment"
all$Psych_cat[all$Psych=="Nitrogen dioxide air pollution; 2007"]<- "Environment"
all$Psych_cat[all$Psych=="Non-cancer illness code, self-reported: asthma"]<- "Health"
all$Psych_cat[all$Psych=="Non-cancer illness code, self-reported: back problem"]<- "Health"
all$Psych_cat[all$Psych=="Non-cancer illness code, self-reported: diabetes"]<- "Health"
all$Psych_cat[all$Psych=="Non-cancer illness code, self-reported: diverticular disease/diverticulitis"]<- "Health"
all$Psych_cat[all$Psych=="Non-cancer illness code, self-reported: emphysema/chronic bronchitis"]<- "Health"
all$Psych_cat[all$Psych=="Non-cancer illness code, self-reported: gastro-oesophageal reflux (gord) / gastric reflux"]<- "Health"
all$Psych_cat[all$Psych=="Non-cancer illness code, self-reported: heart attack/myocardial infarction"]<- "Cardiovascular"
all$Psych_cat[all$Psych=="Non-cancer illness code, self-reported: hiatus hernia"]<- "Health"
all$Psych_cat[all$Psych=="Non-cancer illness code, self-reported: high cholesterol"]<- "Cardiovascular"
all$Psych_cat[all$Psych=="Non-cancer illness code, self-reported: hypertension"]<- "Cardiovascular"
all$Psych_cat[all$Psych=="Non-cancer illness code, self-reported: irritable bowel syndrom"]<- "Health"
all$Psych_cat[all$Psych=="Non-cancer illness code, self-reported: osteoarthritis"]<- "Health"
all$Psych_cat[all$Psych=="Particulate matter air pollution (pm2.5) absorbance; 2010"]<- "Environment"
all$Psych_cat[all$Psych=="Particulate matter air pollution (pm2.5); 2010"]<- "Environment"
all$Psych_cat[all$Psych=="Particulate matter air pollution (pm2.5); 2010"]<- "Environment"
all$Psych_cat[all$Psych=="Smoking status: Current"]<- "Externalizing"
all$Psych_cat[all$Psych=="Smoking status: Never"]<- "Externalizing"
all$Psych_cat[all$Psych=="Smoking status: Previous"]<- "Externalizing"
all$Psych_cat[all$Psych=="Total bilirubin"]<- "Blood Assay"
all$Psych_cat[all$Psych=="Total protein"]<- "Blood Assay"
all$Psych_cat[all$Psych=="Townsend deprivation index at recruitment"]<- "Environment"
all$Psych_cat[all$Psych=="Triglycerides"]<- "Cardiovascular"
all$Psych_cat[all$Psych=="Types of physical activity in last 4 weeks: Heavy DIY (eg: weeding, lawn mowing, carpentry, digging)"]<- "Activity"
all$Psych_cat[all$Psych=="Types of physical activity in last 4 weeks: Light DIY (eg: pruning, watering the lawn)"]<- "Activity"
all$Psych_cat[all$Psych=="Types of physical activity in last 4 weeks: None of the above"]<- "Activity"
all$Psych_cat[all$Psych=="Types of physical activity in last 4 weeks: Strenuous sports"]<- "Activity"
all$Psych_cat[all$Psych=="Types of physical activity in last 4 weeks: Walking for pleasure (not as a means of transport)"]<- "Activity"
all$Psych_cat[all$Psych=="Urate"]<- "Blood Assay"
all$Psych_cat[all$Psych=="Urea"]<- "Blood Assay"
all$Psych_cat[all$Psych=="Vitamin D"]<- "Blood Assay"

bubble plot of all sleep and correlates

– can we do some sort of other network plot?

circadian_col<- "brown2"
alertness_col<- "#00BFC4"
efficiency_col<- "#619CFF"
duration_col<- "#FF61CC"
non_insom_col<- "goldenrod"
regularity_col<- "seagreen"
satisf_col<- "mediumpurple1"
  
fact_colors<- c("Circadian Preference"=circadian_col, "Alertness"=alertness_col, "Efficiency"=efficiency_col, "Duration"=duration_col, "Insomnia"=non_insom_col, "Regularity"=regularity_col, "Satisfaction"=satisf_col)

head(all)
##                   Sleep                      Psych        beta         CI
## 1: Circadian Preference              Internalizing -0.06715839 0.04964146
## 2: Circadian Preference Psychosis Thought Disorder -0.09188608 0.05760290
## 3:           Efficiency              Internalizing -0.11788279 0.07821056
## 4:           Efficiency              Externalizing -0.11928991 0.08286011
## 5:             Insomnia              Internalizing -0.24399952 0.10085731
## 6:             Insomnia              Externalizing -0.39406871 0.05702922
##        study     Psych_cat
## 1: LDSC GSEM Internalizing
## 2: LDSC GSEM     Psychosis
## 3: LDSC GSEM Internalizing
## 4: LDSC GSEM Externalizing
## 5: LDSC GSEM Internalizing
## 6: LDSC GSEM Externalizing
all %>% filter(Sleep=="Circadian Preference" & Psych_cat=="Cardiovascular")
## Empty data.table (0 rows and 6 cols): Sleep,Psych,beta,CI,study,Psych_cat
all_plot<- all %>% group_by(Sleep,Psych_cat) %>%
  summarise(`Sig. Associations`=n()) %>%
  filter(!is.na(Psych_cat))
## `summarise()` has grouped output by 'Sleep'. You can override using the
## `.groups` argument.
ggplot(all_plot, aes(x=Sleep, y=Psych_cat, color= Sleep, size = `Sig. Associations`)) +
    geom_point(alpha=0.7) +
  ylab("Correlate Domain") +
  xlab("Sleep Health Domains")+
  scale_color_manual(values = fact_colors) +
  theme(panel.border = element_blank(),
      panel.grid.major = element_blank(),
          axis.ticks.x=element_blank(),
      axis.text.x=element_blank(),
      panel.grid.minor = element_blank())

just psychiatric

psych<- all %>% filter(Psych_cat=="Internalizing" | Psych_cat=="Externalizing" | Psych_cat=="Psychosis" | Psych_cat=="Compulsive" | Psych_cat=="Attention Problems")

just genetic psych

psych_geno<- psych %>% filter(study=="Batch LDSC")

rgs$Psych_cat<- rgs$Psych
rgs$Psych<- paste(rgs$Psych_cat, "Factor", sep=" ")

rgs$Sleep<- as.character(rgs$Sleep)
rgs$Psych_cat<- as.character(rgs$Psych_cat)

rgs$Psych_cat[rgs$Psych_cat=="Compulsive Thought Disorder"]<- "Compulsive Thought Disorders"
rgs$Psych_cat[rgs$Psych_cat=="Psychosis Thought Disorder"]<- "Psychosis Thought Disorders"

table(rgs$Psych_cat)
## 
## Compulsive Thought Disorders                Externalizing 
##                            2                            5 
##                Internalizing  Psychosis Thought Disorders 
##                            6                            5
table(psych_geno$Psych_cat)
## 
## Attention Problems      Externalizing      Internalizing          Psychosis 
##                  2                 18                 48                  5
psych_geno$Psych_cat<- as.character(psych_geno$Psych_cat)

psych_geno$Psych_cat[psych_geno$Psych_cat=="Psychosis"]<- "Psychosis Thought Disorders"

gene<- rbind(rgs, psych_geno)

gene<- data.frame(gene)


circadian_col<- "brown2"
alertness_col<- "#00BFC4"
efficiency_col<- "#619CFF"
duration_col<- "#FF61CC"
non_insom_col<- "goldenrod"
regularity_col<- "seagreen"
satisf_col<- "mediumpurple1"
  
fact_colors<- c("Circadian Preference"=circadian_col, "Alertness"=alertness_col, "Efficiency"=efficiency_col, "Duration"=duration_col, "Non-Insomnia"=non_insom_col, "Regularity"=regularity_col, "Satisfaction"=satisf_col)

gene$Sleep[gene$Sleep=="Insomnia"]<- "Non-Insomnia"
gene$Sleep[gene$Sleep=="Non Insomnia"]<- "Non-Insomnia"



geno_plt<- list()

for (i in unique(gene$Psych_cat)) {
  dat<- gene %>% filter(Psych_cat==i)
  p<- ggplot(dat, aes(y=Psych,x=beta, colour=Sleep, shape=study, xmin = beta-CI, xmax = beta+CI)) +
  geom_point(size=5,position=position_dodge(width=0.5)) + 
   geom_errorbarh(aes(x = CI), height=.4, position=position_dodge(width=0.5))+
  xlab("rG") +
  ylab("Psychiatric Traits") + 
    ggtitle(i)+
  scale_y_discrete(labels = function(x) str_wrap(x, width = 55))+
  geom_vline(xintercept = 0, linetype="dashed")+
  theme(legend.title = element_blank(),
        panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
               legend.position="bottom",
      plot.title = element_text(hjust = 0.5))+
     scale_color_manual(values = fact_colors) 
   # guides(shape=F)
    geno_plt[[i]]<- p
}
## Warning in geom_errorbarh(aes(x = CI), height = 0.4, position = position_dodge(width = 0.5)): Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
geno_plt
## $Internalizing

## 
## $`Psychosis Thought Disorders`

## 
## $Externalizing

## 
## $`Compulsive Thought Disorders`

## 
## $`Attention Problems`

### can they all fit in one?

## re size y axis traits tho

gene$Psych[gene$Psych=="Non-cancer illness code, self-reported: depression"]<- "self-reported: depression"
gene$Psych[gene$Psych=="Non-cancer illness code, self-reported: anxiety/panic attacks"]<- "self-reported: anxiety/panic attacks"
gene$Psych[gene$Psych=="Mental health problems ever diagnosed by a professional: Panic attacks"]<- "Panic attacks"
gene$Psych[gene$Psych=="Mental health problems ever diagnosed by a professional: Depression"]<- "Depression"
gene$Psych[gene$Psych=="Mental health problems ever diagnosed by a professional: Anxiety, nerves or generalized anxiety disorder"]<- "Anxiety, nerves or generalized anxiety disorder"

ggplot(gene, aes(y=Psych,x=beta, colour=Sleep, shape=study, xmin = beta-CI, xmax = beta+CI)) +
  geom_point(size=5,position=position_dodge(width=0.5)) + 
   geom_errorbarh(aes(x = CI), height=.4, position=position_dodge(width=0.5))+
  xlab("rG") +
  ylab("Psychiatric Traits") + 
    ggtitle("Genetic correlations among all sleep and psychiatric domains \nacross studies")+
  scale_y_discrete(labels = function(x) str_wrap(x, width = 55))+
  geom_vline(xintercept = 0, linetype="dashed")+
  theme(legend.title = element_blank(),
        panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
               legend.position="bottom",
      plot.title = element_text(hjust = 0.5))+
     scale_color_manual(values = fact_colors) 
## Warning in geom_errorbarh(aes(x = CI), height = 0.4, position =
## position_dodge(width = 0.5)): Ignoring unknown aesthetics: x

#### reg and neurot

neurot<- c("Neuroticism score","Worrier / anxious feelings", "Tense / 'highly strung'", "Sensitivity / hurt feelings", "Mood swings", "Miserableness", "Loneliness, isolation", "Irritability", "Guilty feelings", "Fed-up feelings")

neurot<- gene %>% filter(Psych %in% neurot) 

neurot$Psych <- factor(neurot$Psych, levels = neurot$Psych[1:10])



ggplot(neurot, aes(y=Psych,x=beta, colour=Sleep, shape=study, xmin = beta-CI, xmax = beta+CI)) +
  geom_point(size=5,position=position_dodge(width=0.5)) + 
   geom_errorbarh(aes(x = CI), height=.4, position=position_dodge(width=0.5))+
  xlab("rG") +
  ylab("Psychiatric Traits") + 
    ggtitle("Genetic correlations among sleep health and neuroticism")+
  scale_y_discrete(labels = function(x) str_wrap(x, width = 55))+
  geom_vline(xintercept = 0, linetype="dashed")+
  theme(legend.title = element_blank(),
        panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
               legend.position="bottom",
      plot.title = element_text(hjust = 0.5))+
     scale_color_manual(values = fact_colors) 
## Warning in geom_errorbarh(aes(x = CI), height = 0.4, position =
## position_dodge(width = 0.5)): Ignoring unknown aesthetics: x

colors for below plots

circadian_col<- "brown2"
alertness_col<- "#00BFC4"
efficiency_col<- "#619CFF"
duration_col<- "#FF61CC"
non_insom_col<- "goldenrod"
regularity_col<- "seagreen"
satisf_col<- "mediumpurple1"
  
fact_colors<- c("Circadian Preference"=circadian_col, "Alertness"=alertness_col, "Efficiency"=efficiency_col, "Duration"=duration_col, "Insomnia"=non_insom_col, "Regularity"=regularity_col, "Satisfaction"=satisf_col)

circadian

circ<- psych %>% filter(Sleep=="Circadian Preference") %>%
  filter(Psych!="Anti Social Behavior")

circ_plt<- list()
for (i in unique(circ$Psych_cat)) {
  dat<- circ %>% filter(Psych_cat==i)
  p<- ggplot(dat, aes(x=beta, y=Psych, colour=Sleep, shape=study, xmin = beta-CI, xmax = beta+CI)) +
  geom_point(size=5,position=position_dodge(width=0.5)) + 
   geom_errorbarh(aes(x = CI), height=.4, position=position_dodge(width=0.5))+
  xlab("Beta") +
  ylab("Psychiatric Trait") + 
    ggtitle(i)+
         scale_color_manual(values = fact_colors) +
  scale_y_discrete(labels = function(x) str_wrap(x, width = 55))+
  geom_vline(xintercept = 0, linetype="dashed")+
  theme(legend.title = element_blank(),
        panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      plot.title = element_text(hjust = 0.5))
  circ_plt[[i]]<- p
}
## Warning in geom_errorbarh(aes(x = CI), height = 0.4, position = position_dodge(width = 0.5)): Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
circ_plt
## $Internalizing

## 
## $Psychosis

## 
## $Externalizing

## 
## $Compulsive

## 
## $`Attention Problems`

#### alert

alert<- psych %>% filter(Sleep=="Alertness") %>%
  filter(Psych!="Anti Social Behavior")

alert_plt<- list()
for (i in unique(alert$Psych_cat)) {
  dat<- alert %>% filter(Psych_cat==i)
  p<- ggplot(dat, aes(x=beta, y=Psych, colour=Sleep, shape=study, xmin = beta-CI, xmax = beta+CI)) +
  geom_point(size=5,position=position_dodge(width=0.5)) + 
   geom_errorbarh(aes(x = CI), height=.4, position=position_dodge(width=0.5))+
  xlab("Beta") +
  ylab("Psychiatric Trait") + 
    ggtitle(i)+
         scale_color_manual(values = fact_colors) +
  scale_y_discrete(labels = function(x) str_wrap(x, width = 55))+
  geom_vline(xintercept = 0, linetype="dashed")+
  theme(legend.title = element_blank(),
        panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      plot.title = element_text(hjust = 0.5))
  alert_plt[[i]]<- p
}
## Warning in geom_errorbarh(aes(x = CI), height = 0.4, position = position_dodge(width = 0.5)): Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
alert_plt
## $Internalizing

## 
## $Externalizing

## 
## $Psychosis

## 
## $Compulsive

## 
## $`Attention Problems`

table(alert$Psych_cat)
## 
## Attention Problems         Compulsive      Externalizing      Internalizing 
##                  5                  2                  8                 26 
##          Psychosis 
##                 16

effic

effic<- psych %>% filter(Sleep=="Efficiency") %>%
  filter(Psych!="Anti Social Behavior")

effic_plt<- list()

for (i in unique(alert$Psych_cat)) {
  dat<- effic %>% filter(Psych_cat==i)
  p<- ggplot(dat, aes(x=beta, y=Psych, colour=Sleep, shape=study, xmin = beta-CI, xmax = beta+CI)) +
  geom_point(size=5,position=position_dodge(width=0.5)) + 
   geom_errorbarh(aes(x = CI), height=.4, position=position_dodge(width=0.5))+
  xlab("Beta") +
  ylab("Psychiatric Trait") + 
    ggtitle(i)+
  scale_color_manual(values = fact_colors) +
  scale_y_discrete(labels = function(x) str_wrap(x, width = 55))+
  geom_vline(xintercept = 0, linetype="dashed")+
  theme(legend.title = element_blank(),
        panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      plot.title = element_text(hjust = 0.5))
  effic_plt[[1]]<- p
  }
## Warning in geom_errorbarh(aes(x = CI), height = 0.4, position = position_dodge(width = 0.5)): Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
effic_plt
## [[1]]

table(effic$Psych_cat)
## 
## Attention Problems         Compulsive      Externalizing      Internalizing 
##                  3                  1                  7                  8 
##          Psychosis 
##                  9

dur

dur<- psych %>% filter(Sleep=="Duration") %>%
  filter(Psych!="Anti Social Behavior")

dur_plt<- list()

for (i in unique(dur$Psych_cat)) {
  dat<- dur %>% filter(Psych_cat==i)
  p<- ggplot(dat, aes(x=beta, y=Psych, colour=Sleep, shape=study, xmin = beta-CI, xmax = beta+CI)) +
  geom_point(size=5,position=position_dodge(width=0.5)) + 
   geom_errorbarh(aes(x = CI), height=.4, position=position_dodge(width=0.5))+
  xlab("Beta") +
    scale_color_manual(values = fact_colors) +
    ggtitle(i)+
  ylab("Psychiatric Trait") + 
  scale_y_discrete(labels = function(x) str_wrap(x, width = 55))+
  geom_vline(xintercept = 0, linetype="dashed")+
  theme(legend.title = element_blank(),
        panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      plot.title = element_text(hjust = 0.5))
  dur_plt[[i]]<- p
}
## Warning in geom_errorbarh(aes(x = CI), height = 0.4, position = position_dodge(width = 0.5)): Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
dur_plt
## $Internalizing

## 
## $Externalizing

## 
## $Psychosis

## 
## $Compulsive

## 
## $`Attention Problems`

table(dur$Psych_cat)
## 
## Attention Problems         Compulsive      Externalizing      Internalizing 
##                  3                  2                  9                 11 
##          Psychosis 
##                 24

satsif

satisf<- psych %>% filter(Sleep=="Satisfaction") %>%
  filter(Psych!="Anti Social Behavior")

satisf_plt<- list()

for (i in unique(satisf$Psych_cat)) {
  dat<- satisf %>% filter(Psych_cat==i)
  p<- ggplot(dat, aes(x=beta, y=Psych, colour=Sleep, shape=study, xmin = beta-CI, xmax = beta+CI)) +
  geom_point(size=5,position=position_dodge(width=0.5)) + 
   geom_errorbarh(aes(x = CI), height=.4, position=position_dodge(width=0.5))+
  xlab("Beta") +
  ylab("Psychiatric Trait") + 
    ggtitle(i)+
        scale_color_manual(values = fact_colors) +
  scale_y_discrete(labels = function(x) str_wrap(x, width = 55))+
  geom_vline(xintercept = 0, linetype="dashed")+
  theme(legend.title = element_blank(),
        panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      plot.title = element_text(hjust = 0.5))
satisf_plt[[i]]<- p
}
## Warning in geom_errorbarh(aes(x = CI), height = 0.4, position = position_dodge(width = 0.5)): Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
satisf_plt
## $Internalizing

## 
## $Externalizing

table(satisf$Psych_cat)
## 
## Externalizing Internalizing 
##             2             2

var

reg<- psych %>% filter(Sleep=="Regularity") %>%
  filter(Psych!="Anti Social Behavior")

reg_plt<- list()

for (i in unique(reg$Psych_cat)) {
  dat<- reg %>% filter(Psych_cat==i)
  p<- ggplot(dat, aes(x=beta, y=Psych, colour=Sleep, shape=study, xmin = beta-CI, xmax = beta+CI)) +
  geom_point(size=5,position=position_dodge(width=0.5)) + 
   geom_errorbarh(aes(x = CI), height=.4, position=position_dodge(width=0.5))+
  xlab("Beta") +
  ylab("Psychiatric Trait") + 
        scale_color_manual(values = fact_colors) +
    ggtitle(i)+
  scale_y_discrete(labels = function(x) str_wrap(x, width = 55))+
  geom_vline(xintercept = 0, linetype="dashed")+
  theme(legend.title = element_blank(),
        panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      plot.title = element_text(hjust = 0.5))
    reg_plt[[i]]<- p
}
## Warning in geom_errorbarh(aes(x = CI), height = 0.4, position = position_dodge(width = 0.5)): Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
reg_plt
## $Internalizing

## 
## $Externalizing

## 
## $Psychosis

## 
## $Compulsive

## 
## $`Attention Problems`

table(reg$Psych_cat)
## 
## Attention Problems         Compulsive      Externalizing      Internalizing 
##                  2                  1                  7                 11 
##          Psychosis 
##                  4

insom

insom<- psych %>% filter(Sleep=="Insomnia") %>%
  filter(Psych!="Anti Social Behavior")

insom_plt<- list()

for (i in unique(insom$Psych_cat)) {
  dat<- insom %>% filter(Psych_cat==i)
  p<- ggplot(dat, aes(x=beta, y=Psych, colour=Sleep, shape=study, xmin = beta-CI, xmax = beta+CI)) +
  geom_point(size=5,position=position_dodge(width=0.5)) + 
   geom_errorbarh(aes(x = CI), height=.4, position=position_dodge(width=0.5))+
  xlab("Beta") +
  ylab("Psychiatric Trait") + 
        scale_color_manual(values = fact_colors) +
ggtitle(i)+
  scale_y_discrete(labels = function(x) str_wrap(x, width = 55))+
  geom_vline(xintercept = 0, linetype="dashed")+
  theme(legend.title = element_blank(),
        panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      plot.title = element_text(hjust = 0.5))
  insom_plt[[i]]<- p
}
## Warning in geom_errorbarh(aes(x = CI), height = 0.4, position = position_dodge(width = 0.5)): Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
insom_plt
## $Internalizing

## 
## $Externalizing

## 
## $Psychosis

## 
## $Compulsive

## 
## $`Attention Problems`

table(insom$Psych_cat)
## 
## Attention Problems         Compulsive      Externalizing      Internalizing 
##                  4                  1                 15                 32 
##          Psychosis 
##                  6
insom<- data.frame(insom)

all sleep and psych!!!

all_psych<- rbind(circ, effic, alert, satisf, effic, insom,reg)
head(all_psych)
##                   Sleep                       Psych        beta         CI
## 1: Circadian Preference               Internalizing -0.06715839 0.04964146
## 2: Circadian Preference  Psychosis Thought Disorder -0.09188608 0.05760290
## 3: Circadian Preference               Internalizing -0.06500962 0.06820794
## 4: Circadian Preference               Externalizing  0.04545356 0.09060105
## 5: Circadian Preference  Psychosis Thought Disorder -0.03593931 0.07169101
## 6: Circadian Preference Compulsive Thought Disorder -0.03373276 0.12826175
##                       study     Psych_cat
## 1:                LDSC GSEM Internalizing
## 2:                LDSC GSEM     Psychosis
## 3: Multiple Regression GSEM Internalizing
## 4: Multiple Regression GSEM Externalizing
## 5: Multiple Regression GSEM     Psychosis
## 6: Multiple Regression GSEM    Compulsive
all_psych$sig<- ifelse(all_psych$beta>0, ifelse(all_psych$beta-all_psych$CI<0, 0, 1), ifelse(all_psych$beta<0, ifelse(all_psych$beta+all_psych$CI>0, 0, 1), NA))

all_psych<- all_psych %>% filter(sig==1) ### 190 sig associations

all_psych_plt<- list()

for (i in unique(all_psych$Psych_cat)) {
  dat<- all_psych %>% filter(Psych_cat==i)
  p<- ggplot(dat, aes(x=beta, y=Psych, colour=Sleep, shape=study, xmin = beta-CI, xmax = beta+CI)) +
  geom_point(size=5,position=position_dodge(width=0.5)) + 
   geom_errorbarh(aes(x = CI), height=.4, position=position_dodge(width=0.5))+
  xlab("Beta") +
  ylab("Psychiatric Trait") + 
        scale_color_manual(values = fact_colors) +
ggtitle(i)+
  scale_y_discrete(labels = function(x) str_wrap(x, width = 55))+
  geom_vline(xintercept = 0, linetype="dashed")+
  theme(legend.title = element_blank(),
        panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      plot.title = element_text(hjust = 0.5))
  all_psych_plt[[i]]<- p
}
## Warning in geom_errorbarh(aes(x = CI), height = 0.4, position = position_dodge(width = 0.5)): Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
all_psych_plt
## $Internalizing

## 
## $Psychosis

## 
## $Externalizing

## 
## $`Attention Problems`

## 
## $Compulsive

insom<- insom %>% group_by(Psych_cat) %>%
  summarise(`Sig. Associations`=n()) %>%
  mutate(Sleep="Insomnia")

circ<- circ %>% group_by(Psych_cat) %>%
  summarise(`Sig. Associations`=n()) %>%
  mutate(Sleep="Circadian Preference")

alert<- alert %>% group_by(Psych_cat) %>%
  summarise(`Sig. Associations`=n()) %>%
  mutate(Sleep="Alertness")

effic<- effic %>% group_by(Psych_cat) %>%
  summarise(`Sig. Associations`=n()) %>%
  mutate(Sleep="Efficiency")

dur<- dur %>% group_by(Psych_cat) %>%
  summarise(`Sig. Associations`=n()) %>%
  mutate(Sleep="Duration")

reg<- reg %>% group_by(Psych_cat) %>%
  summarise(`Sig. Associations`=n()) %>%
  mutate(Sleep="Regularity")

satisf<- satisf %>% group_by(Psych_cat) %>%
  summarise(`Sig. Associations`=n()) %>%
  mutate(Sleep="Satisfaction")

plot N of findings by psychiatric domain

sum<- rbind(insom, circ, alert, effic, dur, reg, satisf)

ggplot(sum, aes(x=Sleep, y=Psych_cat, color= Sleep, size = `Sig. Associations`)) +
    geom_point(alpha=0.7) +
  ylab("Psychiatric") +
  xlab("Sleep Health Domains")+
  scale_color_manual(values = fact_colors) +
    theme(panel.border = element_blank(),
      panel.grid.major = element_blank(),
          axis.ticks.x=element_blank(),
      axis.text.x=element_blank(),
      panel.grid.minor = element_blank())

### all “causal”

circadian_col<- "brown2"
alertness_col<- "#00BFC4"
efficiency_col<- "#619CFF"
duration_col<- "#FF61CC"
non_insom_col<- "goldenrod"
regularity_col<- "seagreen"
satisf_col<- "mediumpurple1"
  
fact_colors<- c("Circadian Preference"=circadian_col, "Alertness"=alertness_col, "Efficiency"=efficiency_col, "Duration"=duration_col, "Insomnia"=non_insom_col, "Regularity"=regularity_col, "Satisfaction"=satisf_col)

causal<- rbind(betas, within, MR_out)
#causal<- rbind(within, MR_out)

nrow(causal)
## [1] 139
head(causal)
##                   Sleep                       Psych         beta         CI
## 1: Circadian Preference               Internalizing -0.065009619 0.06820794
## 2: Circadian Preference               Externalizing  0.045453564 0.09060105
## 3: Circadian Preference  Psychosis Thought Disorder -0.035939306 0.07169101
## 4: Circadian Preference Compulsive Thought Disorder -0.033732757 0.12826175
## 5:           Efficiency               Internalizing  0.006214299 0.07767874
## 6:           Efficiency               Externalizing  0.008887780 0.10385130
##                       study
## 1: Multiple Regression GSEM
## 2: Multiple Regression GSEM
## 3: Multiple Regression GSEM
## 4: Multiple Regression GSEM
## 5: Multiple Regression GSEM
## 6: Multiple Regression GSEM
causal$Sleep_cat[causal$Sleep=="Chronotype" | causal$Sleep=="Social Jetlag" | causal$Sleep=="Chronotype (unit decrease)" |  causal$Sleep=="Chronotype (unit increase)" | causal$Sleep=="Circadian Preference Factor" | causal$Sleep=="Circadian Preference"]<- "Circadian Preference"

causal$Sleep_cat[causal$Sleep=="Alertness Factor" | causal$Sleep=="Napping" | causal$Sleep=="Daytime alertness (sleepiness GWAS)" | causal$Sleep=="Alertness"]<- "Alertness"

causal$Sleep_cat[causal$Sleep=="Accelerometer Weekend Efficiency" | causal$Sleep=="Efficiency Factor" | causal$Sleep=="Accelerometer Weekday Efficiency" | causal$Sleep=="Efficiency"]<- "Efficiency"

causal$Sleep_cat[causal$Sleep=="Weekend Duration" | causal$Sleep=="accelerometer Weekday Duration" | causal$Sleep=="Self-reported sleep duration" |  causal$Sleep=="Weekday Duration" | causal$Sleep=="Accelerometer sleep duration" |  causal$Sleep=="Accelerometer Weekday Duration" | causal$Sleep=="Accelerometer Weekend Duration" | causal$Sleep=="Duration"]<- "Duration"

causal$Sleep_cat[causal$Sleep=="Non Insomnia" | causal$Sleep=="Non-Insomnia" | causal$Sleep=="Accelerometer Weekday Efficiency" | causal$Sleep=="Insomnia"]<- "Insomnia"

causal$Sleep_cat[causal$Sleep=="Variability" | causal$Sleep=="Regularity"]<- "Regularity"


causal$Psych_cat <- factor(causal$Psych, levels=all$Psych, labels=all$Psych_cat)


causal$sig<- ifelse(causal$beta>0, ifelse(causal$beta-causal$CI<0, 0, 1), ifelse(causal$beta<0, ifelse(causal$beta+causal$CI>0, 0, 1), NA))

causal<- causal %>% filter(sig==1) %>%
  filter(Psych!="Anti Social Behavior") %>%
  filter(abs(beta)<3) 

causal_plt<- list()

for (i in unique(causal$Psych_cat)) {
  dat<- causal %>% filter(Psych_cat==i)
  p<- ggplot(dat, aes(x=beta, y=Psych, colour=Sleep_cat, shape=study, xmin = beta-CI, xmax = beta+CI)) +
  geom_point(size=5,position=position_dodge(width=0.5)) + 
   geom_errorbarh(aes(x = CI), height=.4, position=position_dodge(width=0.5))+
  xlab("Beta") +
  ylab("Psychiatric Trait") + 
        scale_color_manual(values = fact_colors) +
ggtitle(i)+
  scale_y_discrete(labels = function(x) str_wrap(x, width = 55))+
  geom_vline(xintercept = 0, linetype="dashed")+
  theme(legend.title = element_blank(),
        panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      plot.title = element_text(hjust = 0.5))
  causal_plt[[i]]<- p
}
## Warning in geom_errorbarh(aes(x = CI), height = 0.4, position = position_dodge(width = 0.5)): Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
## Ignoring unknown aesthetics: x
causal_plt
## $Internalizing

## 
## $Externalizing

## 
## $Psychosis

## 
## $Compulsive

## 
## $`Attention Problems`

dur_psych<- causal %>% filter(Sleep_cat=="Duration" & Psych_cat=="Psychosis")

ggplot(dur_psych, aes(x=beta, y=Psych, colour=Sleep_cat, shape=study, xmin = beta-CI, xmax = beta+CI)) +
  geom_point(size=5,position=position_dodge(width=0.5)) + 
   geom_errorbarh(aes(x = CI), height=.4, position=position_dodge(width=0.5))+
  xlab("Beta") +
  ylab("Psychiatric Trait") + 
        scale_color_manual(values = fact_colors) +
ggtitle("Sleep Duration & Psychosis")+
  scale_y_discrete(labels = function(x) str_wrap(x, width = 55))+
  geom_vline(xintercept = 0, linetype="dashed")+
  theme(legend.title = element_blank(),
        panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      plot.title = element_text(hjust = 0.5))
## Warning in geom_errorbarh(aes(x = CI), height = 0.4, position =
## position_dodge(width = 0.5)): Ignoring unknown aesthetics: x

insom_int<- causal %>% filter(Sleep_cat=="Insomnia" & Psych_cat=="Internalizing")

ggplot(insom_int, aes(x=beta, y=Psych, colour=Sleep_cat, shape=study, xmin = beta-CI, xmax = beta+CI)) +
  geom_point(size=5,position=position_dodge(width=0.5)) + 
   geom_errorbarh(aes(x = CI), height=.4, position=position_dodge(width=0.5))+
  xlab("Beta") +
  ylab("Psychiatric Trait") + 
        scale_color_manual(values = fact_colors) +
ggtitle("Insomnia & Internalizing")+
  scale_y_discrete(labels = function(x) str_wrap(x, width = 55))+
  geom_vline(xintercept = 0, linetype="dashed")+
  theme(legend.title = element_blank(),
        panel.border = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      plot.title = element_text(hjust = 0.5))
## Warning in geom_errorbarh(aes(x = CI), height = 0.4, position =
## position_dodge(width = 0.5)): Ignoring unknown aesthetics: x