---
title: "Meta-regressions and sub-group meta-analyses of substance use moderators"
output:
  html_document:
    toc: true
    toc_depth: 4
    number_sections: true
    toc_float: 
      collapsed: false
date: "`r format(Sys.time(), '%d %B %Y')`"

author:
- "**Code:** Andrew W Brown, PhD^[Indiana University - Bloomington]"
- "Sara Lappan, PhD^[University of Alabama at Birmingham]"
- "Peter Hendricks, PhD^[University of Alabama at Birmingham]"
---
```{r eval=TRUE, echo=long, results='asis'}
substance <- Substance_dic[match(sx,Substance_dic),2] #get readable version of varname
if(is.na(substance)) {
  substance <- substance <- paste0("**Substance ",sx,"**") #if it does not exist, revert to variable name
} else {
  substance <- paste0("**",substance,"**")
}

pandoc.header(substance, level = 2)
```

```{r echo=long, eval=TRUE, include=TRUE, results='asis'}

if(sx==1){ #alcohol
  pandoc.header("Drinks per Day", level=3)
  #run separate model for alcohol drinks per day, which is unique to alcohol
  alcohol.df <- subclean.df[!is.na(subclean.df$Alcohol_drinks_per_day),]
  alcohol.out <- plo.rma.mv(alcohol.df, "Alcohol_drinks_per_day",
                            continuous = TRUE,
                            profile = profile,
                            long = long)
  sx.table <- rbind(sx.table, t(c(substance,rep("",7)))) #print substance to table
  sx.table <- rbind(sx.table, cont.table(alcohol.out, "Drinks per Day")) #add alcohol to table
}

if(sx==2){
  pandoc.header("Cigarettes per Day", level=3)
  #run separate model for tobacco frequency per day, which is unique to tobacco
  tobacco.df <- subclean.df[!is.na(subclean.df$Freq_tobacco_per_day),]
  tobacco.out <- plo.rma.mv(tobacco.df, "Freq_tobacco_per_day", 
                            continuous = TRUE,
                            profile = profile,
                            long = long)
  sx.table <- rbind(sx.table, t(c(substance,rep("",7)))) #print substance to table
  sx.table <- rbind(sx.table, cont.table(tobacco.out, "Cigarettes per Day")) #add tobacco to table
}

#print the substance to the output table
if(!(sx %in% c(1,2))) sx.table <- rbind(sx.table, t(c(substance,rep("",7))))
```

```{r echo=long, eval=TRUE, include=TRUE}
if(sx == 1){
  print(alcohol.out$model)
cat(paste("\nICC:", fmt4(alcohol.out$ICC)))
cat(paste("\nI^2.1", fmt1(alcohol.out$I2[1])))
cat(paste("\nI^2.2", fmt1(alcohol.out$I2[2])))
  kable(alcohol.out$prediction) %>% 
    add_header_above(header = c("Alcohol: Drinks per Day" = 7), align = 'l')%>%
  kable_styling(full_width = F)
}
```

```{r eval=TRUE, echo=long, results='asis'}
if(plots & sx == 1){
  plo.reg(alcohol.out, "Alcohol_drinks_per_day", "Alcohol: Drinks per Day")
  
  cat("<p>**Figure:** Meta-regression for 'Alcohol Drinks per Day'. The solid line represents the predicted dropout rate at the given predictor value; dashed lines represent the 95% CI; dotted lines represent the 95% Prediction Interval. Points are proportional to the inverse variance of the individual dropout rates.</p>")
}
```

```{r echo=long, eval=TRUE, include=TRUE}
if(sx == 2){
  print(tobacco.out$model)
cat(paste("\nICC:", fmt4(tobacco.out$ICC)))
cat(paste("\nI^2.1", fmt1(tobacco.out$I2[1])))
cat(paste("\nI^2.2", fmt1(tobacco.out$I2[2])))
  kable(tobacco.out$prediction) %>% 
    add_header_above(header = c("Tobacco: Cigarettes per Day" = 7), align = 'l')%>%
  kable_styling(full_width = F)
}
```

```{r eval=TRUE, echo=FALSE, include=FALSE, results='asis'}
if(plots & sx == 2){
  plo.reg(tobacco.out, "Freq_tobacco_per_day")
  cat("<p>**Figure:** meta-regression for Cigarettes per Day. The solid line represents the predicted dropout rate at the given predictor value; dashed lines represent the 95% CI; dotted lines represent the 95% Prediction Interval. Points are proportional to the inverse variance of the individual dropout rates.</p>")
}
```

###Frequency of Use (% days)

