forked from MattNolanLab/Inter_Intra_Variation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCA_mixed_model.Rmd
More file actions
488 lines (353 loc) · 17.1 KB
/
PCA_mixed_model.Rmd
File metadata and controls
488 lines (353 loc) · 17.1 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
---
title: "R Notebook"
output: html_notebook
---
```{r setup_Props_PCA, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Partial correlation analysis
Goal is to evaluate condidtional independence of electropohysiological features.
First plot partial correlations for all measured features.
```{r}
data.sc_neurons <- data.sc %>% dplyr::select(vm:fi) %>%
na.omit
Q_neurons <- calcQ(data.sc_neurons)
Q_neurons <- as.matrix(Q_neurons)
colnames(Q_neurons) <- colnames(data.sc_neurons)
rownames(Q_neurons) <- colnames(data.sc_neurons)
order_names <- c("vm", "ir", "sag", "tau", "resf", "resmag", "rheo", "fi", "ahp", "spkmax", "spkthr", "spkhlf")
Q_neurons <- Q_neurons[match(order_names, rownames(Q_neurons)), match(order_names, colnames(Q_neurons))]
new_names <- c("Vm", "IR", "Sag", "Tm", "ResF", "ResM", "Rheo", "F-I", "AHP", "Smax", "Sthr", "SHW")
colnames(Q_neurons) <- new_names
rownames(Q_neurons) <- new_names
(Q_neurons_plot <- ggcorr(data = NULL, cor_matrix = Q_neurons, geom = "circle", min_size = 0, max_size = 5, legend.size = 7, size = 2))
```
Next plot partical correlations for intercepts of mixed model fits.
```{r}
data_summary.mm_vsris <- prep_int_slopes_PCA(data.sc_r, "property", "mm_vsris")
data_intercepts <- spread(data_summary.mm_vsris[,1:3], property, ind_intercept)
data_slopes <- spread(data_summary.mm_vsris[,c(1:2, 4)], property, ind_slope)
data.sc_fits <- data_intercepts %>% dplyr::select(ahp:vm) %>%
na.omit
Q_intercepts <- calcQ(data.sc_fits)
Q_intercepts <- as.matrix(Q_intercepts)
colnames(Q_intercepts) <- colnames(data.sc_fits)
rownames(Q_intercepts) <- colnames(data.sc_fits)
Q_intercepts <- Q_intercepts[match(order_names, rownames(Q_intercepts)), match(order_names, colnames(Q_intercepts))]
colnames(Q_intercepts) <- new_names
rownames(Q_intercepts) <- new_names
(Q_intercepts_plot <- ggcorr(data = NULL, cor_matrix = Q_intercepts, geom = "circle", min_size = 0, max_size = 5, legend.size = 7, size = 2))
```
# PCA mixed model analysis
Goal is to reduce the dimensionality of the dataset and then explore how dorsoventral location and mouse identity map onto each dimension by evaluating linear models generated using the principal components.
Carry out PCA. The fi measurements are absent from a lot of cells. For a first analysis include all columns but remove cells without fi measurments. Focus analysis on ages above P32 and below P45, and on data collected by HP.
```{r}
data.pca <- dplyr::select(data.sc, vm:fi, dvlocmm, id, housing, id, mlpos, hemi, age, housing, expr, patchdir, rectime) %>%
filter(age > 32 & age <45 & expr == "HP")
all.pca <- prcomp(drop_na(data.pca[1:12], fi),
retx = TRUE,
centre = TRUE,
scale = TRUE)
plot(all.pca)
summary(all.pca)
prop.var.df <- as.data.frame(summary(all.pca)$importance[2,])
colnames(prop.var.df) <- c("PropVar")
prop.var.df$components <- 1:12
(all.pca.prop.var.plot <- ggplot(prop.var.df, aes(components, PropVar)) +
geom_bar(stat = "identity") +
scale_x_discrete("Component") +
ylab("Proportion of variance") +
theme(title = element_text(size = 9), axis.text = element_text(size = 9)))
(all.pca.biplot <- pca_biplot(as.data.frame(all.pca$rotation), fontsize = 2, order_names = order_names, new_names = c("Vm", "IR", "Sag", "Tm", "Res. F", "Res. Mag.", "Rheo", "FI", "AHPmax", "Spk. max", "Spk. thr", "Spk. HW")) +
theme(text = element_text(size = 9)) +
xlim(-0.5, 0.55))
```
In second analysis exclude the fi column and so include more cells.
```{r}
sub.pca <- prcomp(data.pca[1:11],
retx = TRUE,
centre = TRUE,
scale = TRUE)
plot(sub.pca)
summary(sub.pca)
(sub.pca.biplot <- pca_biplot(as.data.frame(sub.pca$rotation)))
```
## View relationships between principal components
```{r Prepare data for fitting model to principal components}
all.pca.x <- bind_cols(as_tibble(all.pca$x), drop_na(data.pca, fi))
sub.pca.x <- bind_cols(as_tibble(sub.pca$x), data.pca)
```
```{r Plot principal components versus location}
# For alternative analysis could replace all.pca.x with sub.pca.x
out.pca.x_g1_11 <- all.pca.x %>%
gather("component", "value", 1:11)
pc_plot <- ggplot(data = out.pca.x_g1_11, aes(x = dvlocmm, y = value)) +
geom_point() +
facet_wrap(~ component)
out.pca.x_g1_5 <- all.pca.x %>%
gather("component", "value", 1:5)
pc1to5_plot <-ggplot(data = out.pca.x_g1_5, aes(x = dvlocmm, y = value, colour = housing)) +
geom_point(alpha = 0.5) +
facet_wrap(~ component, ncol = 5) +
scale_x_continuous("DV location (mm)", c(0,1,2)) +
theme_classic() +
PCA_theme +
theme(legend.position="bottom")
pc_plot
pc1to5_plot
```
## Fit mixed models to principal components
Fit mixed models to all measured properties using lmer.
```{r}
# Reform data for use with dplyr.
out.pca.x_g <- all.pca.x %>%
gather("component", "value", 1:12) %>%
group_by(component) %>%
nest()
out.pca.x_g <- out.pca.x_g %>%
mutate(mixedmodel_vsris = map(data, model_vsris)) %>%
mutate(mixedmodel_vsris_null = map(data, model_vsris_null)) %>%
mutate(mixedmodel_vsri = map(data, model_vsri)) %>%
mutate(mixedmodel_vsri_null = map(data, model_vsri_null))
```
## Compare fits of mixed models to one another and to population level linear model
Extract AIC for all models
```{r Extract AIC for PCA models}
out.pca.x_g <- out.pca.x_g %>%
mutate(vsris_glance = map(mixedmodel_vsris, broom::glance)) %>%
mutate(vsris_null_glance = map(mixedmodel_vsris_null, broom::glance)) %>%
mutate(vsri_glance = map(mixedmodel_vsri, broom::glance)) %>%
mutate(AIC_vsris = map_dbl(vsris_glance, ~.$AIC)) %>%
mutate(AIC_vsris_null = map_dbl(vsris_null_glance, ~.$AIC)) %>%
mutate(AIC_vsri = map_dbl(vsri_glance, ~.$AIC))
```
Test whether effects of animal id are significant.
```{r Compare mixed with linear PCA models using chisq}
## linearmodel_to_fit fits: lm(value ~ dvlocmm, data = df, na.action = na.exclude)
out.pca.x_g <- out.pca.x_g %>%
mutate(linearmodel = map(data, linearmodel_to_fit))
out.pca.x_g <- bind_cols(out.pca.x_g, mixed_vs_linear_pchisqu(out.pca.x_g, mixedmodel_vsris, linearmodel))
out.pca.x_g$mixedmodel_vsris_vslinear_pdiff_adj <- p.adjust(out.pca.x_g$mixedmodel_vsris_vslinear_pdiff, method = "BH")
table_mixedvslinear(out.pca.x_g, "mixedmodel_vsris_vslinear", "component")
```
## Extract other summary data from the model fits
Focus on the model with random intercept and slope (mixedmodel_vsris).
Store model gradient (extracted with summary / glance), marginal and conditional R2 (extracted with r.squaredGLMM) and p-value vs null model (calculated with ANOVA vs null model). Also extract model slopes.
```{r Extract model properties for PCA, warning=FALSE}
out.pca.x_g <- mixedmod_extract(out.pca.x_g, mixedmodel_vsris)
out.pca.x_g <- out.pca.x_g %>%
mutate(anova = map2(mixedmodel_vsris, mixedmodel_vsris_null, ~anova(.x,.y))) %>%
mutate(tidy_anova = map(anova, broom::tidy)) %>%
mutate(anova_p_val = map_dbl(tidy_anova, ~.$p.value[2])) %>%
mutate(anova_p_val_adj = p.adjust(anova_p_val, method = "BH"))
```
## Generate summary table for PCA
Show model fitting results as a table.
```{r Make and save table with PCA mixed model properties}
props_for_table_PCA <- c("component", "mixedmodel_vsris_gradient_slopes", "anova_p_val_adj", "mixedmodel_vsris_marginal.r2", "mixedmodel_vsris_conditional.r2", "mixedmodel_vsris_slope_min", "mixedmodel_vsris_slope_max", "mixedmodel_vsris_vslinear_pdiff_adj")
props_table_PCA <- as.tibble(out.pca.x_g[props_for_table_PCA])
props_table_PCA$anova_p_val_adj <- format(props_table_PCA$anova_p_val_adj, digits = 3)
props_table_PCA$mixedmodel_vsris_vslinear_pdiff_adj <- format(props_table_PCA$mixedmodel_vsris_vslinear_pdiff_adj, digits = 3)
(props_table_unnest_PCA <- unnest(props_table_PCA) %>%
knitr::kable(
digits = 5,
col.names = c("Feature", "Slope", "p (slope)", "Marginal R2", "Conditional R2", "Slope (min)", "Slope (max)", "p (vs linear)")
) %>%
kableExtra::kable_styling(bootstrap_options = c("striped", "hover")))
table_path <- paste0(getwd(), "/Tables/props_PCA")
if (stajpeg == 1) {
save_kable_jpg_html(props_table_unnest_PCA, table_path)
} else {props_table_unnest_PCA %>% kableExtra::save_kable(file = paste0(table_path, ".html"), self_contained = T)}
```
## Plot fits of mixed models of PCA data
As above, we want to plot for each model the prediction at location = 0 for each animal (I), the model prediction for location = 1 mm (I + S) and a line indicating the slope with start centred at the value of the population level model at location = 0.
Extract model predictions ready to generate plots.
```{r Format PCA mixed model fits ready for plotting, warning=FALSE}
combined_intercepts_slopes_PCA <- prep_int_slopes(out.pca.x_g, "component", "mixedmodel_vsris")
id_housing_PCA <- distinct(all.pca.x, id, housing)
combined_intercepts_slopes_PCA <- left_join(combined_intercepts_slopes_PCA, id_housing_PCA, by = "id")
combined_intercepts_slopes_PCA$component_factors <- as.factor(combined_intercepts_slopes_PCA$component)
combined_intercepts_slopes_PCA$component_factors = factor(combined_intercepts_slopes_PCA$component_factors, c("PC1", "PC2", "PC3", "PC4", "PC5", "PC6", "PC7", "PC8", "PC9", "PC10", "PC11"))
```
Now generate the plot.
```{r Make facetted plot of PCA model fits}
IS_figure_PCA_1_11 <- ggplot(combined_intercepts_slopes_PCA, aes(x = measure, y = value_1, colour = housing)) +
geom_line(aes(group = id)) +
geom_jitter(aes(y = value_2), width = 0.2) +
scale_x_discrete(limits = c("ind_intercept", "ind_intercept_slope", "global_intercept", "global_intercept_slope"), label = c("I", "I + S", "", "")) +
facet_wrap(~component_factors) +
theme_classic() +
hist_theme +
theme(axis.line.x = element_blank(), axis.ticks.x = element_blank())
IS_figure_PCA_1_5 <- ggplot(subset(combined_intercepts_slopes_PCA, component %in% c("PC1", "PC2", "PC3", "PC4", "PC5")), aes(x = measure, y = value_1, colour = housing)) +
geom_line(aes(group = id)) +
geom_jitter(aes(y = value_2), width = 0.2) +
scale_x_discrete(limits = c("ind_intercept", "ind_intercept_slope", "global_intercept", "global_intercept_slope"), label = c("I", "I + S", "", "")) +
facet_wrap(~component_factors, ncol = 5) +
theme_classic() +
hist_theme +
theme(axis.line.x = element_blank(), axis.ticks.x = element_blank(), axis.text.x = element_text(angle = 90)) +
theme(legend.position="none")
IS_figure_PCA_1_11
IS_figure_PCA_1_5
```
## Make figure with conditional independence and PCA analyses.
```{r}
ggdraw() +
draw_plot(IS_figure_PCA_1_5, x = 0.57, y = 0.05, width = 0.4, height = 0.4) +
draw_plot(pc1to5_plot, x = 0.57, y = 0.45, width = 0.4, height = 0.5) +
draw_plot(Q_neurons_plot, x = 0, y = 0.5, width = 0.35, height = 0.45) +
draw_plot(Q_intercepts_plot, x = 0, y = 0, width = 0.35, height = 0.45) +
draw_plot(all.pca.prop.var.plot, x = 0.35, y = 0.55, width = 0.2, height = 0.35) +
draw_plot(all.pca.biplot, x = 0.35, y = 0, width = 0.2, height = 0.4) +
draw_plot_label(label = c("A", "B", "C", "D", "E", "F"), size = 20,
x = c(0, 0, 0.35, 0.35, 0.55, 0.55), y = c(1, 0.5, 1, 0.5, 1, 0.5))
```
Save the figure.
```{r Save plot of PCA model fits}
ggsave("Figures/CondInd_PCA_figure.png", width = 220, height = 120, units = "mm")
```
## Evaluate influence of individual fixed effects on PCA components
Housing
```{r}
out.pca.x_g <- out.pca.x_g %>%
mutate(mm_vsris_housing = map(data, model_vsris_housing)) %>%
summary_2fixedeffects(mm_vsris_housing, "housing") %>%
FE2_p.adjust("housing")
(housing_table_PCA <- FE_table(out.pca.x_g, "housing", "component"))
table_path <- paste0(getwd(), "/Tables/PCA_FE_housing")
if (stajpeg == 1) {
save_kable_jpg_html(housing_table_PCA, table_path)
} else {housing_table_PCA %>% kableExtra::save_kable(file = paste0(table_path, ".html"), self_contained = T)}
```
Evaluate housing in datset containing only male mice.
Prepare data
```{r}
data.pca_male <- dplyr::select(data.sc, vm:fi, dvlocmm, id, housing, id, mlpos, hemi, age, sex, housing, expr, patchdir, rectime) %>%
filter(age > 32 & age <45 & expr == "HP" & sex == "M")
all_male.pca <- prcomp(drop_na(data.pca_male[1:12], fi),
retx = TRUE,
centre = TRUE,
scale = TRUE)
plot(all_male.pca)
summary(all_male.pca)
all.pca_male.x <- bind_cols(as_tibble(all_male.pca$x), drop_na(data.pca_male, fi))
out.pca.x_male_g <- all.pca_male.x %>%
gather("component", "value", 1:12) %>%
group_by(component) %>%
nest()
```
Do analysis
```{r}
out.pca.x_male_g <- out.pca.x_male_g %>%
mutate(mm_vsris_housing = map(data, model_vsris_housing)) %>%
summary_2fixedeffects(mm_vsris_housing, "housing") %>%
FE2_p.adjust("housing")
(housing_male_table_PCA <- FE_table(out.pca.x_male_g, "housing", "component"))
table_path <- paste0(getwd(), "/Tables/PCA_FE_housing_male")
if (stajpeg == 1) {
save_kable_jpg_html(housing_male_table_PCA, table_path)
} else {housing_male_table_PCA %>% kableExtra::save_kable(file = paste0(table_path, ".html"), self_contained = T)}
```
Evaluate whether interanimal differences remain in models that account for housing.
```{r}
out.pca.x_g <- out.pca.x_g %>%
mutate(lm_vsris_housing = map(data, linearmodel_housing))
out.pca.x_g <- mixed_vs_linear_pchisqu(out.pca.x_g,
mm_vsris_housing,
lm_vsris_housing)
out.pca.x_g$mm_vsris_housing_vslinear_pdiff_adj <- p.adjust(out.pca.x_g$mm_vsris_housing_vslinear_pdiff, method = "BH")
(PCA_mix_vs_linear_table <- table_mixedvslinear(out.pca.x_g, "mm_vsris_housing_vslinear", "component"))
if (stajpeg == 1) {
table_path <- paste0(getwd(), "/Tables/PCA_mm_vsris_housing_vslinear.jpg")
PCA_mix_vs_linear_table %>% kableExtra::save_kable(file = table_path, self_contained = T)
}
table_path <- paste0(getwd(), "/Tables/PCA_mm_vsris_housing_vslinear.html")
PCA_mix_vs_linear_table %>% kableExtra::save_kable(file = table_path, self_contained = T)
```
Sex
```{r}
out.pca.x_g <- out.pca.x_g %>%
mutate(mm_vsris_sex = map(data, model_vsris_sex)) %>%
summary_2fixedeffects(mm_vsris_sex, "sex") %>%
FE2_p.adjust("sex")
(sex_table_PCA <- FE_table(out.pca.x_g, "sex", "component"))
table_path <- paste0(getwd(), "/Tables/PCA_FE_sex.html")
sex_table_PCA %>% kableExtra::save_kable(file = table_path, self_contained = T)
```
Mediolateral position
```{r}
out.pca.x_g <- out.pca.x_g %>%
mutate(mm_vsris_mlpos = map(data, model_vsris_ml)) %>%
summary_2fixedeffects(mm_vsris_mlpos, "mlpos") %>%
FE2_p.adjust("mlpos")
(ml_table_PCA <- FE_table(out.pca.x_g, "mlpos", "component"))
table_path <- paste0(getwd(), "/Tables/PCA_FE_mlpos.html")
ml_table_PCA %>% kableExtra::save_kable(file = table_path, self_contained = T)
```
Hemisphere.
```{r}
out.pca.x_g <- out.pca.x_g %>%
mutate(mm_vsris_hemi = map(data, model_vsris_hemi)) %>%
summary_2fixedeffects(mm_vsris_hemi, "hemi") %>%
FE2_p.adjust("hemi")
(hemi_table_PCA <- FE_table(out.pca.x_g, "hemi", "component"))
table_path <- paste0(getwd(), "/Tables/PCA_FE_hemi.html")
hemi_table_PCA %>% kableExtra::save_kable(file = table_path, self_contained = T)
```
Direction.
```{r}
out.pca.x_g <- out.pca.x_g %>%
mutate(mm_vsris_dir = map(data, model_vsris_dir)) %>%
summary_2fixedeffects(mm_vsris_dir, "dir") %>%
FE2_p.adjust("dir")
(dir_table_PCA <- FE_table(out.pca.x_g, "dir", "component"))
table_path <- paste0(getwd(), "/Tables/PCA_FE_dir.html")
dir_table_PCA %>% kableExtra::save_kable(file = table_path, self_contained = T)
```
Recording time.
```{r}
out.pca.x_g <- out.pca.x_g %>%
mutate(mm_vsris_rect = map(data, model_vsris_rect)) %>%
summary_2fixedeffects(mm_vsris_rect, "rect") %>%
FE2_p.adjust("rect")
(rect_table_PCA <- FE_table(out.pca.x_g, "rect", "component"))
table_path <- paste0(getwd(), "/Tables/PCA_FE_rect.html")
rect_table_PCA %>% kableExtra::save_kable(file = table_path, self_contained = T)
```
## Evaluate PCA using intercepts and slopes from models fits as inputs.
Obtain intercepts and slopes from previously fit models.
```{r, warning=FALSE}
data_summary.pca <- prep_int_slopes_PCA(data.sc_r, "property", "mm_vsris")
data_intercepts_PCA <- spread(data_summary.pca[,1:3], property, ind_intercept)
data_slopes_PCA <- spread(data_summary.pca[,c(1:2, 4)], property, ind_slope)
```
Carry out PCA for intercepts
```{r}
intercepts.pca <- prcomp(data_intercepts_PCA[2:13],
retx = TRUE,
centre = TRUE,
scale = TRUE)
plot(intercepts.pca)
summary(intercepts.pca)
(intercepts.pca.biplot <- pca_biplot(as.data.frame(intercepts.pca$rotation)))
```
Carry out PCA for slopes.
```{r}
slopes.pca <- prcomp(data_slopes_PCA[2:13],
retx = TRUE,
centre = TRUE,
scale = TRUE)
plot(slopes.pca)
summary(slopes.pca)
(slopes.pca.biplot <- pca_biplot(as.data.frame(slopes.pca$rotation)))
```
Examine distribution of points
```{r}
id_lookup <- unique(data.sc[c("id", "age", "housing")])
slopes.pca_x <- bind_cols(as_tibble(slopes.pca$x), as.tibble(data_intercepts_PCA$id))
slopes.pca_x$age <- id_lookup$age[match(slopes.pca_x$value, id_lookup$id)]
slopes.pca_x$housing <- id_lookup$housing[match(slopes.pca_x$value, id_lookup$id)]
(pc_plot_12 <- ggplot(data = slopes.pca_x, aes(x = PC1, y = PC2)) +
geom_point(aes(colour = housing)))
```