modelFree_spheres_MSE_figures.Rmd 37 KB

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