forked from MattNolanLab/Inter_Intra_Variation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimulatedData.Rmd
257 lines (194 loc) · 10.3 KB
/
SimulatedData.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
---
title: "SimulatedNestedData"
author: "Matt Nolan"
date: "06/02/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
We want to simulate nested / hierarchical data and then use the simulated data to evaluate different model fitting strategies.
We want to simulate a measured parameter, param, that depends on position, pos. The 'real' relationship between param and pos is a line with intercept, int, and slope, slope.
When all subjects are identical the 'real' values of int and slope are the same for each subject.
When subjects differ, the 'real' values of int and slope are drawn from a distribution. In this case we'll use Gaussians with mean 1 and standard deviation, intersd_int and intersd_slope
For each simulated experiment, positions are drawn from a uniform random distribution seperately for each subject. The experimentally measured value of param is then estimted from a Gaussian distribution with mean equal to the real value of param for that location, and standard deviation equal determined from the intra-animal variance term, intrasd.
Set random number seed so figures are reproducible.
```{r}
set.seed(1)
```
First, a function to make the 'real' values for each subject.
```{r cars}
make_real_vals <- function(n_subjects, intersd_int = 1, intersd_slope = 1){
tb <- tibble(id = 1:n_subjects, int = rnorm(n_subjects, 1, intersd_int), slope = rnorm(n_subjects, 1, intersd_slope))
tb
}
```
Make groups with and without inter-subject variation.
```{r}
with_var_gp <- make_real_vals(20, 0.2, 0.2)
without_var_gp <- make_real_vals(20, 0, 0)
```
Next, a function to make an example dataset that might be obtained from the subjects. Returns a tidy formatted tibble.
```{r}
make_data <- function(subject_params, loc_n = 20, loc_max = 1, intrasd = 1) {
total_points <- c(loc_n * count(with_var_gp))[[1]]
tb <- tibble(id = rep(subject_params$id, loc_n),
loc = rep(runif(total_points, 0, loc_max)),
param = loc*subject_params$slope + subject_params$int + rnorm(total_points, 0, intrasd))
tb
}
```
Make and plot simulated data with and wihout inter-subject variation.
```{r}
var_gp_format <- function(gg) {
gg <- gg +
ylim(0,3) +
ylab("Feature") +
xlab("Location") +
theme(axis.text = element_blank())
}
with_var_gp_data <- make_data(with_var_gp, 20, 1, 0.2)
with_var_gp_gg <-ggplot(with_var_gp_data, aes(loc, param, group = id)) +
stat_smooth(geom = "line", method = lm, se = FALSE)
(with_var_gp_gg <- var_gp_format(with_var_gp_gg) +
theme(axis.title = element_text(size = 9), axis.title.x = element_text(vjust = 2)))
without_var_gp_data <- make_data(without_var_gp, 20, 1, 0.5)
without_var_gp_gg <- ggplot(without_var_gp_data, aes(loc, param, group = id)) +
stat_smooth(geom = "line", method = lm, se = FALSE) +
ylim(0,3)
(without_var_gp_gg <- var_gp_format(without_var_gp_gg) +
theme(axis.title = element_text(size = 9), axis.title.x = element_text(vjust = 2)))
```
Fit mixed effect models to the datasets.
```{r}
with_var_gp_mm <- lmer(param ~ loc + (loc||id), data = with_var_gp_data, REML = FALSE)
without_var_gp_mm <- lmer(param ~ loc + (loc||id), data = without_var_gp_data, REML = FALSE)
```
Extract marginal and conditional R2 for each mixed model fit.
```{r}
(with_R2 <- r.squaredGLMM(with_var_gp_mm))
(without_R2 <- r.squaredGLMM(without_var_gp_mm))
```
Plot fits
```{r}
mm_with_predictplot <- predict_plot_2(with_var_gp_data, with_var_gp_mm, with_var_gp_data$loc, with_var_gp_data$param)
(mm_with_predictplot <- var_gp_format(mm_with_predictplot) +
annotate("text", x = 0.6, y = 0.6, label = "paste(italic(R) ^ 2, \"marginal = .49\")", parse = TRUE, size = 2) +
annotate("text", x = 0.6, y = 0.2, label = "paste(italic(R) ^ 2, \"conditional = .73\")", parse = TRUE, size = 2) +
theme(axis.title = element_text(size = 9), axis.title.x = element_text(vjust = 2)))
mm_without_predictplot <- predict_plot_2(without_var_gp_data, without_var_gp_mm, without_var_gp_data$loc, without_var_gp_data$param)
(mm_without_predictplot <- var_gp_format(mm_without_predictplot) +
annotate("text", x = 0.6, y = 0.6, label = "paste(italic(R) ^ 2, \"marginal = .31\")", parse = TRUE, size = 2) +
annotate("text", x = 0.6, y = 0.2, label = "paste(italic(R) ^ 2, \"conditional = .31\")", parse = TRUE, size = 2) +
theme(axis.title = element_text(size = 9), axis.title.x = element_text(vjust = 2)))
```
Make revised version of mixed model figure. Requires that code in 'Props_mixed_model.Rmd' has been run.
Reformat plots of simulated data.
```{r}
without_var_gp_gg <- without_var_gp_gg +
labs(x = "") +
scale_x_continuous(labels = function(breaks) {rep_along(breaks, "")})
mm_without_predictplot <- mm_without_predictplot +
labs(x = "") +
scale_x_continuous(labels = function(breaks) {rep_along(breaks, "")})
```
Get conditional and marginal R2 values for mixed model fits to data.
```{r}
r.squaredGLMM(filter(data.sc_r, property == "rheo")$mm_vsris[[1]])
r.squaredGLMM(filter(data.sc_r, property == "resf")$mm_vsris[[1]])
```
Reformat plots of fitted data.
```{r warning=FALSE}
rheo_predictplot_mod <- rheo_predictplot +
theme(axis.title = element_text(size = 9), axis.text = element_text(size = 7), axis.title.x = element_text(vjust = 2.5))
mm_rheo_predictplot_mod <- mm_rheo_predictplot +
annotate("text", x = 1, y = 130, label = "paste(italic(R) ^ 2, \"marginal = .38\")", parse = TRUE, size = 2) +
annotate("text", x = 1, y = 50, label = "paste(italic(R) ^ 2, \"conditional = .65\")", parse = TRUE, size = 2) +
theme(axis.title = element_text(size = 9), axis.text = element_text(size = 7), axis.title.x = element_text(vjust = 2.5))
resf_predictplot_mod <- resf_predictplot +
theme(axis.title = element_text(size = 9), axis.text = element_text(size = 7), axis.title.x = element_text(vjust = 2.5)) +
labs(x = "") +
scale_x_continuous(labels = function(breaks) {rep_along(breaks, "")})
mm_resf_predictplot_mod <- mm_resf_predictplot +
annotate("text", x = 1, y = 3, label = "paste(italic(R) ^ 2, \"marginal = .12\")", parse = TRUE, size = 2) +
annotate("text", x = 1, y = 1, label = "paste(italic(R) ^ 2, \"conditional = .55\")", parse = TRUE, size = 2) +
theme(axis.title = element_text(size = 9), axis.text = element_text(size = 7), axis.title.x = element_text(vjust = 2.5)) +
labs(x = "") +
scale_x_continuous(labels = function(breaks) {rep_along(breaks, "")})
IS_figure_mod <- IS_figure + theme(legend.position = c(0.5, -0.1), plot.margin = margin(0, 0, 1, 0, "cm"), legend.direction = "horizontal")
```
Make the figure
```{r}
pw <- 0.18
ph <- 0.26
dh <- 0.26
dw <- 0.18
ggdraw() +
draw_plot(with_var_gp_gg, x = 0, y = .47, width = pw, height = ph) +
draw_plot(without_var_gp_gg, x = 0, y = 0.67, width = pw, height = ph) +
draw_plot(mm_with_predictplot, x = 0.2, y = .47, width = pw, height = ph) +
draw_plot(mm_without_predictplot, x = 0.2, y = 0.67, width = pw, height = ph) +
draw_plot(resf_predictplot_mod, x = 0, y = .21, width = dw, height = dh) +
draw_plot(mm_resf_predictplot_mod, x = 0.2, y = .21, width = dw, height = dh) +
draw_plot(rheo_predictplot_mod, x = 0, y = 0, width = dw, height = dh) +
draw_plot(mm_rheo_predictplot_mod, x = 0.2, y = .0, width = dw, height = dh) +
draw_plot(IS_figure_mod, x = 0.42, y = 0, width = .58, height = 1) +
draw_plot_label(label = c("A", "B", "C"), size = 15,
x = c(0, 0, 0.4), y = c(1, 0.52, 1)) +
draw_plot_label(label = c("Independent fits", "Mixed model fits"), size = 9,
x = c(0, 0.18), y = c(0.98, 0.98)) +
draw_plot_label(label = c("Simulated without inter-animal variation", "Simulated with inter-animal variation", "Experimental"), x = c(0, 0.02, 0.1), y = c(0.94, 0.74, 0.50), size = 7)
```
Make a version with example data: rheo_example and resf_example.
```{r}
rheo_example_mod <- rheo_example +
theme(axis.title = element_text(size = 7),
axis.text = element_text(size = 7),
axis.title.x = element_text(vjust = 2.5),
legend.text = element_text(size = 5),
legend.title=element_text(size=5),
legend.key.size = unit(0.2, "cm"),
legend.position = "right",
legend.key = element_rect(fill = "white", colour = "black")) +
scale_colour_manual(labels = c("", ""), values = c("red", "blue"))
resf_example <- resf_example +
theme(axis.title = element_text(size = 7), axis.text = element_text(size = 7), axis.title.x = element_text(vjust = 2.5))
mm_with_predictplot <- mm_with_predictplot +
labs(y = "")
mm_without_predictplot <- mm_without_predictplot +
labs(x = "", y = "")
mm_rheo_predictplot_mod <- mm_rheo_predictplot_mod +
labs(y = "")
ew <- 0.35
eh <- 0.3
pw <- 0.18
ph <- 0.24
dh <- 0.24
dw <- 0.18
pdx1 <- 0.03
pdx2 <- 0.22
ggdraw() +
draw_plot(rheo_example_mod, x = 0.03, y = .68, width = ew, height = eh) +
draw_plot(rheo_predictplot_mod, x = pdx1, y = 0, width = dw, height = dh) +
draw_plot(mm_rheo_predictplot_mod, x = pdx2, y = .0, width = dw, height = dh) +
draw_plot(with_var_gp_gg, x = pdx1, y = .23, width = pw, height = ph) +
draw_plot(without_var_gp_gg, x = pdx1, y = 0.43, width = pw, height = ph) +
draw_plot(mm_with_predictplot, x = pdx2, y = .23, width = pw, height = ph) +
draw_plot(mm_without_predictplot, x = pdx2, y = 0.43, width = pw, height = ph) +
draw_plot(IS_figure_mod, x = 0.42, y = 0, width = .58, height = 1) +
draw_plot_label(label = c("A", "B", "C", "D", "E"), size = 15,
x = c(0, 0, 0, 0, 0.4), y = c(1, 0.73, 0.49, 0.28, 1)) +
draw_plot_label(label = c("Independent fits", "Mixed model fits"), size = 9,
x = c(0.02, 0.22), y = c(0.72, 0.72)) +
draw_plot_label(label = c("Simulated without inter-animal variation", "Simulated with inter-animal variation", "Experimental"), x = c(0, 0.02, 0.1), y = c(0.68, 0.48, 0.26), size = 7)
```
Save the figure.
```{r Save plot of model fits}
ggsave("Figures/I_S_figure.png", width = 220, height = 120, units = "mm")
```
Try a different strategy to make the figure.
```{r}
examples <- plot_grid(without_var_gp_gg, mm_without_predictplot, with_var_gp_gg, mm_with_predictplot, resf_predictplot_mod, mm_resf_predictplot_mod, rheo_predictplot_mod, mm_rheo_predictplot_mod, labels = c("A", "", "", "", "B", "", "", "", ""), nrow = 4, ncol = 2, rel_widths = c(1,1,4))
all_fits <- plot_grid(IS_figure_mod, labels = "C")
plot_grid(examples, all_fits, rel_widths = c(2,4))
```