modelFree_stereology_MSE_figures.Rmd 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. ---
  2. title: "Axonal Length"
  3. author: "Sergio D?ez Hermano"
  4. date: '`r format(Sys.Date(),"%e de %B, %Y")`'
  5. output:
  6. html_document:
  7. highlight: tango
  8. toc: yes
  9. toc_depth: 4
  10. toc_float:
  11. collapsed: no
  12. ---
  13. ```{r setup, include=FALSE}
  14. require(knitr)
  15. # include this code chunk as-is to set options
  16. opts_chunk$set(comment = NA, prompt = FALSE, fig.height=5, fig.width=5, dpi=300, fig.align = "center", echo = TRUE, message = FALSE, warning = FALSE, cache=FALSE)
  17. Sys.setlocale("LC_TIME", "C")
  18. ```
  19. ##Load libraries and functions
  20. ```{r}
  21. library(R.matlab)
  22. library(grid)
  23. library(gridExtra)
  24. library(lattice)
  25. library(dplyr)
  26. library(ggplot2)
  27. library(ggpubr)
  28. library(ggridges)
  29. library(mltools)
  30. library(data.table)
  31. library(caret)
  32. library(interactions)
  33. library(data.table)
  34. library(wesanderson)
  35. options(digits = 10)
  36. # https://rstudio-pubs-static.s3.amazonaws.com/297778_5fce298898d64c81a4127cf811a9d486.html
  37. ###############################
  38. # Add continuous color legend #
  39. ###############################
  40. legend.col <- function(col, lev){
  41. opar <- par
  42. n <- length(col)
  43. bx <- par("usr")
  44. box.cx <- c(bx[2] + (bx[2] - bx[1]) / 1000,
  45. bx[2] + (bx[2] - bx[1]) / 1000 + (bx[2] - bx[1]) / 50)
  46. box.cy <- c(bx[3], bx[3])
  47. box.sy <- (bx[4] - bx[3]) / n
  48. xx <- rep(box.cx, each = 2)
  49. par(xpd = TRUE)
  50. for(i in 1:n){
  51. yy <- c(box.cy[1] + (box.sy * (i - 1)),
  52. box.cy[1] + (box.sy * (i)),
  53. box.cy[1] + (box.sy * (i)),
  54. box.cy[1] + (box.sy * (i - 1)))
  55. polygon(xx, yy, col = col[i], border = col[i])
  56. }
  57. par(new = TRUE)
  58. plot(0, 0, type = "n",
  59. ylim = c(min(lev), max(lev)),
  60. yaxt = "n", ylab = "",
  61. xaxt = "n", xlab = "",
  62. frame.plot = FALSE)
  63. axis(side = 4, las = 2, at = lev, tick = FALSE, line = .15)
  64. par <- opar
  65. }
  66. ```
  67. ##Load data
  68. ```{r}
  69. # Get file paths
  70. axonNames <- list.files(paste("EstereoDataPlanos/", sep=""), full.names=T)
  71. # Load matlab file
  72. axonFiles <- lapply(axonNames, function(x) readMat(x))
  73. names(axonFiles) <- c("Type1", "Type2", "Type3", "Type4")
  74. # Extract data
  75. # errorMatrix contains 4 arrays, one per neuron type
  76. # within each array, the dimensions corresponds to step:dist:neuron
  77. # this means that each array has as many matrices as neuron of a certain type
  78. dist <- lapply(axonFiles, function(x) x$Distancias.Entre.Planos)[[1]]
  79. step <- lapply(axonFiles, function(x) x$Step.Lengths)[[1]]
  80. errorMatrix <- lapply(axonFiles, function(x) x$MATRIZ.ERROR.LENGTH)
  81. lpMatrix <- lapply(axonFiles, function(x) x$MATRIZ.ESTIMATED.AXON.LENGTH)
  82. lrVals <- lapply(axonFiles, function(x) x$AXON.REAL.LENGTH)
  83. # Get number of neurons per type
  84. numberTypes <- unlist(lapply(errorMatrix, function(x) dim(x)[3]))
  85. # Vectorizing the arrays goes as follows: neuron, dist, step
  86. errorVector <- unlist(lapply(errorMatrix, function(x) as.vector(x)))
  87. lpVector <- unlist(lapply(lpMatrix, function(x) as.vector(x)))
  88. lrVector <- unlist(lapply(lrVals, function(x) as.vector(x)))
  89. # Store in data frames
  90. allData <- data.frame(id = rep(1:sum(numberTypes), each = 90),
  91. neurType = rep(c("Type1", "Type2", "Type3", "Type4"), times = 90*numberTypes),
  92. dist = rep(rep(dist, each = 9), sum(numberTypes)),
  93. step = rep(rep(step, 10), sum(numberTypes)),
  94. error = abs(errorVector),
  95. error2 = errorVector,
  96. LRe = lpVector,
  97. LR = rep(lrVector, each = 90))
  98. allData$errorRaw <- allData$LR - allData$LRe
  99. allData$interact <- interaction(allData$step, allData$dist, lex.order = T)
  100. allData$interact2 <- interaction(allData$neurType, allData$step, allData$dist, lex.order = T)
  101. ```
  102. ## Estimate MSE
  103. ```{r}
  104. mseDistep <- by(allData, allData$interact2, function(x) sqrt(mltools::mse(x$LRe, x$LR)))
  105. # mseDistep <- by(allData, allData$interact2, function(x) cor(x$LRe, x$LR)^2)
  106. ```
  107. ###RMSE ggplot (smooth bands)
  108. ```{r}
  109. #######################
  110. # Organize data frame #
  111. #######################
  112. # Store MSE in data frame
  113. mseFrame <- data.frame(mse = as.numeric(mseDistep),
  114. x = rep(1:90, 4),
  115. step = rep(rep(seq(70,150,10), each = 10), 4),
  116. dist = rep(rep(seq(3,30,3), 9), 4),
  117. axType = rep(c("Type1", "Type2", "Type3", "Type4"), each = 90))
  118. # Find max and min for Types1-3
  119. min123 <- min(mseFrame[mseFrame$axType != "Type4", "mse"])
  120. max123 <- max(mseFrame[mseFrame$axType != "Type4", "mse"])
  121. # Find max and min for Type4
  122. min4 <- min(mseFrame[mseFrame$axType == "Type4", "mse"])
  123. max4 <- max(mseFrame[mseFrame$axType == "Type4", "mse"])
  124. # Normalize
  125. mseFrame$maxMSE <- c(rep(max123, 90*3), rep(max4, 90))
  126. mseFrame$mseNorm <- mseFrame$mse/mseFrame$maxMSE
  127. # Color palette
  128. pal <- wes_palette("Zissou1", 10, type = "continuous")
  129. rmseList <- list()
  130. # Find max and min for plotting
  131. min123plot <- min(mseFrame[mseFrame$axType != "Type4", "mseNorm"])
  132. max123plot <- max(mseFrame[mseFrame$axType != "Type4", "mseNorm"])
  133. min4plot <- min(mseFrame[mseFrame$axType == "Type4", "mseNorm"])
  134. max4plot <- max(mseFrame[mseFrame$axType == "Type4", "mseNorm"])
  135. # Color palette
  136. pal <- wes_palette("Zissou1", 10, type = "continuous")
  137. rmseList <- list()
  138. ####################
  139. # RMSE Types 1,2,3 #
  140. ####################
  141. mseType <- mseFrame[mseFrame$axType != "Type4", ]
  142. mse123 <- ggplot(data=mseType, aes(x=x, y=mseNorm, group=step)) +
  143. # geom_line() +
  144. facet_grid(. ~ axType) +
  145. geom_smooth(colour = "black", method = "loess", span = 1.2, size = 0.5) +
  146. geom_point(aes(colour = dist), size = 3) +
  147. # geom_point(shape = 1, colour = "black") +
  148. scale_x_continuous(breaks=seq(5,90,10), labels = seq(70,150,10)) +
  149. scale_colour_gradientn(name = "Distance",
  150. colours = pal,
  151. breaks = seq(3,30,3)) +
  152. # geom_vline(xintercept=seq(0,90,10), colour = "gray", linetype="dashed", ) +
  153. theme_bw() +
  154. theme(panel.grid.major = element_blank(),
  155. panel.grid.minor = element_blank(),
  156. legend.key.size = unit(1.9, "cm"),
  157. legend.key.width = unit(0.5,"cm"),
  158. strip.text = element_text(size = 20),
  159. plot.title = element_text(hjust = 0.5, size = 22),
  160. axis.text.x = element_text(size = 15, color = "black"),
  161. axis.text.y = element_text(size = 15, color = "black"),
  162. axis.title.x = element_text(size = 18),
  163. axis.title.y = element_text(size = 18),
  164. panel.background = element_rect(colour = "black",size = 1)) +
  165. xlab("Step") +
  166. ylab("Model error")
  167. ###############
  168. # RMSE Type 4 #
  169. ###############
  170. mseType <- mseFrame[mseFrame$axType == "Type4", ]
  171. # mseType$axType <- factor(mseFrame$axType, levels = c("Type4", "Type1", "Type2", "Type3"))
  172. mse4 <- ggplot(data=mseType, aes(x=x, y=mseNorm, group=step)) +
  173. # geom_line() +
  174. facet_grid(. ~ axType) +
  175. geom_smooth(colour = "black", method = "loess", span = 1.2, size = 0.5) +
  176. geom_point(aes(colour = dist), size = 3) +
  177. # geom_point(shape = 1, colour = "black") +
  178. scale_x_continuous(breaks=seq(5,90,10), labels = seq(70,150,10)) +
  179. scale_colour_gradientn(name = "Distance",
  180. colours = pal,
  181. breaks = seq(3,30,3)) +
  182. # geom_vline(xintercept=seq(0,90,10), colour = "gray", linetype="dashed", ) +
  183. theme_bw() +
  184. theme(panel.grid.major = element_blank(),
  185. panel.grid.minor = element_blank(),
  186. legend.key.size = unit(1.9, "cm"),
  187. legend.key.width = unit(0.5,"cm"),
  188. strip.text = element_text(size = 20),
  189. plot.title = element_text(hjust = 0.5, size = 20),
  190. axis.text.x = element_text(size = 15, color = "black"),
  191. axis.text.y = element_text(size = 15, color = "black"),
  192. axis.title.x = element_text(size = 18),
  193. axis.title.y = element_text(size = 18),
  194. panel.background = element_rect(colour = "black",size = 1)) +
  195. xlab("Step") +
  196. ylab("Model error")
  197. ###########################
  198. # EXAMPLES Types 1,2,3, 4 #
  199. ###########################
  200. #List to store
  201. exList <- list()
  202. # Type 1
  203. for (i in unique(allData$neurType)) {
  204. data2d <- allData[allData$neurType == i & (allData$interact == "70.3" | allData$interact == "150.30"), ]
  205. dataReal <- data.frame(LR = data2d$LR, LRe = data2d$LR)
  206. if (i == "Type1" | i == "Type4") {
  207. exList[[i]] <- ggplot(data=data2d, aes(x=LRe, y=LR)) +
  208. facet_grid(. ~ interact) +
  209. geom_point(colour = "black", size = 1) +
  210. # geom_point(shape = 1, colour = "black") +
  211. geom_point(data = dataReal, colour = "red", size = 1) +
  212. # geom_point(data = dataReal, shape = 1, colour = "black") +
  213. theme_bw() +
  214. theme(panel.grid.major = element_blank(),
  215. panel.grid.minor = element_blank(),
  216. strip.text = element_text(size = 20),
  217. plot.title = element_text(hjust = 0.5, size = 12),
  218. axis.title.x=element_text(size = 14),
  219. axis.text.x=element_blank(),
  220. axis.ticks.x=element_blank(),
  221. axis.title.y=element_text(size = 14),
  222. axis.text.y=element_blank(),
  223. axis.ticks.y=element_blank(),
  224. panel.background = element_rect(colour = "black",size = 1),
  225. plot.margin=unit(c(0.2,-0.2,0.2,-0.2), "cm")) +
  226. xlab("Predicted length") +
  227. ylab("Real Length")
  228. } else {
  229. exList[[i]] <- ggplot(data=data2d, aes(x=LRe, y=LR)) +
  230. facet_grid(. ~ interact) +
  231. geom_point(colour = "black", size = 1) +
  232. # geom_point(shape = 1, colour = "black") +
  233. geom_point(data = dataReal, colour = "red", size = 1) +
  234. # geom_point(data = dataReal, shape = 1, colour = "black") +
  235. theme_bw() +
  236. theme(panel.grid.major = element_blank(),
  237. panel.grid.minor = element_blank(),
  238. strip.text = element_text(size = 20),
  239. plot.title = element_text(hjust = 0.5, size = 12),
  240. axis.title.x=element_text(size = 14),
  241. axis.text.x=element_blank(),
  242. axis.ticks.x=element_blank(),
  243. axis.title.y=element_blank(),
  244. axis.text.y=element_blank(),
  245. axis.ticks.y=element_blank(),
  246. panel.background = element_rect(colour = "black",size = 1),
  247. plot.margin=unit(c(0.2,-0.2,0.2,-0.2), "cm")) +
  248. xlab("Predicted length")
  249. }
  250. }
  251. ```
  252. ###Arrange
  253. ```{r, fig.width=15, fig.height=8}
  254. # https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html
  255. setwd("EstereoFigures/")
  256. png(filename=paste("rmse123_arranged.png", sep=""), type="cairo",
  257. units="in", width=15, height=8, pointsize=12,
  258. res=300)
  259. gridExtra::grid.arrange(mse123, exList[[1]], exList[[2]], exList[[3]], ncol = 7, nrow = 2,
  260. layout_matrix = rbind(rep(1, 7), c(NA,2,NA,3,NA,4,NA)),
  261. widths = c(0.68, 5.85, 0.28, 5.5, 0.28, 5.5, 1.375),
  262. heights = c(6, 3))
  263. dev.off()
  264. ```
  265. ```{r, fig.width=20, fig.height=8}
  266. # https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html
  267. setwd("EstereoFigures/")
  268. png(filename=paste("rmseFull_arranged.png", sep=""), type="cairo",
  269. units="in", width=20, height=8, pointsize=12,
  270. res=300)
  271. gridExtra::grid.arrange(mse123, exList[[1]], exList[[2]], exList[[3]], mse4, exList[[4]], ncol = 11, nrow = 2,
  272. layout_matrix = rbind(c(rep(1, 7), NA, 5, 5, 5),
  273. c(NA, 2, NA, 3, NA, 4, NA, NA, NA, 6, NA)),
  274. widths = c(0.725, 5.8, 0.28, 5.5, 0.28, 5.5, 1.5, 1, 0.9, 5.85, 1.5),
  275. heights = c(6, 3))
  276. dev.off()
  277. ```
  278. ###Save mse
  279. UNCOMMENT IF ARRANGE DOES NOT WORK
  280. ```{r, fig.width=15, fig.height=5}
  281. # # Types 1,2,3
  282. # setwd("EstereoFigures/")
  283. # png(filename=paste("rmse123.png", sep=""), type="cairo",
  284. # units="in", width=15, height=5, pointsize=12,
  285. # res=300)
  286. # mse123
  287. # dev.off()
  288. #
  289. # # Type 4
  290. # png(filename=paste("rmse4.png", sep=""), type="cairo",
  291. # units="in", width=6, height=5, pointsize=12,
  292. # res=300)
  293. # mse4
  294. # dev.off()
  295. #
  296. # # Examples
  297. # png(filename=paste("examplesRMSE_Type1.png", sep=""), type="cairo",
  298. # units="in", width=3.5, height=2.25, pointsize=12,
  299. # res=300)
  300. # exList[["Type1"]]
  301. # dev.off()
  302. #
  303. # png(filename=paste("examplesRMSE_Type2.png", sep=""), type="cairo",
  304. # units="in", width=3.5, height=2.25, pointsize=12,
  305. # res=300)
  306. # exList[["Type2"]]
  307. # dev.off()
  308. #
  309. # png(filename=paste("examplesRMSE_Type3.png", sep=""), type="cairo",
  310. # units="in", width=3.5, height=2.25, pointsize=12,
  311. # res=300)
  312. # exList[["Type3"]]
  313. # dev.off()
  314. #
  315. # png(filename=paste("examplesRMSE_Type4.png", sep=""), type="cairo",
  316. # units="in", width=3.5, height=2.25, pointsize=12,
  317. # res=300)
  318. # exList[["Type4"]]
  319. # dev.off()
  320. ```
  321. ```{r, fig.width=3.5, fig.height=2.25}
  322. setwd("EstereoFigures/")
  323. ```
  324. ## Estimate R2
  325. ```{r}
  326. mseDistep <- by(allData, allData$interact2, function(x) cor(x$LRe, x$LR)^2)
  327. ```
  328. ###R2 ggplot (smooth bands)
  329. ```{r}
  330. #######################
  331. # Organize data frame #
  332. #######################
  333. # Store MSE in data frame
  334. mseFrame <- data.frame(mse = as.numeric(mseDistep),
  335. x = rep(1:90, 4),
  336. step = rep(rep(seq(70,150,10), each = 10), 4),
  337. dist = rep(rep(seq(3,30,3), 9), 4),
  338. axType = rep(c("Type1", "Type2", "Type3", "Type4"), each = 90))
  339. # Find max and min for Types1-3
  340. min123 <- min(mseFrame[mseFrame$axType != "Type4", "mse"])
  341. max123 <- max(mseFrame[mseFrame$axType != "Type4", "mse"])
  342. # Find max and min for Type4
  343. min4 <- min(mseFrame[mseFrame$axType == "Type4", "mse"])
  344. max4 <- max(mseFrame[mseFrame$axType == "Type4", "mse"])
  345. # Normalize
  346. mseFrame$maxMSE <- c(rep(max123, 90*3), rep(max4, 90))
  347. mseFrame$mseNorm <- mseFrame$mse/mseFrame$maxMSE
  348. # Color palette
  349. pal <- wes_palette("Zissou1", 10, type = "continuous")
  350. rmseList <- list()
  351. # Find max and min for plotting
  352. min123plot <- min(mseFrame[mseFrame$axType != "Type4", "mseNorm"])
  353. max123plot <- max(mseFrame[mseFrame$axType != "Type4", "mseNorm"])
  354. min4plot <- min(mseFrame[mseFrame$axType == "Type4", "mseNorm"])
  355. max4plot <- max(mseFrame[mseFrame$axType == "Type4", "mseNorm"])
  356. # Color palette
  357. pal <- wes_palette("Zissou1", 10, type = "continuous")
  358. rmseList <- list()
  359. ####################
  360. # RMSE Types 1,2,3 #
  361. ####################
  362. mseType <- mseFrame[mseFrame$axType != "Type4", ]
  363. mse123 <- ggplot(data=mseType, aes(x=x, y=mseNorm, group=step)) +
  364. # geom_line() +
  365. facet_grid(. ~ axType) +
  366. geom_smooth(colour = "black", method = "loess", span = 1.2, size = 0.5) +
  367. geom_point(aes(colour = dist), size = 3) +
  368. # geom_point(shape = 1, colour = "black") +
  369. scale_x_continuous(breaks=seq(5,90,10), labels = seq(70,150,10)) +
  370. scale_colour_gradientn(name = "Distance",
  371. colours = pal,
  372. breaks = seq(3,30,3)) +
  373. # geom_vline(xintercept=seq(0,90,10), colour = "gray", linetype="dashed", ) +
  374. theme_bw() +
  375. theme(panel.grid.major = element_blank(),
  376. panel.grid.minor = element_blank(),
  377. legend.key.size = unit(1.9, "cm"),
  378. legend.key.width = unit(0.5,"cm"),
  379. strip.text = element_text(size = 20),
  380. plot.title = element_text(hjust = 0.5, size = 22),
  381. axis.text.x = element_text(size = 15, color = "black"),
  382. axis.text.y = element_text(size = 15, color = "black"),
  383. axis.title.x = element_text(size = 18),
  384. axis.title.y = element_text(size = 18),
  385. panel.background = element_rect(colour = "black",size = 1)) +
  386. xlab("Step") +
  387. ylab("R2")
  388. ###############
  389. # RMSE Type 4 #
  390. ###############
  391. mseType <- mseFrame[mseFrame$axType == "Type4", ]
  392. # mseType$axType <- factor(mseFrame$axType, levels = c("Type4", "Type1", "Type2", "Type3"))
  393. mse4 <- ggplot(data=mseType, aes(x=x, y=mseNorm, group=step)) +
  394. # geom_line() +
  395. facet_grid(. ~ axType) +
  396. geom_smooth(colour = "black", method = "loess", span = 1.2, size = 0.5) +
  397. geom_point(aes(colour = dist), size = 3) +
  398. # geom_point(shape = 1, colour = "black") +
  399. scale_x_continuous(breaks=seq(5,90,10), labels = seq(70,150,10)) +
  400. scale_colour_gradientn(name = "Distance",
  401. colours = pal,
  402. breaks = seq(3,30,3)) +
  403. # geom_vline(xintercept=seq(0,90,10), colour = "gray", linetype="dashed", ) +
  404. theme_bw() +
  405. theme(panel.grid.major = element_blank(),
  406. panel.grid.minor = element_blank(),
  407. legend.key.size = unit(1.9, "cm"),
  408. legend.key.width = unit(0.5,"cm"),
  409. strip.text = element_text(size = 20),
  410. plot.title = element_text(hjust = 0.5, size = 20),
  411. axis.text.x = element_text(size = 15, color = "black"),
  412. axis.text.y = element_text(size = 15, color = "black"),
  413. axis.title.x = element_text(size = 18),
  414. axis.title.y = element_text(size = 18),
  415. panel.background = element_rect(colour = "black",size = 1)) +
  416. xlab("Step") +
  417. ylab("R2")
  418. ###########################
  419. # EXAMPLES Types 1,2,3, 4 #
  420. ###########################
  421. #List to store
  422. exList <- list()
  423. # Type 1
  424. for (i in unique(allData$neurType)) {
  425. data2d <- allData[allData$neurType == i & (allData$interact == "70.3" | allData$interact == "150.30"), ]
  426. dataReal <- data.frame(LR = data2d$LR, LRe = data2d$LR)
  427. if (i == "Type1" | i == "Type4") {
  428. exList[[i]] <- ggplot(data=data2d, aes(x=LRe, y=LR)) +
  429. facet_grid(. ~ interact) +
  430. geom_point(colour = "black", size = 1) +
  431. # geom_point(shape = 1, colour = "black") +
  432. geom_point(data = dataReal, colour = "red", size = 1) +
  433. # geom_point(data = dataReal, shape = 1, colour = "black") +
  434. theme_bw() +
  435. theme(panel.grid.major = element_blank(),
  436. panel.grid.minor = element_blank(),
  437. strip.text = element_text(size = 20),
  438. plot.title = element_text(hjust = 0.5, size = 12),
  439. axis.title.x=element_text(size = 14),
  440. axis.text.x=element_blank(),
  441. axis.ticks.x=element_blank(),
  442. axis.title.y=element_text(size = 14),
  443. axis.text.y=element_blank(),
  444. axis.ticks.y=element_blank(),
  445. panel.background = element_rect(colour = "black",size = 1),
  446. plot.margin=unit(c(0.2,-0.2,0.2,-0.2), "cm")) +
  447. xlab("Predicted length") +
  448. ylab("Real Length")
  449. } else {
  450. exList[[i]] <- ggplot(data=data2d, aes(x=LRe, y=LR)) +
  451. facet_grid(. ~ interact) +
  452. geom_point(colour = "black", size = 1) +
  453. # geom_point(shape = 1, colour = "black") +
  454. geom_point(data = dataReal, colour = "red", size = 1) +
  455. # geom_point(data = dataReal, shape = 1, colour = "black") +
  456. theme_bw() +
  457. theme(panel.grid.major = element_blank(),
  458. panel.grid.minor = element_blank(),
  459. strip.text = element_text(size = 20),
  460. plot.title = element_text(hjust = 0.5, size = 12),
  461. axis.title.x=element_text(size = 14),
  462. axis.text.x=element_blank(),
  463. axis.ticks.x=element_blank(),
  464. axis.title.y=element_blank(),
  465. axis.text.y=element_blank(),
  466. axis.ticks.y=element_blank(),
  467. panel.background = element_rect(colour = "black",size = 1),
  468. plot.margin=unit(c(0.2,-0.2,0.2,-0.2), "cm")) +
  469. xlab("Predicted length")
  470. }
  471. }
  472. ```
  473. ###Arrange
  474. ```{r, fig.width=20, fig.height=8}
  475. # https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html
  476. setwd("EstereoFigures/")
  477. png(filename=paste("r2_Full_arranged.png", sep=""), type="cairo",
  478. units="in", width=20, height=8, pointsize=12,
  479. res=300)
  480. gridExtra::grid.arrange(mse123, exList[[1]], exList[[2]], exList[[3]], mse4, exList[[4]], ncol = 11, nrow = 2,
  481. layout_matrix = rbind(c(rep(1, 7), NA, 5, 5, 5),
  482. c(NA, 2, NA, 3, NA, 4, NA, NA, NA, 6, NA)),
  483. widths = c(0.95, 5.8, 0.28, 5.5, 0.28, 5.5, 1.5, 1, 0.9, 5.85, 1.5),
  484. heights = c(6, 3))
  485. dev.off()
  486. ```
  487. ###Save R2
  488. UNCOMMENT IF ARRANGE DOES NOT WORK
  489. ```{r, fig.width=15, fig.height=5}
  490. # # Types 1,2,3
  491. # setwd("EstereoFigures/")
  492. # png(filename=paste("r2_123.png", sep=""), type="cairo",
  493. # units="in", width=15, height=5, pointsize=12,
  494. # res=300)
  495. # mse123
  496. # dev.off()
  497. #
  498. # # Type 4
  499. # png(filename=paste("r2_4.png", sep=""), type="cairo",
  500. # units="in", width=6, height=5, pointsize=12,
  501. # res=300)
  502. # mse4
  503. # dev.off()
  504. ```
  505. ##Mean error (Type123 vs 4)
  506. ```{r, fig.height=10, fig.width=20}
  507. # Estimate mean residual error
  508. meanFrame <- aggregate(error ~ dist + step + neurType, allData, mean)
  509. # Library
  510. library(latticeExtra)
  511. # coul <- pals::parula(10000)
  512. coul <- pals::jet(100000)
  513. # Store heatmaps
  514. heatList <- list()
  515. smoothList <- list()
  516. # Limits
  517. min123 <- min(meanFrame[meanFrame$neurType != "Type4", "error"])
  518. max123 <- max(meanFrame[meanFrame$neurType != "Type4", "error"])
  519. # Type 1
  520. smoothList[["Type1"]] <- levelplot(error ~ dist * step, data = meanFrame[meanFrame$neurType == "Type1", ], col.regions = coul,
  521. scales = list(y = list(at = unique(allData$step), labels = unique(allData$step), cex = 1.5),
  522. x = list(at = unique(allData$dist), labels = unique(allData$dist)), cex = 1.5),
  523. ylim = c(150,70),
  524. xlim = c(3,30),
  525. ylab = list(label = "Step", cex = 1.5),
  526. xlab = list(label = "Distance", cex = 1.5),
  527. main = list(label = "Type1", cex = 1.5),
  528. at = seq(min123,
  529. max123,
  530. 0.05),
  531. cuts = 100,
  532. colorkey = FALSE,
  533. panel = panel.levelplot.points, cex = 0) +
  534. layer_(panel.2dsmoother(..., n = 200))
  535. # Type 2
  536. smoothList[["Type2"]] <- levelplot(error ~ dist * step, data = meanFrame[meanFrame$neurType == "Type2", ], col.regions = coul,
  537. scales = list(y = list(at = unique(allData$step), labels = NULL, cex = 1.5),
  538. x = list(at = unique(allData$dist), labels = unique(allData$dist)), cex = 1.5),
  539. ylim = c(150,70),
  540. xlim = c(3,30),
  541. ylab="",
  542. xlab = list(label = "Distance", cex = 1.5),
  543. main = list(label = "Type2", cex = 1.5),
  544. at = seq(min123,
  545. max123,
  546. 0.025),
  547. cuts = 100,
  548. colorkey = FALSE,
  549. panel = panel.levelplot.points, cex = 0) +
  550. layer_(panel.2dsmoother(..., n = 200))
  551. # Type 3
  552. smoothList[["Type3"]] <- levelplot(error ~ dist * step, data = meanFrame[meanFrame$neurType == "Type3", ], col.regions = coul,
  553. scales = list(y = list(at = unique(allData$step), labels = NULL, cex = 1.5),
  554. x = list(at = unique(allData$dist), labels = unique(allData$dist)), cex = 1.5),
  555. ylim = c(150,70),
  556. xlim = c(3,30),
  557. ylab="",
  558. xlab = list(label = "Distance", cex = 1.5),
  559. main = list(label = "Type3", cex = 1.5),
  560. at = seq(min123,
  561. max123,
  562. 0.025),
  563. cuts = 100,
  564. colorkey = list(at = seq(min123,
  565. max123,
  566. 0.025),
  567. cex = 1.2),
  568. panel = panel.levelplot.points, cex = 0) +
  569. layer_(panel.2dsmoother(..., n = 200))
  570. # Type 4
  571. smoothList[["Type4"]] <- levelplot(error ~ dist * step, data = meanFrame[meanFrame$neurType == "Type4", ], col.regions = coul,
  572. scales = list(y = list(at = unique(allData$step), labels = unique(allData$step), cex = 1.5),
  573. x = list(at = unique(allData$dist), labels = unique(allData$dist)), cex = 1.5),
  574. ylim = c(150,70),
  575. xlim = c(3,30),
  576. ylab = list(label = "Step", cex = 1.5),
  577. xlab = list(label = "Distance", cex = 1.5),
  578. main = list(label = "Type4", cex = 1.5),
  579. cuts = 100,
  580. colorkey = list(at = (seq(min(meanFrame[meanFrame$neurType == "Type4", "error"]),
  581. max(meanFrame[meanFrame$neurType == "Type4", "error"]),
  582. 0.05)),
  583. cex = 1.2),
  584. panel = panel.levelplot.points, cex = 0) +
  585. layer_(panel.2dsmoother(..., n = 200))
  586. ```
  587. ####Arrange
  588. ```{r, fig.width=20, fig.height=5}
  589. # Plot
  590. setwd("EstereoFigures/")
  591. png(filename=paste("meanError_Full_jet.png", sep=""), type="cairo",
  592. units="in", width=20, height=5, pointsize=12,
  593. res=300)
  594. gridExtra::grid.arrange(grobs = smoothList, ncol = 5, nrow = 1,
  595. layout_matrix = rbind(c(1,2,3,NA,4)),
  596. widths = c(5,4.5,5,0.5,5.5))
  597. dev.off()
  598. ```
  599. ####Joint arrange
  600. ```{r}
  601. coul <- pals::jet(100000)
  602. # Types 1,2,3
  603. m123 <- levelplot(error ~ dist * step | neurType, data = meanFrame[meanFrame$neurType != "Type4", ], col.regions = coul,
  604. scales = list(y = list(at = unique(allData$step), labels = unique(allData$step), cex = 1.5),
  605. x = list(at = unique(allData$dist), labels = unique(allData$dist)), cex = 1.5),
  606. ylim = c(150,70),
  607. xlim = c(3,30),
  608. ylab = list(label = "Step", cex = 1.5),
  609. xlab = list(label = "Distance", cex = 1.5),
  610. at = seq(min123,
  611. max123,
  612. 0.05),
  613. cuts = 100,
  614. colorkey = list(labels=list(cex=1.5)),
  615. layout = c(3, 1),
  616. par.settings = list(strip.background=list(col="white")),
  617. par.strip.text=list(cex=1.5),
  618. panel = panel.levelplot.points, cex = 0) +
  619. layer_(panel.2dsmoother(..., n = 200))
  620. # Type4
  621. m4 <- levelplot(error ~ dist * step | neurType, data = meanFrame[meanFrame$neurType == "Type4", ], col.regions = coul,
  622. scales = list(y = list(at = unique(allData$step), labels = unique(allData$step), cex = 1.5),
  623. x = list(at = unique(allData$dist), labels = unique(allData$dist), cex = 1.5, alternating = 3)),
  624. ylim = c(150,70),
  625. xlim = c(3,30),
  626. ylab = list(label = "Step", cex = 1.5),
  627. xlab = list(label = "Distance", cex = 1.5),
  628. cuts = 100,
  629. colorkey = list(labels=list(cex=1.5)),
  630. par.settings = list(strip.background=list(col="white")),
  631. par.strip.text=list(cex=1.5),
  632. panel = panel.levelplot.points, cex = 0) +
  633. layer_(panel.2dsmoother(..., n = 200))
  634. # Plot
  635. setwd("EstereoFigures/")
  636. png(filename=paste("meanError_Full_joint_jet.png", sep=""), type="cairo",
  637. units="in", width=24, height=6.1, pointsize=12,
  638. res=300)
  639. gridExtra::grid.arrange(m123, m4, ncol = 3, nrow = 1,
  640. layout_matrix = rbind(c(1,NA,2)),
  641. widths = c(17,1.2,7.4))
  642. dev.off()
  643. ```
  644. ##Errors vs cumul probability
  645. RUN ONLY ONCE, THEN LOAD RDS FILE IN PLOT CHUNK
  646. ```{r, fig.height=5, fig.width=20}
  647. # Inverse quantile
  648. quantInv <- function(distr, value) ecdf(distr)(value)
  649. # Function to apply to all axon types
  650. quantType <- function(peFrame, probSeq) {
  651. probList <- list()
  652. for (i in unique(peFrame$interact)) {
  653. dataProb <- peFrame[peFrame$interact == i, "error"]
  654. probVec <- numeric()
  655. for (j in probSeq) {
  656. errProb <- quantInv(dataProb, j) - quantInv(dataProb, -j)
  657. probVec <- c(probVec, errProb)
  658. }
  659. probList[[i]] <- probVec
  660. }
  661. return(probList)
  662. }
  663. # Define errors for which to calculate probability
  664. binProb <- 0.5
  665. probSeq <- seq(binProb, 100, binProb)
  666. # Store axon types in lists
  667. frameList <- list(Type1 = allData[allData$neurType == "Type1", ],
  668. Type2 = allData[allData$neurType == "Type2", ],
  669. Type3 = allData[allData$neurType == "Type3", ],
  670. Type4 = allData[allData$neurType == "Type4", ])
  671. axProb <- lapply(frameList, function(x) quantType(x, probSeq))
  672. setwd("EstereoAnalysis/")
  673. saveRDS(axProb, "errorProbs_RMSE.rds")
  674. ```
  675. ###Load probs
  676. ```{r, fig.height=10, fig.width=15}
  677. # Reformat as dataframe
  678. binProb <- 0.5
  679. probSeq <- seq(binProb, 100, binProb)
  680. axProb <- readRDS("EstereoAnalysis/errorProbs_RMSE.rds")
  681. probFrame <- data.frame(error = rep(probSeq, 90*4),
  682. neurType = rep(c("Type1", "Type2", "Type3", "Type4"), each = length(probSeq)*90),
  683. dist = rep(rep(unique(allData$dist), each = length(probSeq)*9), 4),
  684. step = rep(rep(unique(allData$step), each = length(probSeq)), 10*4),
  685. prob = unlist(axProb))
  686. ```
  687. ####Plot conditional heatmap
  688. ```{r, fig.height=15, fig.width=15}
  689. # Color scale
  690. coul <- viridis::magma(10000)
  691. ###############
  692. # Types 1,2,3 #
  693. ###############
  694. # Create data frame to plot
  695. dataPlot <- probFrame[probFrame$neurType != "Type4" & (probFrame$error == 5 | probFrame$error == 10 | probFrame$error == 15), ]
  696. dataPlot$condError <- factor(dataPlot$error, levels = c("15", "10", "5"))
  697. levels(dataPlot$condError) <- c("15%", "10%", "5%")
  698. lp123 <- levelplot(prob ~ dist * step | neurType + condError, data = dataPlot,
  699. col.regions = coul,
  700. scales = list(y = list(at = unique(allData$step), labels = unique(allData$step), cex = 1.5),
  701. x = list(at = unique(allData$dist), labels = unique(allData$dist), cex = 1.5)),
  702. ylim = c(150,70),
  703. xlim = c(3,30),
  704. cuts = 100,
  705. colorkey = list(at = (seq(min(dataPlot$prob),
  706. max(dataPlot$prob),
  707. 0.0025)),
  708. labels = list(cex = 1.5),
  709. title = "P(%error <= x)"),
  710. ylab = list(label = "Step", cex = 2),
  711. xlab = list(label = "Distance", cex = 2),
  712. par.settings = list(strip.background=list(col=c("slategray1","white"))),
  713. par.strip.text=list(cex=1.5),
  714. panel = panel.levelplot.points, cex = 0) +
  715. layer_(panel.2dsmoother(..., n = 200))
  716. #########
  717. # Type4 #
  718. #########
  719. # Create data frame to plot
  720. dataPlot <- probFrame[probFrame$neurType == "Type4" & (probFrame$error == 5 | probFrame$error == 10 | probFrame$error == 15), ]
  721. dataPlot$condError <- factor(dataPlot$error, levels = c("15", "10", "5"))
  722. levels(dataPlot$condError) <- c("15%", "10%", "5%")
  723. lp4 <- levelplot(prob ~ dist * step | neurType + condError, data = dataPlot,
  724. col.regions = coul,
  725. scales = list(y = list(at = unique(allData$step), labels = unique(allData$step), cex = 1.5),
  726. x = list(at = unique(allData$dist), labels = unique(allData$dist), cex = 1.5, alternating = 3)),
  727. ylim = c(150,70),
  728. xlim = c(3,30),
  729. cuts = 100,
  730. colorkey = list(at = (seq(min(dataPlot$prob),
  731. max(dataPlot$prob),
  732. 0.0025)),
  733. labels = list(cex = 1.5),
  734. title = "P(%error <= x)"),
  735. ylab = list(label = "Step", cex = 2),
  736. xlab = list(label = "Distance", cex = 2),
  737. # main = list(label = paste("P(%Error <= x)", sep = ""), cex = 2),
  738. par.settings = list(strip.background=list(col=c("slategray1","white"))),
  739. par.strip.text=list(cex=1.5),
  740. panel = panel.levelplot.points, cex = 0) +
  741. layer_(panel.2dsmoother(..., n = 200))
  742. ```
  743. ####Arrange
  744. ```{r}
  745. # Plot
  746. setwd("EstereoFigures/")
  747. png(filename=paste("probError_full_jet.png", sep=""), type="cairo",
  748. units="in", width=24, height=15, pointsize=12,
  749. res=300)
  750. gridExtra::grid.arrange(lp123, lp4, ncol = 3, nrow = 1,
  751. layout_matrix = rbind(c(1,NA,2)),
  752. widths = c(17,1.2,7.5))
  753. dev.off()
  754. ```
  755. ##Boxplot error~dist:step
  756. ```{r, fig.width=20, fig.height=5}
  757. # Sort by step
  758. dataStep <- allData[order(allData$step), ]
  759. dataStep$x <- rep(1:10, 8559)
  760. # Color palette
  761. pal <- wes_palette("Zissou1", 10, type = "continuous")
  762. errorList <- list()
  763. # ggplot(dataStep, aes(x=factor(step), y=error, fill=factor(dist))) +
  764. # geom_boxplot() +
  765. # scale_fill_manual(values=pal) +
  766. # ylim(c(-5,100))
  767. # Plot and store in list
  768. for (i in unique(allData$neurType)) {
  769. errType <- dataStep[dataStep$neurType == i, ]
  770. meanErr <- aggregate(error ~ step + dist, errType, mean)
  771. meanErr <- meanErr[order(meanErr$step), ]
  772. errorList[[i]] <- ggplot(errType, aes(x=factor(step), y=error)) +
  773. geom_boxplot(aes(fill=interact), outlier.shape = NA) +
  774. scale_fill_manual(values=pal) +
  775. # geom_line() +
  776. # geom_boxplot() +
  777. # scale_x_continuous(breaks=seq(5,90,10), labels = seq(70,150,10)) +
  778. # scale_y_continuous(breaks=seq(2000, 16000, 2000), limits = c(2000,16000)) +
  779. # scale_colour_gradientn(name = "Distance",
  780. # colours = pal,
  781. # breaks = seq(3,30,3)) +
  782. # geom_vline(xintercept=seq(0,90,10), colour = "gray", linetype="dashed", ) +
  783. geom_smooth(method="loess", se=TRUE) +
  784. theme_bw() +
  785. theme(panel.grid.major = element_blank(),
  786. panel.grid.minor = element_blank(),
  787. legend.key.size = unit(1, "cm"),
  788. legend.key.width = unit(0.5,"cm"),
  789. plot.title = element_text(hjust = 0.5, size = 22),
  790. axis.text.x = element_text(size = 13, color = "black"),
  791. axis.text.y = element_text(size = 13, color = "black"),
  792. axis.title.x = element_text(size = 16),
  793. axis.title.y = element_text(size = 16)) +
  794. xlab("Step") +
  795. ylab("RMSE") +
  796. ggtitle(i) +
  797. scale_y_continuous(limits = c(0,200))
  798. }
  799. # Save as png
  800. # png(filename=paste("rmseEstereo2.png", sep=""), type="cairo",
  801. # units="in", width=22, height=5, pointsize=12,
  802. # res=300)
  803. gridExtra::grid.arrange(grobs = errorList, ncol = 4, nrow = 1)
  804. # dev.off()
  805. ```
  806. ```{r}
  807. data=data.frame(date=as.Date(c("2011-02-10","2011-02-10","2011-02-10","2011-02-10","2011-02-10",
  808. "2011-02-10","2011-02-10","2011-02-10","2011-02-10","2011-02-10",
  809. "2011-02-20","2011-02-20","2011-02-20","2011-02-20","2011-02-20",
  810. "2011-02-20","2011-02-20","2011-02-20","2011-02-20","2011-02-20",
  811. "2011-02-28","2011-02-28","2011-02-28","2011-02-28","2011-02-28",
  812. "2011-02-28","2011-02-28","2011-02-28","2011-02-28","2011-02-28",
  813. "2011-03-10","2011-03-10","2011-03-10","2011-03-10","2011-03-10",
  814. "2011-03-10","2011-03-10","2011-03-10","2011-03-10","2011-03-10"),format="%Y-%m-%d"),
  815. spore=c(0,1,0,1,0,
  816. 1,2,0,1,1,
  817. 8,5,6,12,7,
  818. 7,5,4,7,6,
  819. 18,24,25,32,14,
  820. 24,16,18,23,30,
  821. 27,26,36,31,22,
  822. 28,29,37,30,24),
  823. house = rep(c("Int", "Ext"), each = 5, times = 4)
  824. )
  825. data$interact <- interaction(data$date, data$house)
  826. ggplot(data,aes(x=date,y=spore)) +
  827. geom_boxplot(aes(fill=interact), outlier.shape = NA) +
  828. # scale_fill_manual(values=rep(pal,9)) +
  829. geom_smooth() +
  830. # scale_y_continuous(limits = c(0,200)) +
  831. theme(legend.position = "none")
  832. ggplot(allData,aes(x=step,y=error)) +
  833. geom_boxplot(aes(fill=interact), outlier.shape = NA) +
  834. scale_fill_manual(values=rep(pal,9)) +
  835. geom_smooth(method="loess") +
  836. scale_y_continuous(limits = c(0,200)) +
  837. theme(legend.position = "none")
  838. ```