modelFree_stereology_MSE.Rmd 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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(lattice)
  23. library(dplyr)
  24. library(ggplot2)
  25. library(ggpubr)
  26. library(ggridges)
  27. library(mltools)
  28. library(data.table)
  29. library(caret)
  30. library(interactions)
  31. library(data.table)
  32. library(wesanderson)
  33. options(digits = 10)
  34. # https://rstudio-pubs-static.s3.amazonaws.com/297778_5fce298898d64c81a4127cf811a9d486.html
  35. ###############################
  36. # Add continuous color legend #
  37. ###############################
  38. legend.col <- function(col, lev){
  39. opar <- par
  40. n <- length(col)
  41. bx <- par("usr")
  42. box.cx <- c(bx[2] + (bx[2] - bx[1]) / 1000,
  43. bx[2] + (bx[2] - bx[1]) / 1000 + (bx[2] - bx[1]) / 50)
  44. box.cy <- c(bx[3], bx[3])
  45. box.sy <- (bx[4] - bx[3]) / n
  46. xx <- rep(box.cx, each = 2)
  47. par(xpd = TRUE)
  48. for(i in 1:n){
  49. yy <- c(box.cy[1] + (box.sy * (i - 1)),
  50. box.cy[1] + (box.sy * (i)),
  51. box.cy[1] + (box.sy * (i)),
  52. box.cy[1] + (box.sy * (i - 1)))
  53. polygon(xx, yy, col = col[i], border = col[i])
  54. }
  55. par(new = TRUE)
  56. plot(0, 0, type = "n",
  57. ylim = c(min(lev), max(lev)),
  58. yaxt = "n", ylab = "",
  59. xaxt = "n", xlab = "",
  60. frame.plot = FALSE)
  61. axis(side = 4, las = 2, at = lev, tick = FALSE, line = .15)
  62. par <- opar
  63. }
  64. ```
  65. ##Load data
  66. ```{r}
  67. # Get file paths
  68. axonNames <- list.files(paste("EstereoDataPlanos/", sep=""), full.names=T)
  69. # Load matlab file
  70. axonFiles <- lapply(axonNames, function(x) readMat(x))
  71. names(axonFiles) <- c("Type1", "Type2", "Type3", "Type4")
  72. # Extract data
  73. # errorMatrix contains 4 arrays, one per neuron type
  74. # within each array, the dimensions corresponds to step:dist:neuron
  75. # this means that each array has as many matrices as neuron of a certain type
  76. dist <- lapply(axonFiles, function(x) x$Distancias.Entre.Planos)[[1]]
  77. step <- lapply(axonFiles, function(x) x$Step.Lengths)[[1]]
  78. errorMatrix <- lapply(axonFiles, function(x) x$MATRIZ.ERROR.LENGTH)
  79. lpMatrix <- lapply(axonFiles, function(x) x$MATRIZ.ESTIMATED.AXON.LENGTH)
  80. lrVals <- lapply(axonFiles, function(x) x$AXON.REAL.LENGTH)
  81. # Get number of neurons per type
  82. numberTypes <- unlist(lapply(errorMatrix, function(x) dim(x)[3]))
  83. # Vectorizing the arrays goes as follows: neuron, dist, step
  84. errorVector <- unlist(lapply(errorMatrix, function(x) as.vector(x)))
  85. lpVector <- unlist(lapply(lpMatrix, function(x) as.vector(x)))
  86. lrVector <- unlist(lapply(lrVals, function(x) as.vector(x)))
  87. # Store in data frames
  88. allData <- data.frame(id = rep(1:sum(numberTypes), each = 90),
  89. neurType = rep(c("Type1", "Type2", "Type3", "Type4"), times = 90*numberTypes),
  90. dist = rep(rep(dist, each = 9), sum(numberTypes)),
  91. step = rep(rep(step, 10), sum(numberTypes)),
  92. error = abs(errorVector),
  93. error2 = errorVector,
  94. LRe = lpVector,
  95. LR = rep(lrVector, each = 90))
  96. allData$errorRaw <- allData$LR - allData$LRe
  97. allData$interact <- interaction(allData$step, allData$dist, lex.order = T)
  98. allData$interact2 <- interaction(allData$neurType, allData$step, allData$dist, lex.order = T)
  99. ```
  100. ## Estimate MSE
  101. ```{r}
  102. mseDistep <- by(allData, allData$interact2, function(x) sqrt(mse(x$LRe, x$LR)))
  103. ```
  104. ```{r, fig.width=15}
  105. # mseDist <- by(allData, allData$dist, function(x) sqrt(mse(x$LRe, x$LR)))
  106. # mseStep <- by(allData, allData$step, function(x) sqrt(mse(x$LRe, x$LR)))
  107. # mseDistep <- by(allData, allData$interact2, function(x) sqrt(mse(x$LRe, x$LR)))
  108. #
  109. # par(mfrow=c(1,3))
  110. #
  111. # plot(seq(3,30,3), mseDist,
  112. # xaxt = "n", xlab = "Dist", ylab = "RMSE",
  113. # pch = 21, bg = "firebrick", col = NA,
  114. # main = "Test Error ~ Distancia",
  115. # cex = 1.5, cex.axis = 1.5, cex.main = 2, cex.lab = 1.5)
  116. # lines(seq(3,30,3), mseDist)
  117. # axis(side=1, at=seq(3,30,3), cex.axis = 1.5)
  118. # grid()
  119. #
  120. # plot(seq(70,150,10), mseStep,
  121. # xaxt = "n", xlab = "Step", ylab = "RMSE",
  122. # pch = 21, bg = "firebrick", col = NA,
  123. # main = "Test Error ~ Step",
  124. # cex = 1.5, cex.axis = 1.5, cex.main = 2, cex.lab = 1.5)
  125. # lines(seq(70,150,10), mseStep)
  126. # axis(side=1, at=seq(70,150,10), cex.axis = 1.5)
  127. # grid()
  128. #
  129. # plot(1:90, mseDistep,
  130. # xaxt = "n", xlab = "Step", ylab = "RMSE",
  131. # pch = 21, bg = rep(wes_palette("Zissou1", 10, type = "continuous"), 9), col = NA,
  132. # main = "Test Error ~ Step:Dist",
  133. # cex = 1.5, cex.axis = 1.5, cex.main = 2, cex.lab = 1.5)
  134. #
  135. # for (i in seq(0,90,10)) {
  136. # lines((i+1):(i+10), mseDistep[(i+1):(i+10)])
  137. # }
  138. #
  139. # axis(side=1, at=seq(5,90,10), las = 1, srt = 60, labels = seq(70, 150, 10), cex.axis = 1.5)
  140. # abline(v=seq(0,90,10), lty="dashed", col = "gray")
  141. #
  142. # legend.col(wes_palette("Zissou1", 10, type = "continuous"), seq(3,30,3))
  143. ```
  144. ###RMSE ggplot (dashed lines)
  145. ```{r, fig.height=5, fig.width=20}
  146. # Store MSE in data frame
  147. mseFrame <- data.frame(mse = as.numeric(mseDistep),
  148. x = rep(1:90, 4),
  149. step = rep(rep(seq(70,150,10), each = 10), 4),
  150. dist = rep(rep(seq(3,30,3), 9), 4),
  151. axType = rep(c("Type1", "Type2", "Type3", "Type4"), each = 90))
  152. # Color palette
  153. pal <- wes_palette("Zissou1", 10, type = "continuous")
  154. rmseList <- list()
  155. # Plot and store in list
  156. for (i in unique(mseFrame$axType)) {
  157. mseType <- mseFrame[mseFrame$axType == i, ]
  158. rmseList[[i]] <- ggplot(data=mseType, aes(x=x, y=mse, group=step)) +
  159. geom_line() +
  160. geom_point(aes(colour = dist)) +
  161. scale_x_continuous(breaks=seq(5,90,10), labels = seq(70,150,10)) +
  162. scale_y_continuous(breaks=seq(2000, 16000, 2000), limits = c(2000,16000)) +
  163. scale_colour_gradientn(name = "Distance",
  164. colours = pal,
  165. breaks = seq(3,30,3)) +
  166. # geom_vline(xintercept=seq(0,90,10), colour = "gray", linetype="dashed", ) +
  167. # geom_smooth() +
  168. theme_bw() +
  169. theme(panel.grid.major = element_blank(),
  170. panel.grid.minor = element_blank(),
  171. legend.key.size = unit(1.9, "cm"),
  172. legend.key.width = unit(0.5,"cm"),
  173. plot.title = element_text(hjust = 0.5, size = 22),
  174. axis.text.x = element_text(size = 13, color = "black"),
  175. axis.text.y = element_text(size = 13, color = "black"),
  176. axis.title.x = element_text(size = 16),
  177. axis.title.y = element_text(size = 16),
  178. panel.background = element_rect(colour = "black",size = 1)) +
  179. xlab("Step") +
  180. ylab("RMSE") +
  181. ggtitle(i)
  182. }
  183. # Save as png
  184. png(filename=paste("rmseEstereo2.png", sep=""), type="cairo",
  185. units="in", width=22, height=5, pointsize=12,
  186. res=300)
  187. gridExtra::grid.arrange(grobs = rmseList, ncol = 4, nrow = 1)
  188. dev.off()
  189. ```
  190. ###RMSE ggplot (smooth bands)
  191. ```{r, fig.height=5, fig.width=20}
  192. # Store MSE in data frame
  193. mseFrame <- data.frame(mse = as.numeric(mseDistep),
  194. x = rep(1:90, 4),
  195. step = rep(rep(seq(70,150,10), each = 10), 4),
  196. dist = rep(rep(seq(3,30,3), 9), 4),
  197. axType = rep(c("Type1", "Type2", "Type3", "Type4"), each = 90))
  198. # Color palette
  199. pal <- wes_palette("Zissou1", 10, type = "continuous")
  200. rmseList <- list()
  201. # Plot and store in list
  202. for (i in unique(mseFrame$axType)) {
  203. mseType <- mseFrame[mseFrame$axType == i, ]
  204. rmseList[[i]] <- ggplot(data=mseType, aes(x=x, y=mse, group=step)) +
  205. # geom_line() +
  206. geom_smooth(colour = "black", method = "loess", span = 1.2, size = 0.5) +
  207. geom_point(aes(colour = dist)) +
  208. geom_point(shape = 1, colour = "black") +
  209. scale_x_continuous(breaks=seq(5,90,10), labels = seq(70,150,10)) +
  210. scale_y_continuous(breaks=seq(2000, 16000, 2000), limits = c(2000,16000)) +
  211. scale_colour_gradientn(name = "Distance",
  212. colours = pal,
  213. breaks = seq(3,30,3)) +
  214. # geom_vline(xintercept=seq(0,90,10), colour = "gray", linetype="dashed", ) +
  215. theme_bw() +
  216. theme(panel.grid.major = element_blank(),
  217. panel.grid.minor = element_blank(),
  218. legend.key.size = unit(1.9, "cm"),
  219. legend.key.width = unit(0.5,"cm"),
  220. plot.title = element_text(hjust = 0.5, size = 22),
  221. axis.text.x = element_text(size = 13, color = "black"),
  222. axis.text.y = element_text(size = 13, color = "black"),
  223. axis.title.x = element_text(size = 16),
  224. axis.title.y = element_text(size = 16),
  225. panel.background = element_rect(colour = "black",size = 1)) +
  226. xlab("Step") +
  227. ylab("RMSE") +
  228. ggtitle(i)
  229. }
  230. # Save as png
  231. png(filename=paste("rmseEstereo_loess_border.png", sep=""), type="cairo",
  232. units="in", width=22, height=5, pointsize=12,
  233. res=300)
  234. gridExtra::grid.arrange(grobs = rmseList, ncol = 4, nrow = 1)
  235. dev.off()
  236. ```
  237. ###RMSE smooth + examples
  238. ```{r, fig.height=5, fig.width=20}
  239. # Color palette
  240. pal <- wes_palette("Zissou1", 10, type = "continuous")
  241. list370 <- list()
  242. list30150 <- list()
  243. # Plot and store in list
  244. for (i in unique(allData$neurType)) {
  245. #EXAMPLE 3.70
  246. data2d <- allData[allData$neurType == i & allData$dist == 3 & allData$step == 70, ]
  247. dataReal <- data.frame(LR = data2d$LR, LRe = data2d$LR)
  248. list370[[i]] <- ggplot(data=data2d, aes(x=LRe, y=LR)) +
  249. geom_point(colour = "black", size = 0.6) +
  250. # geom_point(shape = 1, colour = "black") +
  251. geom_point(data = dataReal, colour = "red", size = 0.6) +
  252. # geom_point(data = dataReal, shape = 1, colour = "black") +
  253. theme_bw() +
  254. theme(panel.grid.major = element_blank(),
  255. panel.grid.minor = element_blank(),
  256. plot.title = element_text(hjust = 0.5, size = 12),
  257. axis.title.x=element_text(size = 10),
  258. axis.text.x=element_blank(),
  259. axis.ticks.x=element_blank(),
  260. axis.title.y=element_text(size = 10),
  261. axis.text.y=element_blank(),
  262. axis.ticks.y=element_blank(),
  263. panel.background = element_rect(colour = "black",size = 1)) +
  264. ggtitle("70.3") +
  265. xlab("Predicted length") +
  266. ylab("Real Length")
  267. #EXAMPLE 30.150
  268. data2d <- allData[allData$neurType == i & allData$dist == 30 & allData$step == 150, ]
  269. dataReal <- data.frame(LR = data2d$LR, LRe = data2d$LR)
  270. list30150[[i]] <- ggplot(data=data2d, aes(x=LRe, y=LR)) +
  271. geom_point(colour = "black", size = 0.6) +
  272. # geom_point(shape = 1, colour = "black") +
  273. geom_point(data = dataReal, colour = "red", size = 0.6) +
  274. # geom_point(data = dataReal, shape = 1, colour = "black") +
  275. theme_bw() +
  276. theme(panel.grid.major = element_blank(),
  277. panel.grid.minor = element_blank(),
  278. plot.title = element_text(hjust = 0.5, size = 12),
  279. axis.title.x= element_text(size = 10),
  280. axis.text.x=element_blank(),
  281. axis.ticks.x=element_blank(),
  282. axis.title.y= element_text(size = 10),
  283. axis.text.y=element_blank(),
  284. axis.ticks.y=element_blank(),
  285. panel.background = element_rect(colour = "black",size = 1)) +
  286. ggtitle("150.30") +
  287. xlab("Real Model") +
  288. ylab("Real Length")
  289. }
  290. # Save as png
  291. for (i in unique(allData$neurType)) {
  292. png(filename=paste("rmseEstereo_loess_border_examples_", i, ".png", sep=""), type="cairo",
  293. units="in", width=2.5, height=1.5, pointsize=12,
  294. res=300)
  295. gridExtra::grid.arrange(grobs = c(list370[i], list30150[i]), ncol = 2, nrow = 1)
  296. dev.off()
  297. }
  298. ```
  299. ##Mean error
  300. ```{r, fig.height=10, fig.width=20}
  301. # Estimate mean residual error
  302. meanFrame <- aggregate(error ~ dist + step + neurType, allData, mean)
  303. # Library
  304. library(latticeExtra)
  305. # Contour color palette
  306. # colfunc <- colorRampPalette(c("navy","royalblue","springgreen","gold","yellow"))
  307. # coul <- colfunc(1000)
  308. # library(viridisLite)
  309. coul <- pals::parula(10000)
  310. # Store heatmaps
  311. heatList <- list()
  312. smoothList <- list()
  313. for (i in c("Type1", "Type2", "Type3", "Type4")) {
  314. heatList[[i]] <- levelplot(error ~ dist * step, data = meanFrame[meanFrame$neurType == i, ], col.regions = coul,
  315. scales = list(y = list(at = unique(allData$step), labels = unique(allData$step), cex = 1.5),
  316. x = list(at = unique(allData$dist), labels = unique(allData$dist)), cex = 1.5),
  317. ylim = c(155, 65),
  318. ylab = list(label = "Step", cex = 1.5),
  319. xlab = list(label = "Distance", cex = 1.5),
  320. main = list(label = paste(i, "Mean % Error"), cex = 2),
  321. colorkey = list(at = (seq(min(meanFrame[meanFrame$neurType == i, "error"]),
  322. max(meanFrame[meanFrame$neurType == i, "error"]),
  323. 0.05)),
  324. cex = 1.2))
  325. smoothList[[i]] <- levelplot(error ~ dist * step, data = meanFrame[meanFrame$neurType == i, ], col.regions = coul,
  326. scales = list(y = list(at = unique(allData$step), labels = unique(allData$step), cex = 1.3),
  327. x = list(at = unique(allData$dist), labels = unique(allData$dist)), cex = 1.3),
  328. ylim = c(150,70),
  329. xlim = c(3,30),
  330. ylab = list(label = "Step", cex = 1.5),
  331. xlab = list(label = "Distance", cex = 1.5),
  332. main = list(label = paste(i, "Smooth"), cex = 1.5),
  333. cuts = 20,
  334. colorkey = list(at = (seq(min(meanFrame[meanFrame$neurType == i, "error"]),
  335. max(meanFrame[meanFrame$neurType == i, "error"]),
  336. 0.05)),
  337. cex = 1.2),
  338. panel = panel.levelplot.points, cex = 0) +
  339. layer_(panel.2dsmoother(..., n = 200))
  340. }
  341. # Plot
  342. png(filename=paste("meanErrorEstereo.png", sep=""), type="cairo",
  343. units="in", width=22, height=10, pointsize=12,
  344. res=300)
  345. gridExtra::grid.arrange(grobs = c(heatList, smoothList), ncol = 4, nrow = 2)
  346. dev.off()
  347. ```
  348. ##Mean error (Type123 vs 4)
  349. ```{r, fig.height=10, fig.width=20}
  350. # Estimate mean residual error
  351. meanFrame <- aggregate(error ~ dist + step + neurType, allData, mean)
  352. # Library
  353. library(latticeExtra)
  354. # Contour color palette
  355. # colfunc <- colorRampPalette(c("navy","royalblue","springgreen","gold","yellow"))
  356. # coul <- colfunc(1000)
  357. # library(viridisLite)
  358. coul <- pals::parula(10000)
  359. # Store heatmaps
  360. heatList <- list()
  361. smoothList <- list()
  362. # Limits
  363. min123 <- min(meanFrame[meanFrame$neurType != "Type4", "error"])
  364. max123 <- max(meanFrame[meanFrame$neurType != "Type4", "error"])
  365. for (i in c("Type1", "Type2", "Type3", "Type4")) {
  366. if (i != "Type4") {
  367. heatList[[i]] <- levelplot(error ~ dist * step, data = meanFrame[meanFrame$neurType == i, ], col.regions = coul,
  368. scales = list(y = list(at = unique(allData$step), labels = unique(allData$step), cex = 1.5),
  369. x = list(at = unique(allData$dist), labels = unique(allData$dist)), cex = 1.5),
  370. ylim = c(155, 65),
  371. ylab = list(label = "Step", cex = 1.5),
  372. xlab = list(label = "Distance", cex = 1.5),
  373. main = list(label = paste(i, "Mean % Error"), cex = 2),
  374. colorkey = list(at = (seq(min123,
  375. max123,
  376. 0.05)),
  377. cex = 1.2))
  378. smoothList[[i]] <- levelplot(error ~ dist * step, data = meanFrame[meanFrame$neurType == i, ], col.regions = coul,
  379. scales = list(y = list(at = unique(allData$step), labels = unique(allData$step), cex = 1.3),
  380. x = list(at = unique(allData$dist), labels = unique(allData$dist)), cex = 1.3),
  381. ylim = c(150,70),
  382. xlim = c(3,30),
  383. ylab = list(label = "Step", cex = 1.5),
  384. xlab = list(label = "Distance", cex = 1.5),
  385. main = list(label = paste(i, "Smooth"), cex = 1.5),
  386. cuts = 20,
  387. colorkey = list(at = (seq(min123,
  388. max123,
  389. 0.05)),
  390. cex = 1.2),
  391. panel = panel.levelplot.points, cex = 0) +
  392. layer_(panel.2dsmoother(..., n = 200))
  393. } else {
  394. heatList[[i]] <- levelplot(error ~ dist * step, data = meanFrame[meanFrame$neurType == i, ], col.regions = coul,
  395. scales = list(y = list(at = unique(allData$step), labels = unique(allData$step), cex = 1.5),
  396. x = list(at = unique(allData$dist), labels = unique(allData$dist)), cex = 1.5),
  397. ylim = c(155, 65),
  398. ylab = list(label = "Step", cex = 1.5),
  399. xlab = list(label = "Distance", cex = 1.5),
  400. main = list(label = paste(i, "Mean % Error"), cex = 2),
  401. colorkey = list(at = (seq(min(meanFrame[meanFrame$neurType == i, "error"]),
  402. max(meanFrame[meanFrame$neurType == i, "error"]),
  403. 0.05)),
  404. cex = 1.2))
  405. smoothList[[i]] <- levelplot(error ~ dist * step, data = meanFrame[meanFrame$neurType == i, ], col.regions = coul,
  406. scales = list(y = list(at = unique(allData$step), labels = unique(allData$step), cex = 1.3),
  407. x = list(at = unique(allData$dist), labels = unique(allData$dist)), cex = 1.3),
  408. ylim = c(150,70),
  409. xlim = c(3,30),
  410. ylab = list(label = "Step", cex = 1.5),
  411. xlab = list(label = "Distance", cex = 1.5),
  412. main = list(label = paste(i, "Smooth"), cex = 1.5),
  413. cuts = 20,
  414. colorkey = list(at = (seq(min(meanFrame[meanFrame$neurType == i, "error"]),
  415. max(meanFrame[meanFrame$neurType == i, "error"]),
  416. 0.05)),
  417. cex = 1.2),
  418. panel = panel.levelplot.points, cex = 0) +
  419. layer_(panel.2dsmoother(..., n = 200))
  420. }
  421. }
  422. # Plot
  423. png(filename=paste("meanErrorEstereo_DIFY.png", sep=""), type="cairo",
  424. units="in", width=22, height=10, pointsize=12,
  425. res=300)
  426. gridExtra::grid.arrange(grobs = c(heatList, smoothList), ncol = 4, nrow = 2)
  427. dev.off()
  428. ```
  429. ##Errors vs cumul probability
  430. ```{r, fig.height=5, fig.width=20}
  431. # Inverse quantile
  432. quantInv <- function(distr, value) ecdf(distr)(value)
  433. # Function to apply to all axon types
  434. quantType <- function(peFrame, probSeq) {
  435. probList <- list()
  436. for (i in unique(peFrame$interact)) {
  437. dataProb <- peFrame[peFrame$interact == i, "error"]
  438. probVec <- numeric()
  439. for (j in probSeq) {
  440. errProb <- quantInv(dataProb, j) - quantInv(dataProb, -j)
  441. probVec <- c(probVec, errProb)
  442. }
  443. probList[[i]] <- probVec
  444. }
  445. return(probList)
  446. }
  447. # Define errors for which to calculate probability
  448. binProb <- 0.5
  449. probSeq <- seq(binProb, 100, binProb)
  450. # Store axon types in lists
  451. frameList <- list(Type1 = allData[allData$neurType == "Type1", ],
  452. Type2 = allData[allData$neurType == "Type2", ],
  453. Type3 = allData[allData$neurType == "Type3", ],
  454. Type4 = allData[allData$neurType == "Type4", ])
  455. axProb <- lapply(frameList, function(x) quantType(x, probSeq))
  456. saveRDS(axProb, "errorProbs_RMSE.rds")
  457. ```
  458. ######Plot heatmap 5, 10
  459. ```{r, fig.height=10, fig.width=15}
  460. library(latticeExtra)
  461. # Reformat as dataframe
  462. binProb <- 0.5
  463. probSeq <- seq(binProb, 100, binProb)
  464. axProb <- readRDS("errorProbs_RMSE.rds")
  465. probFrame <- data.frame(error = rep(probSeq, 90*4),
  466. neurType = rep(c("Type1", "Type2", "Type3", "Type4"), each = length(probSeq)*90),
  467. dist = rep(rep(unique(allData$dist), each = length(probSeq)*9), 4),
  468. step = rep(rep(unique(allData$step), each = length(probSeq)), 10*4),
  469. prob = unlist(axProb))
  470. # Plot contour plot for 5% error
  471. coul <- viridis::magma(10000)
  472. # Store heatmaps
  473. heatProbList <- list()
  474. smoothProbList <- list()
  475. for (i in c("Type1", "Type2", "Type3", "Type4")) {
  476. levelList1 <- list()
  477. levelList2 <- list()
  478. for (j in c(5, 10, 20)) {
  479. dataPlot <- probFrame[probFrame$neurType == i & probFrame$error == j, ]
  480. levelList1[[as.character(j)]] <- levelplot(prob ~ dist * step, data = dataPlot,
  481. col.regions = coul,
  482. scales = list(y = list(at = unique(allData$step), labels = unique(allData$step), cex = 1.3),
  483. x = list(at = unique(allData$dist), labels = unique(allData$dist)), cex = 1.3),
  484. ylim = c(155,65),
  485. colorkey = list(at = (seq(min(dataPlot$prob),
  486. max(dataPlot$prob),
  487. 0.0025))),
  488. ylab = list(label = "Step", cex = 1.5),
  489. xlab = list(label = "Distance", cex = 1.5),
  490. main = list(label = paste(i, " P(Error <= ", j, " %)", sep = ""), cex = 2))
  491. levelList2[[as.character(j)]] <- levelplot(prob ~ dist * step, data = dataPlot,
  492. col.regions = coul,
  493. scales = list(y = list(at = unique(allData$step), labels = unique(allData$step), cex = 1.3),
  494. x = list(at = unique(allData$dist), labels = unique(allData$dist)), cex = 1.3),
  495. ylim = c(150,70),
  496. xlim = c(3,30),
  497. cuts = 20,
  498. colorkey = list(at = (seq(min(dataPlot$prob),
  499. max(dataPlot$prob),
  500. 0.0025))),
  501. ylab = list(label = "Step", cex = 1.5),
  502. xlab = list(label = "Distance", cex = 1.5),
  503. main = list(label = paste(i, " P(Error <= ", j, " %)", sep = ""), cex = 2),
  504. panel = panel.levelplot.points, cex = 0) +
  505. layer_(panel.2dsmoother(..., n = 200))
  506. }
  507. heatProbList[[i]] <- levelList1
  508. smoothProbList[[i]] <- levelList2
  509. }
  510. smoothProb5 <- lapply(smoothProbList, function(x) x[[1]])
  511. smoothProb10 <- lapply(smoothProbList, function(x) x[[2]])
  512. smoothProb20 <- lapply(smoothProbList, function(x) x[[3]])
  513. # Plot
  514. # setwd("EstereoAnalysis/")
  515. for (i in c("Type1", "Type2", "Type3", "Type4")) {
  516. png(filename=paste("errorProbabilities_heatmap_", i, "_MSE.png", sep=""), type="cairo",
  517. units="in", width=15, height=10, pointsize=12,
  518. res=300)
  519. gridExtra::grid.arrange(grobs = c(heatProbList[[i]], smoothProbList[[i]]), ncol = 3, nrow = 2)
  520. dev.off()
  521. }
  522. ```
  523. ##Common plot
  524. ```{r, fig.height=40, fig.width=40}
  525. gridExtra::grid.arrange(grobs = c(rmseList, smoothList, smoothProb5, smoothProb10), ncol = 4, nrow = 4)
  526. ```
  527. ##Boxplot error~dist:step
  528. ```{r, fig.width=20, fig.height=5}
  529. # Sort by step
  530. dataStep <- allData[order(allData$step), ]
  531. dataStep$x <- rep(1:10, 8559)
  532. # Color palette
  533. pal <- wes_palette("Zissou1", 10, type = "continuous")
  534. errorList <- list()
  535. # ggplot(dataStep, aes(x=factor(step), y=error, fill=factor(dist))) +
  536. # geom_boxplot() +
  537. # scale_fill_manual(values=pal) +
  538. # ylim(c(-5,100))
  539. # Plot and store in list
  540. for (i in unique(allData$neurType)) {
  541. errType <- dataStep[dataStep$neurType == i, ]
  542. meanErr <- aggregate(error ~ step + dist, errType, mean)
  543. meanErr <- meanErr[order(meanErr$step), ]
  544. errorList[[i]] <- ggplot(errType, aes(x=factor(step), y=error)) +
  545. geom_boxplot(aes(fill=interact), outlier.shape = NA) +
  546. scale_fill_manual(values=pal) +
  547. # geom_line() +
  548. # geom_boxplot() +
  549. # scale_x_continuous(breaks=seq(5,90,10), labels = seq(70,150,10)) +
  550. # scale_y_continuous(breaks=seq(2000, 16000, 2000), limits = c(2000,16000)) +
  551. # scale_colour_gradientn(name = "Distance",
  552. # colours = pal,
  553. # breaks = seq(3,30,3)) +
  554. # geom_vline(xintercept=seq(0,90,10), colour = "gray", linetype="dashed", ) +
  555. geom_smooth(method="loess", se=TRUE) +
  556. theme_bw() +
  557. theme(panel.grid.major = element_blank(),
  558. panel.grid.minor = element_blank(),
  559. legend.key.size = unit(1, "cm"),
  560. legend.key.width = unit(0.5,"cm"),
  561. plot.title = element_text(hjust = 0.5, size = 22),
  562. axis.text.x = element_text(size = 13, color = "black"),
  563. axis.text.y = element_text(size = 13, color = "black"),
  564. axis.title.x = element_text(size = 16),
  565. axis.title.y = element_text(size = 16)) +
  566. xlab("Step") +
  567. ylab("RMSE") +
  568. ggtitle(i) +
  569. scale_y_continuous(limits = c(0,200))
  570. }
  571. # Save as png
  572. # png(filename=paste("rmseEstereo2.png", sep=""), type="cairo",
  573. # units="in", width=22, height=5, pointsize=12,
  574. # res=300)
  575. gridExtra::grid.arrange(grobs = errorList, ncol = 4, nrow = 1)
  576. # dev.off()
  577. ```
  578. ```{r}
  579. data=data.frame(date=as.Date(c("2011-02-10","2011-02-10","2011-02-10","2011-02-10","2011-02-10",
  580. "2011-02-10","2011-02-10","2011-02-10","2011-02-10","2011-02-10",
  581. "2011-02-20","2011-02-20","2011-02-20","2011-02-20","2011-02-20",
  582. "2011-02-20","2011-02-20","2011-02-20","2011-02-20","2011-02-20",
  583. "2011-02-28","2011-02-28","2011-02-28","2011-02-28","2011-02-28",
  584. "2011-02-28","2011-02-28","2011-02-28","2011-02-28","2011-02-28",
  585. "2011-03-10","2011-03-10","2011-03-10","2011-03-10","2011-03-10",
  586. "2011-03-10","2011-03-10","2011-03-10","2011-03-10","2011-03-10"),format="%Y-%m-%d"),
  587. spore=c(0,1,0,1,0,
  588. 1,2,0,1,1,
  589. 8,5,6,12,7,
  590. 7,5,4,7,6,
  591. 18,24,25,32,14,
  592. 24,16,18,23,30,
  593. 27,26,36,31,22,
  594. 28,29,37,30,24),
  595. house = rep(c("Int", "Ext"), each = 5, times = 4)
  596. )
  597. data$interact <- interaction(data$date, data$house)
  598. ggplot(data,aes(x=date,y=spore)) +
  599. geom_boxplot(aes(fill=interact), outlier.shape = NA) +
  600. # scale_fill_manual(values=rep(pal,9)) +
  601. geom_smooth() +
  602. # scale_y_continuous(limits = c(0,200)) +
  603. theme(legend.position = "none")
  604. ggplot(allData,aes(x=step,y=error)) +
  605. geom_boxplot(aes(fill=interact), outlier.shape = NA) +
  606. scale_fill_manual(values=rep(pal,9)) +
  607. geom_smooth(method="loess") +
  608. scale_y_continuous(limits = c(0,200)) +
  609. theme(legend.position = "none")
  610. ```