-
Notifications
You must be signed in to change notification settings - Fork 1
/
simulation 2018 07 02 appendix.rmd
85 lines (62 loc) · 1.43 KB
/
simulation 2018 07 02 appendix.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
80
81
82
83
84
---
title: "Appendix - Simulation experiment mediation analysis"
author: "Elmar W. Tobi, Erik W. van Zwet, L.H. Lumey, Bastiaan T. Heijmans"
date: "July 2, 2018"
output:
html_document:
toc: true
---
Set options.
```{r set-options}
options(width = 200)
```
# Introduction
Simulation.
```{r, echo = TRUE, message=FALSE,warning=FALSE}
setwd("M:\\projects\\Tobi\\mediation artikel\\rebuttal")
set.seed(123) # fix seed of RNG for reproducibility
library(bda) # for Sobel test
n1=350
n2=650
n=n1+n2
Famine = c(rep(0,n1),rep(1,n2)) # Exposure to Famine
K=1000 # number of simulations
```
# Scenario Mediation
![](5-PartialMediation.png){width=50%}
\bigskip
```{r, echo = TRUE, message=FALSE,warning=FALSE}
a=0.4
b=0.1
c=0.2
pval=numeric(K)
for (ind in 1:K){
DNAm = a * Famine + rnorm(n,0,1)
Health = c * Famine + b *DNAm + rnorm(n,0,1)
fit = mediation.test(mv=DNAm,iv=Famine,dv=Health)
pval[ind] = fit[2,1]
}
power = mean(pval<0.05)
power
```
# Scenario Reverse Causation
![](6-PartialReverseCausation.png){width=50%}
\bigskip
```{r, echo = TRUE, message=FALSE,warning=FALSE}
d=0.3
e=0.1
f=0.1
pval=numeric(K)
for (ind in 1:K){
Health = d * Famine + rnorm(n,0,1)
DNAm = f * Famine + e * Health + rnorm(n,0,1)
fit = mediation.test(mv=DNAm,iv=Famine,dv=Health)
pval[ind] = fit[2,1]
}
power = mean(pval<0.05)
power
```
# Session information
```{r, warning=FALSE, message=FALSE}
sessionInfo()
```