figure_2.Rmd 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. ---
  2. title: "Spacek et al., 2021, Figure 2"
  3. output: pdf_document
  4. ---
  5. ```{r setup, include=FALSE}
  6. knitr::opts_chunk$set(echo = TRUE)
  7. library(arm)
  8. library(lmerTest)
  9. library(tidyverse)
  10. source('get_data.R')
  11. ```
  12. ```{r read_data, include=FALSE}
  13. tib = get_data("../csv/fig2.csv")
  14. ```
  15. # Figure 2e
  16. ``` {r tidy_2e, include=FALSE}
  17. tb_all = tib %>% filter(fmode == "all" )
  18. ```
  19. ## (I) Fit intercept-only model to slope (all spikes)
  20. ``` {r fit_model_2e_slope}
  21. # Random intercept for neurons,
  22. # random intercept for experiments, nested in series
  23. lmer.2e_slope = lmer(slope ~ 1 + (1 | uid) + (1 | sid/eid),
  24. data = tb_all %>% drop_na(slope))
  25. display(lmer.2e_slope)
  26. ```
  27. ```{r, include=FALSE}
  28. print(paste("Intercept: ", format(fixef(lmer.2e_slope), digits=2), " [", format(fixef(lmer.2e_slope) - 2 * se.fixef(lmer.2e_slope), digits=2), ", ", format(fixef(lmer.2e_slope) + 2 * se.fixef(lmer.2e_slope), digits=2), "]", sep=""))
  29. ```
  30. ## 95-% confidence interval on slope
  31. `r format(fixef(lmer.2e_slope)[1], digits=2, nsmall=2)` $\pm$ `r format(2 * se.fixef(lmer.2e_slope)[1], digits=2, nsmall=2)` \newline
  32. n = `r nrow(tb_all %>% drop_na(slope) %>% count(uid))` neurons from `r nrow(tb_all %>% drop_na(slope) %>% count(mid))` mice
  33. \newpage
  34. ## (II) Fit intercept-only model to threshold (all spikes)
  35. ```{r, fit_model_2e_threshold}
  36. # Random intercept for neurons,
  37. # random intercept for experiments, nested in mice
  38. lmer.2e_thresh = lmer(thresh ~ 1 + (1 | uid) + (1 | mid/eid),
  39. data = tb_all %>% drop_na(thresh))
  40. display(lmer.2e_thresh)
  41. ```
  42. ```{r, include=FALSE}
  43. print(paste("Intercept: ", format(fixef(lmer.2e_thresh), digits=2), " [", format(fixef(lmer.2e_thresh) - 2 * se.fixef(lmer.2e_thresh) , digits=2), ", ", format(fixef(lmer.2e_thresh) + 2 * se.fixef(lmer.2e_thresh), digits=2), "]", sep=""))
  44. ```
  45. ## 95-% confidence interval on threshold
  46. `r format(fixef(lmer.2e_thresh)[1], digits=2, nsmall=2)` $\pm$ `r format(2 * se.fixef(lmer.2e_thresh)[1], digits=2, nsmall=2)` \newline
  47. n = `r nrow(tb_all %>% drop_na(thresh) %>% count(uid))` neurons from `r nrow(tb_all %>% drop_na(thresh) %>% count(mid))` mice
  48. \newpage
  49. # Figure 2f
  50. ## Goodness-of-fit vs burst ratio during suppression
  51. ``` {r, fit_model_2f}
  52. # Random intercept for neurons,
  53. # random intercept for experiments, nested in series
  54. lmer.2f = lmer(rsq ~ suppression_meanburstratio + (1 | uid) + (1 | sid/eid),
  55. data = tb_all %>% drop_na(rsq, suppression_meanburstratio))
  56. display(lmer.2f)
  57. anova(lmer.2f)
  58. ```
  59. ## 95-% confidence interval on slope
  60. `r format(fixef(lmer.2f)[2], digits=2, nsmall=2)` $\pm$ `r format(2 * se.fixef(lmer.2f)[2], digits=2, nsmall=2)` \newline
  61. n = `r nrow(tb_all %>% drop_na(rsq, suppression_meanburstratio) %>% count(uid))` neurons from `r nrow(tb_all %>% drop_na(rsq, suppression_meanburstratio) %>% count(mid))` mice
  62. ```{r, store_coeffs_2f, include=F}
  63. # store results in file
  64. coef_df = data.frame("intercept" = fixef(lmer.2f)[1], "slope" = fixef(lmer.2f)[2], row.names = "")
  65. write_csv(coef_df, "_stats/figure_2f_coefs.csv")
  66. ```
  67. \newpage
  68. # Figure 2g
  69. ## Goodness-of-fit with and without removal of burst spikes
  70. ``` {r tidy_2g, include = F}
  71. # Extract two firing modes: all spikes, and non-burst spikes, and turn them into numeric binary predictors
  72. tb <- tib %>% filter(fmode == "all" | fmode == 'nonburst') %>% mutate(allSpikes = ifelse(fmode == "all", 1, 0))
  73. ```
  74. ``` {r fit_model_2g}
  75. # Random intercept for neurons,
  76. # random intercept for experiments, nested in series
  77. lmer.2g = lmer(rsq ~ allSpikes + (1 | uid) + (1 | sid/eid),
  78. data = tb %>% drop_na(rsq, allSpikes))
  79. display(lmer.2g)
  80. anova(lmer.2g)
  81. ```
  82. ```{r get_predicted_average_effect_2g, include=F}
  83. mNonBurst = fixef(lmer.2g)[1]
  84. diffMeans = fixef(lmer.2g)[2]
  85. mAll = fixef(lmer.2g)[1] + diffMeans
  86. ```
  87. # Predicted average effect
  88. All spikes: $R^2$ = `r format(mAll, digits=2, nsmall=2)` \newline
  89. non-burst spikes: $R^2$ = `r format(mNonBurst, digits=2, nsmall=2)` \newline
  90. n = `r nrow(tb %>% drop_na(rsq, allSpikes) %>% count(uid))` neurons from `r nrow(tb %>% drop_na(rsq, allSpikes) %>% count(mid))` mice
  91. \newpage
  92. # Figure 2h
  93. ## Goodness of fit with and without removal of tonic spikes
  94. ``` {r tidy_2h, include=F}
  95. # Extract two firing modes: all spikes, and non-random spikes
  96. tb <- tib %>% filter(fmode == "all" | fmode == 'nonrand') %>% mutate(allSpikes = ifelse(fmode == "all", 1, 0))
  97. ```
  98. ``` {r fit_model_2h}
  99. # Random intercept for neurons,
  100. # random intercept for experiments, nested in series
  101. lmer.2h = lmer(rsq ~ allSpikes + (1 | uid) + (1 | sid/eid),
  102. data = tb %>% drop_na(rsq, allSpikes))
  103. display(lmer.2h)
  104. anova(lmer.2h)
  105. ```
  106. ```{r get_predicted_average_effect_2h, include=F}
  107. mNonRand = fixef(lmer.2h)[1]
  108. diffMeans = fixef(lmer.2h)[2]
  109. mAllSpikes = fixef(lmer.2h)[1] + diffMeans
  110. ```
  111. # Predicted average effect
  112. All spikes: $R^2$ = `r format(mAllSpikes, digits=2, nsmall=2)` \newline
  113. Randomly removed spikes: $R^2$ = `r format(mNonRand, digits=2, nsmall=2)` \newline
  114. n = `r nrow(tb %>% drop_na(rsq, allSpikes) %>% count(uid))` neurons from `r nrow(tb %>% drop_na(rsq, allSpikes) %>% count(mid))` mice
  115. \newpage
  116. # Figure 2i
  117. ``` {r tidy_2i, include=FALSE}
  118. tb_nonburst = tib %>% filter(fmode == "nonburst")
  119. ```
  120. ## (I) Fit intercept-only model to slope (non-burst spikes)
  121. ```{r, fit_model_2i_slope}
  122. # Random intercept for neurons,
  123. # random intercepts for experiments, nested in series
  124. lmer.2i_slope = lmer(slope ~ 1 + (1 | uid) + (1 | sid/eid),
  125. data = tb_nonburst %>% drop_na(slope))
  126. display(lmer.2i_slope)
  127. ```
  128. ## 95-% confidence interval
  129. ```{r, include=FALSE}
  130. print(paste("Intercept: ", format(fixef(lmer.2i_slope), digits=2), " [", format(fixef(lmer.2i_slope) - 2 * se.fixef(lmer.2i_slope), digits=2), ", ", format(fixef(lmer.2i_slope) + 2 * se.fixef(lmer.2i_slope), digits=2), "]", sep=""))
  131. ```
  132. Intercept of `r format(fixef(lmer.2i_slope)[1], digits=2, nsmall=2)` $\pm$ `r format(2 * se.fixef(lmer.2i_slope)[1], digits=1, nsmall=1)` \newline
  133. n = `r nrow(tb_nonburst %>% drop_na(slope) %>% count(uid))` neurons from `r nrow(tb_nonburst %>% drop_na(slope) %>% count(mid))` mice
  134. \newpage
  135. ## (II) Fit intercept-only model to threshold (non-burst spikes)
  136. ```{r, fit_model_2i_threshold}
  137. # Random intercept for neurons,
  138. # random intercept for experiments, nested in series, nested in mice
  139. lmer.2i_thresh = lmer(thresh ~ 1 + (1 | uid) + (1 | mid/sid/eid),
  140. data = tb_nonburst %>% drop_na(thresh))
  141. display(lmer.2i_thresh)
  142. ```
  143. ## 95-% confidence interval
  144. ```{r, include=FALSE}
  145. print(paste("Intercept: ", format(fixef(lmer.2i_thresh), digits=2), " [", format(fixef(lmer.2i_thresh) - 2 * se.fixef(lmer.2i_thresh), digits=2), ", ", format(fixef(lmer.2i_thresh) + 2 * se.fixef(lmer.2i_thresh), digits=2), "]", sep=""))
  146. ```
  147. Intercept of `r format(fixef(lmer.2i_thresh)[1], digits=1, nsmall=1)` $\pm$ `r format(2 * se.fixef(lmer.2i_thresh)[1], digits=1, nsmall=1)`
  148. \newpage
  149. # Fit a single model to Figures 2e and 2i
  150. ## (I) Predict slope based on firing mode (all spikes vs non-burst spikes)
  151. ```{r, tidy_2ei, include=FALSE}
  152. # (1) Fit a single model to panels e and i to examine changes in slope with firing mode as a binary predictor. We need to turn the strings (= factors, = categorical predictors) into binary predictors (numeric) first:
  153. tb = tib %>% filter(fmode == "all" | fmode == "nonburst") %>% mutate(allSpikes = ifelse(fmode == "all", 1, 0))
  154. ```
  155. ```{r fit_model_2ei_slope}
  156. # Random intercept for neurons,
  157. # random intercept for experiments, nested in series, nested in mice
  158. lmer.2ei_slope = lmer(slope ~ allSpikes + (1 | uid) + (1 | mid/sid/eid),
  159. data = tb %>% drop_na(slope, allSpikes))
  160. display(lmer.2ei_slope)
  161. anova(lmer.2ei_slope)
  162. ```
  163. \newpage
  164. ## (II) Predict threshold based on firing mode (all spikes vs non-burst spikes)
  165. ```{r, fit_model_2ei_threshold}
  166. # Random intercept for neurons,
  167. # random intercept for experiments, nested in series, nested in mice
  168. lmer.2ei_thresh = lmer(thresh ~ allSpikes + (1 | uid) + (1 | mid/sid/eid),
  169. data = tb %>% drop_na(thresh, allSpikes))
  170. display(lmer.2ei_thresh)
  171. anova(lmer.2ei_thresh)
  172. ```