rm(list=ls()) setwd("C:\\Users\\julia\\Documents\\Thesis\\SocClass\\Scandinavian journal of public health\\Response to reviewers") library(metafor) library(ggplot2) #get data soc_cl <- read.csv2("SocCl.csv") str(soc_cl) #effect size es<-escalc(measure="RR", ai=ai, ci=ci, n1i=n1i, n2i=n2i, data=soc_cl) es$AV2 <- log(es$AV) es$SD <- (log(es$IC_U) - log(es$IC_L))/(2*1.96) es$V <- es$SD^2 es$yi[is.na(es$yi)] <- es$AV2[is.na(es$yi)] es$vi[is.na(es$vi)] <- es$V[is.na(es$vi)] es<-es[order(es$E, es$yi) ,] str(es) View(es) #pooled effect size all indicators together, fixed and random effect a<-rma(yi=yi, vi=vi, data=es, measure="RR", method="FE", slab=Author) predict(a, transf=exp, digits=2) a<-rma(yi=yi, vi=vi, data=es, measure="RR", slab=paste(Author, E, sep=", ")) predict(a, transf=exp, digits=2) #pooled effect size in indicators, fixed and random effect a <- rma(yi=yi, vi=vi, data=es, method="FE") tbl1 <-function(a) {c(paste(round(exp(a$beta[1,1]),2), " (", round(exp(a$ci.lb[1]),2), ", ", round(exp(a$ci.ub[1]),2), ")", "; I2 = ", round(a$I2,2), "%", sep=""))} tbl1(a) t1 <- rep(NA) for (i in (unique(es$E))) { for (j in (c("FE", "REML"))) { a<-rma(yi=yi, vi=vi, data=es[es$E==i,], method=j) b<-c(paste(i,j), tbl1(a)) t1<-c(t1,b) } } t0<-t1[-1] t1d<- as.data.frame(matrix(t0, byrow=TRUE, ncol=4, nrow=5)) write.csv2(t1d, "table1.csv") #separate models in social class indicators tbl2 <-function(a) {c(paste(round(exp(a$beta[1,1]),2), " (", round(exp(a$ci.lb[1]),2), ", ", round(exp(a$ci.ub[1]),2), ")", sep=""), paste(round(exp(a$beta[2,1]),2), " (", round(exp(a$ci.lb[2]),2), ", ", round(exp(a$ci.ub[2]),2), ")", sep=""), paste(round(exp(a$beta[3,1]),2), " (", round(exp(a$ci.lb[3]),2), ", ", round(exp(a$ci.ub[3]),2), ")", sep=""), paste(round(exp(a$beta[4,1]),2), " (", round(exp(a$ci.lb[4]),2), ", ", round(exp(a$ci.ub[4]),2), ")", sep=""), round(a$QMp,3))} a<-rma.mv(yi=yi, V=vi, data=es[es$E=="E",], mods = ~ Year+Age+Women) t1 <- rep(NA,5) t2 <- rep(NA,5) t<- data.frame(t1, t2) for (i in (c("E", "I", "O"))){ a<-rma.mv(yi=yi, V=vi, data=es[es$E==i,], mods = ~ Year+Age+Women) b<-tbl2(a) t<-cbind(t,b) } rownames(t)<-c("Intercept", "Year", "Age", "Sex", "Moderators' p-value") colnames(t)<-c("1", "2", "Education", "Income", "Occupation") t<-t[,3:5] write.csv2(t, "table2.csv") #effect of country and setting a<-rma.mv(yi=yi, V=vi, data=es[es$E=="O",], mods = ~ Year+Age+Women, random = ~ 1 | Country/Author) b<-rma.mv(yi=yi, V=vi, data=es[es$E=="O",], mods = ~ Year+Age+Women, random = ~ 1 | Country/Author, sigma2 = c(0, NA)) print(anova(b,a)) #cohort effect a<-rma.mv(yi=yi, V=vi, data=es[es$E=="O",], mods = ~ Age+Women+Country, random = ~ 1 | Year/Author) b<-rma.mv(yi=yi, V=vi, data=es[es$E=="O",], mods = ~ Age+Women+Country, random = ~ 1 | Year/Author, sigma2 = c(0, NA)) print(anova(b,a)) rma.mv(yi=yi, V=vi, data=es, mods = ~ Age+Women+Beginning+E) # race rma(yi=yi, vi=vi, data=es, mods = ~ Ethn) #pulication bias res <- rma(yi,vi, method = "ML", data=es) taf <- trimfill(res) funnel(taf, main="Funnel plot, social class indicators and the risk of dementia") a<-trimfill(rma.uni(yi,vi, method = "ML", data=es), side="left") b<-trimfill(rma.uni(yi,vi, method = "ML", data=es), side="right") predict(a, transf=exp, digits=2) predict(b, transf=exp, digits=2) # Forest plot dans les susgroupes 3 str(es) res <- rma(yi,vi,data=es, slab=paste(Author, ", ", E, " " ,Neib, sep="")) funtxt <-function(res) {paste ("RR: ", round(exp(res$b),2), "; p-value: ", round(res$pval, 3), "; tau2: ", round(res$tau2,2) , "; I2: ", round(res$I2,1),"%", sep="")} forest(res, order=order(es$E, es$yi), xlim=c(-4.5, 4), at=log(c(0.25, 0.5, 1, 3.5, 7)), ylim=c(-1, 63), atransf=exp, ilab.xpos=c(-9.5,-8,-6,-4.5), cex=0.75, rows=c(4:15, 20:28, 33:38, 43:52, 57:58), mlab=funtxt(res), psize=1, header="Author(s), year", main="Social class indicators and the risk of dementia") par(font=4) ### add text for the subgroups text(-4.55,c(16, 29, 39, 53,59), pos=4, c("Education", "Income", "Neighbourhood, aggregated", "Occupation", "Wealth"), cex=0.75) ### fit random-effects model in the subgroups res.e <- rma(yi,vi,data=es, subset=(E=="E"), slab=Author) res.i <- rma(yi,vi,data=es, subset=(E=="I"), slab=Author) res.o <- rma(yi,vi,data=es, subset=(E=="O"), slab=Author) res.n <- rma(yi,vi,data=es, subset=(E=="N"), slab=Author) res.w <- rma(yi,vi,data=es, subset=(E=="W"), slab=Author) ### add diamnats addpoly(res.e, row=2.5, cex=0.75, atransf=exp, mlab=funtxt(res.e), col="white") addpoly(res.i, row=18.5, cex=0.75, atransf=exp, mlab=funtxt(res.i), col="white") addpoly(res.o, row=31.5, cex=0.75, atransf=exp, mlab=funtxt(res.o), col="white") addpoly(res.n, row=41.5, cex=0.75, atransf=exp, mlab=funtxt(res.n), col="white") addpoly(res.w, row=55.5, cex=0.75, atransf=exp, mlab=funtxt(res.w), col="white") text(-4.5, -2, pos=4, cex=0.75, c("Risk ratios, reference group: the 'highest' class"))