library(meta)
library (metafor)

### META-ANALYSIS, FUNNEL PLOT AND EGGER'S TEST
#1 Resilience - post-intervention
# Generic invariance approach
data_Resilience_post <- read.csv2("/Users/angelakunzler/dataset01_Resilience_post-intervention.csv")
View(data_Resilience_post)
m1 <- metagen(TE, 
              seTE, 
              data = data_Resilience_post, 
              studlab = paste(Author, Year), 
              comb.fixed = FALSE, 
              comb.random = TRUE, 
              method.tau = "SJ", 
              hakn = TRUE, 
              prediction = TRUE, 
              sm="SMD")
summary(m1)
m1
forest(m1)

funnel(m1)

metabias(m1, method = "linreg")

#2 Resilience - short-term follow-up
# Means and SDs
data_Resilience_FU <- read.csv2("/Users/angelakunzler/dataset02_Resilience_short-term_FU.csv")
View(data_Resilience_FU)
data_Resilience_FU
m2 <- metacont(Ne, Me, Se, Nc, Mc, Sc, data = data_Resilience_FU, studlab = paste(Author, Year), sm = "SMD")
summary(m2)
m2
forest(m2)

funnel(m2)

metabias(m2, method = "linreg")

#3 Depression - post-intervention
# Generic invariance approach
data_Depression_post <- read.csv2("/Users/angelakunzler/dataset03_Depression_post-intervention.csv")
View(data_Depression_post)
m3 <- metagen(TE, 
              seTE, 
              data = data_Depression_post, 
              studlab = paste(Author, Year), 
              comb.fixed = FALSE, 
              comb.random = TRUE, 
              method.tau = "SJ", 
              hakn = TRUE, 
              prediction = TRUE, 
              sm="SMD")
summary(m3)
m3
forest(m3)

funnel(m3)

metabias(m3, method = "linreg", k=9)

#4 Stress or stress perception - post-intervention
# Generic invariance approach
data_Stress_post <- read.csv2("/Users/angelakunzler/dataset04_Stress_post-intervention.csv")
View(data_Stress_post)
data_Stress_post
m4 <- metagen(TE, 
              seTE, 
              data = data_Stress_post, 
              studlab = paste(Author, Year), 
              comb.fixed = FALSE, 
              comb.random = TRUE, 
              method.tau = "SJ", 
              hakn = TRUE, 
              prediction = TRUE, 
              sm="SMD")
summary(m4)
m4
forest(m4)

funnel(m4)

metabias(m4, method = "linreg")

#5 Stress or stress perception - short-term follow-up
#Means and SDs
data_Stress_FU <- read.csv2("/Users/angelakunzler/dataset05_Stress_short-term_FU.csv")
View(data_Stress_FU)
data_Stress_FU
m5 <- metacont(Ne, Me, Se, Nc, Mc, Sc, data = data_Stress_FU, studlab = paste(Author, Year), sm = "SMD")
summary(m5)
m5
forest(m5)

funnel(m5)

metabias(m5, method = "linreg")

#6 Wellbeing or quality of life - post-intervention
# Generic invariance approach
data_Wellbeing_post <- read.csv2("/Users/angelakunzler/dataset06_Wellbeing_post-intervention.csv")
View(data_Wellbeing_post)
data_Wellbeing_post
m6 <- metagen(TE, 
              seTE, 
              data = data_Wellbeing_post, 
              studlab = paste(Author, Year), 
              comb.fixed = FALSE, 
              comb.random = TRUE, 
              method.tau = "SJ", 
              hakn = TRUE, 
              prediction = TRUE, 
              sm="SMD")
summary(m6)
m6
forest(m6)

funnel(m6)

metabias(m6, method = "linreg")

#7 Wellbeing or quality of life - short-term follow-up
# Generic invariance approach
data_Wellbeing_FU <- read.csv2("/Users/angelakunzler/dataset07_Wellbeing_short-term_FU.csv")
View(data_Wellbeing_FU)
data_Wellbeing_FU
m7 <- metagen(TE, 
                 seTE, 
                 data = data_Wellbeing_FU, 
                 studlab = paste(Author, Year), 
                 comb.fixed = FALSE, 
                 comb.random = TRUE, 
                 method.tau = "SJ", 
                 hakn = TRUE, 
                 prediction = TRUE, 
                 sm="SMD")
