---
title: "Implementation of extended formulas when there are effect measure modifiers"
author: "Yi Li, Kazuki Yoshida"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Implementation of extended formulas when there are effect measure modifiers}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r, message = FALSE, tidy = FALSE, echo = F}
## knitr configuration: http://yihui.name/knitr/options#chunk_options
library(knitr)
showMessage <- FALSE
showWarning <- TRUE
set_alias(w = "fig.width", h = "fig.height", res = "results")
opts_chunk$set(comment = "##", error= TRUE, warning = showWarning, message = showMessage,
               tidy = FALSE, cache = FALSE, echo = TRUE,
               fig.width = 7, fig.height = 7,
               fig.path = "man/figures")
```

In this document, we demonstrate including effect measure modification (EMM) terms in the mediator or the outcome models. The dataset used in this document is still `vv2015`.

```{r}
library(regmedint)
library(tidyverse)
## Prepare dataset
data(vv2015)
```

# No EMM by covariates

In the first model fit, we do not include any EMM term.
```{r}
regmedint_obj1 <- regmedint(data = vv2015,
                            ## Variables
                            yvar = "y",
                            avar = "x",
                            mvar = "m",
                            cvar = c("c"),
                            eventvar = "event",
                            ## Values at which effects are evaluated
                            a0 = 0,
                            a1 = 1,
                            m_cde = 1,
                            c_cond = 3,
                            ## Model types
                            mreg = "logistic",
                            yreg = "survAFT_weibull",
                            ## Additional specification
                            interaction = TRUE,
                            casecontrol = FALSE)
summary(regmedint_obj1)
```



# EMM by covariates

## There is $A\times C$ term in mediator model
Now suppose the covariate $C$ modifies the treatment effect on the mediator. We add `emm_ac_mreg = c("c")` in `regmedint()`. Although there is only one covariate in our dataset, `emm_ac_mreg` can take a vector of multiple covariates. Please note that the covariates in `emm_ac_mreg` should be a subset of the covariates specified in `cvar`, i.e. if a covariate is an effect measure modifier included in `emm_ac_mreg`, it must be included in `cvar`, otherwise an error message will be printed.

```{r}
regmedint_obj2 <- regmedint(data = vv2015,
                            ## Variables
                            yvar = "y",
                            avar = "x",
                            mvar = "m",
                            cvar = c("c"),
                            emm_ac_mreg = c("c"),
                            emm_ac_yreg = NULL,
                            emm_mc_yreg = NULL,
                            eventvar = "event",
                            ## Values at which effects are evaluated
                            a0 = 0,
                            a1 = 1,
                            m_cde = 1,
                            c_cond = 3,
                            ## Model types
                            mreg = "logistic",
                            yreg = "survAFT_weibull",
                            ## Additional specification
                            interaction = TRUE,
                            casecontrol = FALSE)
summary(regmedint_obj2)
```




## There is $A\times C$ term in both mediator and outcome models
Now suppose in addition to the EMM on mediator, the covariate $C$ also modifies the treatment effect on the outcome We add `emm_ac_yreg = c("c")` in `regmedint()`.  Please note that the covariates in `emm_ac_yreg` should be a subset of the covariates specified in `cvar`, i.e. if a covariate is an effect measure modifier included in `emm_ac_yreg`, it must be included in `cvar`, otherwise an error message will be printed.
```{r}
regmedint_obj3 <- regmedint(data = vv2015,
                            ## Variables
                            yvar = "y",
                            avar = "x",
                            mvar = "m",
                            cvar = c("c"),
                            emm_ac_mreg = c("c"),
                            emm_ac_yreg = c("c"),
                            emm_mc_yreg = NULL,
                            eventvar = "event",
                            ## Values at which effects are evaluated
                            a0 = 0,
                            a1 = 1,
                            m_cde = 1,
                            c_cond = 3,
                            ## Model types
                            mreg = "logistic",
                            yreg = "survAFT_weibull",
                            ## Additional specification
                            interaction = TRUE,
                            casecontrol = FALSE)
summary(regmedint_obj3)
```




## There are $A\times C$ term in both mediator and outcome models, and $M\times C$ term in outcome model
Now suppose in addition to the EMM of treatment effect, the covariate $C$ also modifies the mediator effect on the outcome. We add `emm_mc_yreg = c("c")` in `regmedint()`.  Please note that the covariates in `emm_mc_yreg` should be a subset of the covariates specified in `cvar`, i.e. if a covariate is an effect measure modifier included in `emm_mc_yreg`, it must be included in `cvar`, otherwise an error message will be printed.
```{r}
regmedint_obj4 <- regmedint(data = vv2015,
                            ## Variables
                            yvar = "y",
                            avar = "x",
                            mvar = "m",
                            cvar = c("c"),
                            emm_ac_mreg = c("c"),
                            emm_ac_yreg = c("c"),
                            emm_mc_yreg = c("c"),
                            eventvar = "event",
                            ## Values at which effects are evaluated
                            a0 = 0,
                            a1 = 1,
                            m_cde = 1,
                            c_cond = 3,
                            ## Model types
                            mreg = "logistic",
                            yreg = "survAFT_weibull",
                            ## Additional specification
                            interaction = TRUE,
                            casecontrol = FALSE)
summary(regmedint_obj4)
```
