I'm a super beginner with R & RStudio and I'm desperately looking for a solution to the following problem:
I'm performing a soubgroups analysis in which we compare the cognitive performance (measured by the so called Montreal Cognitive Assessment Test) of different coffee consumer groups (devided by the number of cups consumed by day, e.g. <1cup/d, 1cup/d. 2-3cups/d, 4-5cups/d and >5cups/d). We would like to performe sex (female vs. male) and age (<80 and >80 years of age) related subanalysis. Up to now i was able to show the results of one single group (e.g. only male, or only male <80 years of age). See code and plot below.
2b. Subanalyzes
dt_bsl_women <- dt_bsl[dt_bsl$pat.sex=="Female",]
dt_bsl_men <- dt_bsl[dt_bsl$pat.sex=="Male",]
dt_bsl_old <- dt_bsl[dt_bsl$age.bl>80,]
dt_bsl_young <- dt_bsl[dt_bsl$age.bl<=80,]
dt_bsl_men_old <- dt_bsl[dt_bsl$pat.sex=="Male"&dt_bsl$age.bl>80,]
dt_bsl_men_young <- dt_bsl[dt_bsl$pat.sex=="Male"&dt_bsl$age.bl<=80,]
dt_bsl_women_old <- dt_bsl[dt_bsl$pat.sex=="Female"&dt_bsl$age.bl>80,]
dt_bsl_women_young <- dt_bsl[dt_bsl$pat.sex=="Female"&dt_bsl$age.bl<=80,]
Models for MoCA-score
Only men
mod_bsl <- lmer (moca.merged ~ exposure + age.bsl.centered + education +
bmi.centered + sport + active_smoker + prev.stroke + prev.diabetes +
prev.hypertonie + gds.cat +
(1|center) + crphs + il6, data= subset(dt_bsl_men , dt_bsl_women))
# summary(mod_bsl)
ci <- confint(mod_bsl, oldNames=FALSE)
output <- tidy(mod_bsl, conf.int=TRUE, exponentiate=FALSE, effects="fixed")[,-1]
kable(output[,c(1,2,7,8,6)], digits=c(2,2,2,2,3))
# plot
res <- data.frame("term"=c("1", "2-3", "4-5", ">5"),
"estimate" = summary(mod_bsl)$coefficients[2:5,1],
"conf.low" = ci[4:7,1],
"conf.high"= ci[4:7,2])
res$term <- factor(res$term, levels=c("1", "2-3", "4-5", ">5"))
ggplot(res, aes(y = estimate, x = term, col=term)) +
geom_pointrange(aes(ymin = conf.low, ymax = conf.high), size = 0.8)+
geom_hline(yintercept = 0, linetype = "dotted", size = 1) +
labs(title="MoCA-Scores in men with different coffee consumption", y = "Effect estimate on MoCA-score", x = "Consumed cups of coffee per day") +
coord_flip(ylim = c(-6, 8)) +
theme_bw()+
theme(legend.position="none", axis.text=element_text(size=14, face = "bold"), axis.title=element_text(size=18), plot.title = element_text(size=22))
The outpot looks like following:
My goal is to create something like this, in which several groups (here comparison Sweden and Danmark) in my case with e.g. only males, only female, male >80 years of age, female >80 years of age and so on in one plot.
Many thanks for your help!
I tried to add at the lmer function at the last position the different dataset dt_bsl_women resp. dt_bsl_men etc...
I also tried the function groupby(xxx) but it didn't work.