```{r eval=TRUE, echo=long, include=TRUE}
sub.out <- suppressMessages(
  tryCatch(plo.rma.mv(df = substance.df,
                      varname = "Freq_._of_days_used",
                      continuous = TRUE,
                      title = paste0(substance, ": Frequency of Use (% days)"),
                      profile = profile,
                      long = long),
           warning = function(w) return(w$message),
           error = function(e) e)
)
assign(paste0("sub.days",sx,".out"), sub.out)

if(class(sub.out) == "character"){
  sx.table <- rbind(sx.table, t(c("Frequency of Use (% days)",sub.out,rep("",6)))) #report error in table
  print(sub.out)
} else {
  sx.table <- rbind(sx.table, cont.table(sub.out, "Frequency of Use (% days)"))
  print(sub.out$model)
cat(paste("\nICC:", fmt4(sub.out$ICC)))
cat(paste("\nI^2.1", fmt1(sub.out$I2[1])))
cat(paste("\nI^2.2", fmt1(sub.out$I2[2])))
  header <- 7
  names(header) <- paste0(substance, ": Frequency of Use (% days)")
  kable(sub.out$prediction) %>% 
    add_header_above(header = header, align = 'l')%>%
  kable_styling(full_width = F)
}
```

```{r eval=TRUE, echo=FALSE, results='asis'}
if(plots & class(sub.out)!= "character"){
  plo.reg(sub.out,"Freq_._of_days_used", "Frequency of Use (% days)")
 cat("<p>**Figure:** meta-regression for ",substance," Frequency of Use (% days). The solid line represents the predicted dropout rate at the given predictor value; dashed lines represent the 95% CI; dotted lines represent the 95% Prediction Interval. Points are proportional to the inverse variance of the individual dropout rates.</p>")
}
```

###Length of Use (years)

```{r echo=long, eval=TRUE, include=TRUE}
sub.out <- suppressMessages(
  tryCatch(plo.rma.mv(df = substance.df,
                      varname = "Length_use_years",
                      continuous = TRUE,
                      title = paste0(substance, ": Length of Use (years)"),
                      profile = profile,
                      long = long),
           warning = function(w) return(w$message),
           error = function(e) e)
)
assign(paste0("sub.years",sx,".out"), sub.out)
if(class(sub.out) == "character"){
  sx.table <- rbind(sx.table, t(c("Length of Use (years)",sub.out,rep("",6)))) #report error in table
  print(sub.out)
} else {
  sx.table <- rbind(sx.table, cont.table(sub.out, "Length of Use (years)"))
  print(sub.out$model)
cat(paste("\nICC:", fmt4(sub.out$ICC)))
cat(paste("\nI^2.1", fmt1(sub.out$I2[1])))
cat(paste("\nI^2.2", fmt1(sub.out$I2[2])))
  header <- 7
  names(header) <- paste0(substance, ": Length of Use (years)")
  kable(sub.out$prediction) %>% 
    add_header_above(header = header, align = 'l')%>%
  kable_styling(full_width = F)
}
```

```{r eval=TRUE, echo = FALSE, results='asis'}
if(plots & class(sub.out)!= "character"){
  plo.reg(sub.out,"Length_use_years", "Length of Use (years)")
  cat("<p>**Figure:** meta-regression for ",substance," Length of Use (years). The solid line represents the predicted dropout rate at the given predictor value; dashed lines represent the 95% CI; dotted lines represent the 95% Prediction Interval. Points are proportional to the inverse variance of the individual dropout rates.</p>")
}
```

###Use Pattern Degree

```{r echo=long, eval=TRUE, include=TRUE}
sub.out <- suppressMessages(
  tryCatch(plo.rma.mv(df = substance.df,
                      varname = "Use_Pat_Deg",
                      continuous = FALSE,
                      title = paste0(substance, ": Degree of use pattern"),
                      profile = profile,
                      long = long),
           warning = function(w) return(w$message),
           error = function(e) e)
)
assign(paste0("sub.deg",sx,".out"), sub.out)
if(class(sub.out) == "character"){
  sub.deg.table <- rbind(sub.deg.table, c(substance,sub.out,"","","","")) #report error in table
  print(sub.out)
} else {
  sub.deg.table <- rbind(sub.deg.table, cat.table(sub.out, substance))
  print(sub.out$model)
cat(paste("\nICC:", fmt4(sub.out$ICC)))
cat(paste("\nI^2.1", fmt1(sub.out$I2[1])))
cat(paste("\nI^2.2", fmt1(sub.out$I2[2])))
  header <- 8
  names(header) <- paste0(substance, ": Degree of use pattern")
  kable(sub.out$prediction) %>% 
    add_header_above(header = header, align = 'l')%>%
  kable_styling(full_width = F)
}
```

```{r eval=TRUE, echo=FALSE, results='asis'}
if(plots & class(sub.out)!= "character"){
  plo.forest(out = sub.out
                       ,variable = "Use_Pat_Deg"
  )

  cat("<p>**Figure:** subgroup meta-analysis for ",substance," Degree of Use Pattern. The dashed line represents the mean dropout rate across all dropout rates included in this particular analysis. Because the inclusion of studies and study arms is dependent on the reporting of the moderator, the overall dropout rate in this analysis may be different than in other analyses. Red polygons represent the mean and 95% CI for the average dropout. Note that 95% Prediction Interval are not included.</p>")
}
```