summary(m7)
m7
forest(m7)

funnel(m7)

metabias(m7, method = "linreg")

# Contour-enhanced funnel plots

#1 Resilience - post-intervention
# Funnel plot
funnel(m1)

# Same result as code above:
funnel(m1$TE, m1$seTE, sm="RR")

# Funnel plot with confidence intervals, fixed effect estimate and contours
cc <- funnel(m1, comb.fixed=TRUE,
             level=0.95, contour=c(0.9, 0.95, 0.99))$col.contour
legend(0.05, 0.05,
       c("0.1 > p > 0.05", "0.05 > p > 0.01", "< 0.01"), fill=cc)

# Contour-enhanced funnel plot with user-chosen colours
funnel(m1, comb.fixed=TRUE,
       level=0.95, contour=c(0.9, 0.95, 0.99),
       col.contour=c("darkblue", "blue", "lightblue"),
       lwd=1.5, cex=0.5, pch=16,  studlab=TRUE, cex.studlab=0.5)

legend(0.75, 0.00,
       c("0.1 > p > 0.05", "0.05 > p > 0.01", "< 0.01"),
       fill=c("darkblue", "blue", "lightblue"))

#3 Depression - post-intervention
# Funnel plot
funnel(m3)

# Same result as code above:
funnel(m3$TE, m3$seTE, sm="RR")

# Funnel plot with confidence intervals, fixed effect estimate and contours
cc <- funnel(m3, comb.fixed=TRUE,
             level=0.95, contour=c(0.9, 0.95, 0.99))$col.contour
legend(0.05, 0.05,
       c("0.1 > p > 0.05", "0.05 > p > 0.01", "< 0.01"), fill=cc)

# Contour-enhanced funnel plot with user-chosen colours
funnel(m3, comb.fixed=TRUE,
       level=0.95, contour=c(0.9, 0.95, 0.99),
       col.contour=c("darkblue", "blue", "lightblue"),
       lwd=1.5, cex=0.5, pch=16,  studlab=TRUE, cex.studlab=0.5)

legend(2.00, -0.02,
       c("0.1 > p > 0.05", "0.05 > p > 0.01", "< 0.01"),
       fill=c("darkblue", "blue", "lightblue"))

#4 Stress or stress perception - post-intervention
# Funnel plot
funnel(m4)

# Same result as code above:
funnel(m4$TE, m4$seTE, sm="RR")

# Funnel plot with confidence intervals, fixed effect estimate and contours
cc <- funnel(m4, comb.fixed=TRUE,
             level=0.95, contour=c(0.9, 0.95, 0.99))$col.contour
legend(0.05, 0.05,
       c("0.1 > p > 0.05", "0.05 > p > 0.01", "< 0.01"), fill=cc)

# Contour-enhanced funnel plot with user-chosen colours
funnel(m4, comb.fixed=TRUE,
       level=0.95, contour=c(0.9, 0.95, 0.99),
       col.contour=c("darkblue", "blue", "lightblue"),
       lwd=1.5, cex=0.5, pch=16,  studlab=TRUE, cex.studlab=0.5)

legend(2.60, -0.02,
       c("0.1 > p > 0.05", "0.05 > p > 0.01", "< 0.01"),
       fill=c("darkblue", "blue", "lightblue"))

#6 Well-being or quality of life - post-intervention
# Funnel plot
funnel(m6)

# Same result as code above:
funnel(m6$TE, m6$seTE, sm="RR")

# Funnel plot with confidence intervals, fixed effect estimate and contours
cc <- funnel(m6, comb.fixed=TRUE,
             level=0.95, contour=c(0.9, 0.95, 0.99))$col.contour
legend(0.05, 0.05,
       c("0.1 > p > 0.05", "0.05 > p > 0.01", "< 0.01"), fill=cc)

# Contour-enhanced funnel plot with user-chosen colours
funnel(m6, comb.fixed=TRUE,
       level=0.95, contour=c(0.9, 0.95, 0.99),
       col.contour=c("darkblue", "blue", "lightblue"),
       lwd=1.5, cex=0.5, pch=16,  studlab=TRUE, cex.studlab=0.5)

legend(2.80, -0.10,
       c("0.1 > p > 0.05", "0.05 > p > 0.01", "< 0.01"),
       fill=c("darkblue", "blue", "lightblue"))
