-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulation.Rmd
79 lines (57 loc) · 2.58 KB
/
Simulation.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
---
title: "Explanation of the R code"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(message = FALSE)
knitr::opts_chunk$set(warning = FALSE)
```
```{r}
source("R/dgm.r")
source("R/vis.r")
source("R/imp.r")
source("R/sim.r")
```
We first need to set up the data-generating mechanism to enable the generation of a non-randomized dataset with two treatments. By default, a total of 61 visits will be generated for each patient, with a frequency of one visit per month. The outcome at each visit (EDSS) is determined by the received treatment, the total treatment exposure time, and the age at baseline. In addition, random variation is added to the EDSS scores, with clustering by patient ID and by center ID.
```{r}
simpars <- setup(delta_xt = -0.007)
```
The configuration above ensures that the active treatment will reduce deterioration in EDSS by 0.007 points each day (as compared to the control treatment). We can now simulate a dataset:
```{r}
dat <- sim_data(simpars)
head(dat)
```
So far, the simulated dataset does not contain any missing visits. We can introduce irregular visit patterns by informatively censoring certain vists from `dat`. Eight different informative censoring functions are available from `R/dgm.r`. We will consider `censor_visits_7()`, which introduces the following visit pattern:
```{r}
plot_md_pattern(simpars, censor_visits_7)
```
We can apply the informative visit process as follows:
```{r}
misdat <- censor_visits_7(dat)
subset(misdat, patid == 1)
```
The observed EDSS trajectory of patient with `patid=1` is then as follows:
```{r}
plot_example_trajectory(misdat, sel_patid = 1)
```
We can also visualize the age distribution across the treatment groups:
```{r}
plot_dens_x(misdat, x_var = "age", x_label = "Age")
```
We can visualize the total follow-up for each center:
```{r}
plot_max_fup(misdat)
```
We can now generate an imputed dataset using the `mice` software package. We will only generate imputed values on a 3-month grid.
```{r}
imptimes <- seq(0, 60, by = 3)
imp <- impute(misdat, times = imptimes, maxit = 1)
# Visualize the imputed trajectory of one patient
plot_imputed_trajectories(imp, sel_patid = 1)
```
Finally, we can run a simulation study to assess the ability of the imputation model to recover the missing EDSS scores and to estimate the treatment effect. Since no Gibbs sampler is needed in this case, we implemented a faster version that bypassess the need of MICE and generates 10 imputed datasets simultaneously.
```{r}
sim <- run_sim(simpars, censor_visits_7, seed = 101)
sim
```