参考文献:Mendelian randomization analysis does not reveal a causal influence of mental diseases on osteoporosis(孟德尔随机化分析未揭示精神疾病对骨质疏松症的因果影响)
文章框架:
(1)绘制总的森林图
#制定函数批量处理数据
datamr<-function(x){
library(magrittr)
out<-combined_table[combined_table$method==x,] %>% generate_odds_ratios()
fenge<-function(x){
split_string <- strsplit(x, "||", fixed = TRUE)
result <- split_string[[1]][1]
return(result)
}
out_1<-out
out_1[,3:4]<-apply(out_1[,3:4],2,function(x) sapply(x, fenge))
#重新排列
#指定排序顺序
out_1$outcome
order_vec <- c("Forearm bone mineral density ", "Lumbar spine bone mineral density ", "Femoral neck bone mineral density ",
"Total body bone mineral density ")
#根据指定顺序排序数据框
out_2 <- out_1[order(match(out_1$outcome, order_vec)), ]
#修改数据适合绘制森林图
out_2$outcome[-seq(3, nrow(out_2), by = 5)] <-""
out_2$`OR(95%CI)`<-sprintf("%.2f(%.2f to %.2f)",out_2$or,out_2$or_lci95,out_2$or_uci95)
return(out_2)
}
#IVW
IVW<-datamr("Inverse variance weighted")
#IVW(fixed-effect)
`IVW(fixed-effect)`<-datamr("Inverse variance weighted (fixed effects)")
#绘制多组的森林图
new_colnames <- paste0(colnames(`IVW(fixed-effect)`), "_2")
colnames(`IVW(fixed-effect)`) <- new_colnames
dt<-cbind(IVW,`IVW(fixed-effect)`)
dt$` `<-paste(rep(" ", dim(dt)[1]), collapse = " ")
dt$` `<-paste(rep(" ", dim(dt)[1]), collapse = " ")
colnames(dt)
#绘制基本图形
tm <- forest_theme(base_size = 8, # 文字大小
#可信区间
ci_pch = 10,#点形状
ci_col = "red", #颜色
ci_lty = 1,#线条类型
ci_lwd = 4,#线宽度
ci_Theight = 0.2, #两端加短竖线
# 参考线
refline_lwd = 1,#宽度
refline_lty = "dashed",#类型
refline_col = "black",#颜色
# 汇总菱形
summary_fill = "blue",#填充色
summary_col = "blue",#边框色
# 脚注
footnote_cex = 0.6,#大小
footnote_fontface = "italic",#字体
footnote_col = "black")#颜色
p <-forest(dt[,c(3,4,31,15,18,19,32,30)],
est = list(dt$or,
dt$or_2),
lower = list(dt$or_lci95,
dt$or_lci95_2),
upper = list(dt$or_uci95,
dt$or_uci95_2),
ci_column = c(3, 7),
ref_line = 1,
xlim = c(0.01,100),x_trans ="log10",
ticks_at = c(0.01,0.1,1,10,100),
#vert_line = c(1,2),
#nudge_y = 0.2,
theme = tm)
p
#顶部插入信息
p1<-insert_text(p,text =c("IVW","IVW(fixed-effect)"),
col = c(2,6),
part = "header",
gp = gpar(fontface =
"bold"))
p1
#在标题下方加一条下划线
p2 <-add_border(p1,
row =c(1),
col=c(1:3,5:7),
part = "header")
p2
#为标题的两行添加下划线
p3 <-add_border(p2,
row =c(0,2),
col=1:8,
part = "header")
p3
(2)绘制留一法森林图
Mydata<- harmonise_data(exposure_dat=wer,
outcome_dat=asd,
action= 2)
leave<- mr_leaveoneout(Mydata)
mr_leaveoneout_plot(leave)
3、散点图、森林图、漏斗图
#定义了5种方法,包括固定效应和随机效应模型
random<-mr(Mydata,method_list=c("mr_ivw", "mr_ivw_fe",
"mr_two_sample_ml",
"mr_egger_regression", "mr_weighted_median",
"mr_penalised_weighted_median",
"mr_simple_mode", "mr_weighted_mode"))
#散点图
mr_scatter_plot(random,Mydata)
#森林图
forest<- mr_singlesnp(Mydata)
mr_forest_plot(forest)
#漏斗图
mr_funnel_plot(forest)
#生成OR和可信区间
out<-generate_odds_ratios(random)