dataforPepo.Rmd 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. ##Libraries and functions
  20. ```{r}
  21. suppressMessages(library(R.matlab)) #import mat files
  22. ```
  23. ##PROJECTIONS
  24. ###Cumulative probability
  25. ####Format and save
  26. ```{r}
  27. # Load rds
  28. probList <- readRDS("ProyeccionAnalysis/cumProb_5CV.rds")
  29. binProb <- 0.5
  30. probSeq <- seq(binProb, 100, binProb)
  31. # Store as long format data frame
  32. probFrame <- data.frame(axonType = rep(c("Type1", "Type2", "Type3", "Type4"), each = 3*200),
  33. Plane = rep(rep(c("XY", "XZ", "YZ"), each = 200),4),
  34. error = rep(probSeq, 12),
  35. cumulProb = unlist(probList))
  36. # Save as csv
  37. write.csv(probFrame, "dataPepo/projection_error_cumulativeProbability.csv", sep=",", row.names = FALSE)
  38. ```
  39. ###Error density
  40. ####Estimation
  41. ```{r}
  42. # Load 5-fold CV
  43. axList <- readRDS("ProyeccionAnalysis/modelBased_projections_5CV_allAxons.rds")
  44. # Estimate and store in list
  45. peList <- list()
  46. peCount <- 1
  47. lrLength <- list()
  48. for (h in c("Type1", "Type2", "Type3", "Type4")) {
  49. for (i in 1:5) {
  50. for (j in c("XY", "XZ", "YZ")) {
  51. # Extract LR values
  52. lr <- axList[[h]]$sets[[i]]
  53. lr <- lr[lr$Plane == j, "LR"]
  54. # Extract LP values
  55. lp <- axList[[h]]$cv[[i]][[j]][[1]]
  56. for (k in 1:length(lr)) {
  57. # Divide errors by LR to get porcentual errors
  58. pe <- lp[k, ] / lr[k]
  59. # Store in list
  60. peList[[peCount]] <- pe*100
  61. peCount <-peCount + 1
  62. }
  63. }
  64. }
  65. lrLength[h] <- length(lr)
  66. }
  67. # Store in data frame
  68. lrLength <- unlist(lrLength)
  69. peFrame <- data.frame(pe = unlist(peList),
  70. neurType = rep(c("Type1", "Type2", "Type3", "Type4"), times = lrLength*5*3*100),
  71. Plane = c(rep(rep(c("XY", "XZ", "YZ"), each = lrLength[1]*100), 5),
  72. rep(rep(c("XY", "XZ", "YZ"), each = lrLength[2]*100), 5),
  73. rep(rep(c("XY", "XZ", "YZ"), each = lrLength[3]*100), 5),
  74. rep(rep(c("XY", "XZ", "YZ"), each = lrLength[4]*100), 5)))
  75. peFrame$interact <- interaction(peFrame$neurType, peFrame$Plane, lex.order=T)
  76. # Find mean bandwidth
  77. bwFrame <- aggregate(pe ~ neurType + Plane, peFrame, function(x) density(x)$bw)
  78. # Estimate density
  79. densList <- by(peFrame$pe, peFrame$interact, function(x) density(x, bw = mean(bwFrame$pe)))
  80. ```
  81. ####Format and save
  82. ```{r}
  83. # Extract from list
  84. densXY <- lapply(densList, function(x) cbind(error = x$x, density = x$y))
  85. densFrame <- data.frame(axonType = rep(c("Type1", "Type2", "Type3", "Type4"), each = 512*3),
  86. Plane = rep(rep(c("XY", "XZ", "YZ"), each = 512), 4))
  87. # Store in long format data frame
  88. densFrame <- cbind(densFrame, do.call(rbind, densXY))
  89. # Save density as csv
  90. write.csv(densFrame, "dataPepo/projection_error_Densities.csv", sep=",", row.names = FALSE)
  91. # Save original data as csv
  92. oriFrame <- cbind(axonType = peFrame[, "neurType"], Plane = peFrame[, "Plane"], error = peFrame[, "pe"], data.frame(abserror = abs(peFrame$pe)))
  93. write.csv(oriFrame, "dataPepo/projection_error_OriginalData.csv", sep=",", row.names = FALSE)
  94. ```
  95. ###Mean errors
  96. ####Estimate, format and save
  97. ```{r}
  98. # Mean absolute error
  99. meanErr <- aggregate(abs(pe) ~ Plane + neurType, peFrame, mean)
  100. # SD absolute error
  101. sdErr <- aggregate(abs(pe) ~ Plane + neurType, peFrame, sd)
  102. # SE absolute error
  103. seErr <- aggregate(abs(pe) ~ Plane + neurType, peFrame, sjstats::se)
  104. # Common data frame
  105. meansdErr <- data.frame(axonType = meanErr$neurType,
  106. Plane = meanErr$Plane,
  107. mean = meanErr$`abs(pe)`,
  108. sd = sdErr$`abs(pe)`,
  109. se = seErr$`abs(pe)`,
  110. ci95 = 1.96*seErr$`abs(pe)`)
  111. # Save as csv
  112. write.csv(meansdErr, "dataPepo/projection_error_MeanSD.csv", sep=",", row.names = FALSE)
  113. ```
  114. ###Five-number summary
  115. ####Format and save
  116. ```{r}
  117. # Store boxplot data
  118. boxData <- boxplot(abs(pe) ~ Plane + neurType, peFrame)
  119. fiveNum <- boxData$stats
  120. # Reformat as data frame
  121. fiveNum.frame <- data.frame(axonType = rep(c("Type1", "Type2", "Type3", "Type4"), each = 15),
  122. Plane = rep(rep(c("XY", "XZ", "YZ"), each = 5), 4),
  123. quantileType = rep(c("lowlim", "p25", "p50", "p75", "uplim"), 12),
  124. quantileDat = as.vector(fiveNum))
  125. # Save
  126. write.csv(fiveNum.frame, "dataPepo/projection_error_boxplot.csv", sep=",", row.names = FALSE)
  127. ```
  128. ##STEREO PLANES
  129. ###Load data
  130. ```{r}
  131. # Get file paths
  132. axonNames <- list.files(paste("EstereoDataPlanos/", sep=""), full.names=T)
  133. # Load matlab file
  134. axonFiles <- lapply(axonNames, function(x) readMat(x))
  135. names(axonFiles) <- c("Type1", "Type2", "Type3", "Type4")
  136. # Extract data
  137. # errorMatrix contains 4 arrays, one per neuron type
  138. # within each array, the dimensions corresponds to step:dist:neuron
  139. # this means that each array has as many matrices as neuron of a certain type
  140. dist <- lapply(axonFiles, function(x) x$Distancias.Entre.Planos)[[1]]
  141. step <- lapply(axonFiles, function(x) x$Step.Lengths)[[1]]
  142. errorMatrix <- lapply(axonFiles, function(x) x$MATRIZ.ERROR.LENGTH)
  143. lpMatrix <- lapply(axonFiles, function(x) x$MATRIZ.ESTIMATED.AXON.LENGTH)
  144. lrVals <- lapply(axonFiles, function(x) x$AXON.REAL.LENGTH)
  145. # Get number of neurons per type
  146. numberTypes <- unlist(lapply(errorMatrix, function(x) dim(x)[3]))
  147. # Vectorizing the arrays goes as follows: neuron, dist, step
  148. errorVector <- unlist(lapply(errorMatrix, function(x) as.vector(x)))
  149. lpVector <- unlist(lapply(lpMatrix, function(x) as.vector(x)))
  150. lrVector <- unlist(lapply(lrVals, function(x) as.vector(x)))
  151. # Store in data frames
  152. allData <- data.frame(id = rep(1:sum(numberTypes), each = 90),
  153. neurType = rep(c("Type1", "Type2", "Type3", "Type4"), times = 90*numberTypes),
  154. dist = rep(rep(dist, each = 9), sum(numberTypes)),
  155. step = rep(rep(step, 10), sum(numberTypes)),
  156. error = abs(errorVector),
  157. error2 = errorVector,
  158. LRe = lpVector,
  159. LR = rep(lrVector, each = 90))
  160. allData$errorRaw <- allData$LR - allData$LRe
  161. allData$interact <- interaction(allData$step, allData$dist, lex.order = T)
  162. allData$interact2 <- interaction(allData$neurType, allData$step, allData$dist, lex.order = T)
  163. ```
  164. ###Mean errors
  165. ####Estimate, format and save
  166. ```{r}
  167. # Estimate mean residual error
  168. meanFrame <- aggregate(error ~ dist + step + neurType, allData, mean)
  169. n <- 200
  170. grid <- expand.grid(dist = seq(3, 30, length = n), step = seq(70, 150, length = n))
  171. # Smooth lm
  172. for (i in unique(meanFrame$neurType)) {
  173. axlm <- lm(error ~ dist * step, data = meanFrame[meanFrame$neurType == i, ])
  174. print(summary(axlm))
  175. axfit <- cbind(grid, prediction = predict(axlm, grid))
  176. # Save as csv
  177. write.csv(axfit, paste("dataPepo/stereoPlanes_meanError_smooth_", i, ".csv", sep=""))
  178. }
  179. ```
  180. ###Error density
  181. ####Estimation
  182. ```{r}
  183. # Find mean bandwidth
  184. bwFrame <- aggregate(error2 ~ neurType + dist + step, allData, function(x) density(x)$bw)
  185. # Estimate density using mean bw per axonType
  186. densList <- list()
  187. for (i in c("Type1", "Type2", "Type3", "Type4")) {
  188. axData <- allData[allData$neurType == i, ]
  189. densList[[i]] <- by(axData$error2, axData$interact, function(x) density(x,
  190. bw = ceiling(mean(bwFrame[bwFrame$neurType == i, "error2"]))))
  191. }
  192. # Restore format
  193. densList <- unlist(densList, recursive = FALSE)
  194. ```
  195. ####Format and save
  196. ```{r}
  197. # Extract from list
  198. densXY <- lapply(densList, function(x) cbind(error = x$x, density = x$y))
  199. densFrame <- data.frame(axonType = rep(c("Type1", "Type2", "Type3", "Type4"), each = 512*90),
  200. step = rep(rep(unique(allData$step), each = 512*10), 4),
  201. step = rep(rep(unique(allData$dist), each = 512), 4*9))
  202. # Store in long format data frame
  203. densFrame <- cbind(densFrame, do.call(rbind, densXY))
  204. # Save density as csv
  205. write.csv(densFrame, "dataPepo/stereoPlanes_error_Densities.csv", sep=",", row.names = FALSE)
  206. # Save original data as csv
  207. oriFrame <- cbind(axonType = allData[, "neurType"], dist = allData[, "dist"], step = allData[, "step"],
  208. error = allData[, "error2"], abserror = allData[, "error"])
  209. write.csv(oriFrame, "dataPepo/stereoPlanes_error_OriginalData.csv", sep=",", row.names = FALSE)
  210. ```
  211. ###Cumul probability
  212. ####Estimate, format and save
  213. ```{r}
  214. # Reformat as dataframe
  215. binProb <- 0.5
  216. probSeq <- seq(binProb, 100, binProb)
  217. axProb <- readRDS("EstereoAnalysis/errorProbs_RMSE.rds")
  218. probFrame <- data.frame(error = rep(probSeq, 90*4),
  219. neurType = rep(c("Type1", "Type2", "Type3", "Type4"), each = length(probSeq)*90),
  220. dist = rep(rep(unique(allData$dist), each = length(probSeq)*9), 4),
  221. step = rep(rep(unique(allData$step), each = length(probSeq)), 10*4),
  222. prob = unlist(axProb))
  223. n <- 200
  224. grid <- expand.grid(dist = seq(3, 30, length = n), step = seq(70, 150, length = n))
  225. # Smooth lm
  226. for (h in c(5, 10)) {
  227. for (i in unique(probFrame$neurType)) {
  228. axlm <- lm(prob ~ dist * step, data = probFrame[probFrame$error == h & probFrame$neurType == i, ])
  229. print(summary(axlm))
  230. axfit <- cbind(grid, prediction = predict(axlm, grid))
  231. # Save as csv
  232. write.csv(axfit, paste("dataPepo/stereoPlanes_cumulProb_error_", h, "_smooth_", i, ".csv", sep=""))
  233. }
  234. }
  235. ```
  236. ##STEREO SPHERES
  237. ###Load data
  238. ```{r}
  239. # Get file paths
  240. axonNames <- list.files(paste("EsferasData/", sep=""), full.names=T)
  241. # Load matlab file
  242. axonFiles <- lapply(axonNames, function(x) readMat(x))
  243. names(axonFiles) <- c("Type1", "Type2", "Type3_1", "Type3_2", "Type4")
  244. # Extract data
  245. # errorMatrix contains 4 arrays, one per neuron type
  246. # within each array, the dimensions corresponds to step:diams:neuron
  247. # this means that each array has as many matrices as neuron of a certain type
  248. diams <- lapply(axonFiles, function(x) x$Diams.Sonda)[[1]]
  249. step <- lapply(axonFiles, function(x) x$Step.Lengths)[[1]]
  250. errorMatrix <- lapply(axonFiles, function(x) x$MATRIZ.ERROR.LENGTH)
  251. lpMatrix <- lapply(axonFiles, function(x) x$ESTIMATED.AXON.LENGTH)
  252. lrVals <- lapply(axonFiles, function(x) x$AXON.REAL.LENGTH)
  253. # Get number of neurons per type
  254. numberTypes <- unlist(lapply(errorMatrix, function(x) dim(x)[3]))
  255. # Vectorizing the arrays goes as follows: neuron, diams, step
  256. errorVector <- unlist(lapply(errorMatrix, function(x) as.vector(x)))
  257. lpVector <- unlist(lapply(lpMatrix, function(x) as.vector(x)))
  258. lrVector <- unlist(lapply(lrVals, function(x) as.vector(x)))
  259. # Store in data frames
  260. allData <- data.frame(id = rep(1:sum(numberTypes), each = 81),
  261. diams = rep(rep(diams, each = 9), sum(numberTypes)),
  262. step = rep(rep(step, 9), sum(numberTypes)),
  263. error = abs(errorVector),
  264. error2 = errorVector,
  265. LRe = lpVector,
  266. LR = rep(lrVector, each = 81))
  267. # Remove all LR = 0 (duplicated Type3)
  268. allData <- allData[allData$LR != 0, ]
  269. # Get number of neurons per type
  270. numberTypes <- numberTypes[-3]
  271. names(numberTypes) <- c("Type1", "Type2", "Type3", "Type4")
  272. # Reassign id and neurType
  273. allData$id <- rep(1:sum(numberTypes), each = 81)
  274. allData$neurType <- rep(c("Type1", "Type2", "Type3", "Type4"), times = 81*numberTypes)
  275. allData$errorRaw <- allData$LR - allData$LRe
  276. allData$interact <- interaction(allData$step, allData$diams, lex.order = T)
  277. allData$interact2 <- interaction(allData$neurType, allData$step, allData$diams, lex.order = T)
  278. ```
  279. ###Mean errors
  280. ####Estimate, format and save
  281. ```{r}
  282. # Estimate mean residual error
  283. meanFrame <- aggregate(error ~ diams + step + neurType, allData, mean)
  284. n <- 200
  285. grid <- expand.grid(diams = seq(10, 50, length = n), step = seq(70, 150, length = n))
  286. # Smooth lm
  287. for (i in unique(meanFrame$neurType)) {
  288. axlm <- lm(error ~ diams * step, data = meanFrame[meanFrame$neurType == i, ])
  289. print(summary(axlm))
  290. axfit <- cbind(grid, prediction = predict(axlm, grid))
  291. # Save as csv
  292. write.csv(axfit, paste("dataPepo/stereoSpheres_meanError_smooth_", i, ".csv", sep=""))
  293. }
  294. ```
  295. ###Error density
  296. ####Estimation
  297. ```{r}
  298. # Find mean bandwidth
  299. bwFrame <- aggregate(error2 ~ neurType + diams + step, allData, function(x) density(x)$bw)
  300. # Estimate density using mean bw per axonType
  301. densList <- list()
  302. for (i in c("Type1", "Type2", "Type3", "Type4")) {
  303. axData <- allData[allData$neurType == i, ]
  304. densList[[i]] <- by(axData$error2, axData$interact, function(x) density(x,
  305. bw = ceiling(mean(bwFrame[bwFrame$neurType == i, "error2"]))))
  306. }
  307. # Restore format
  308. densList <- unlist(densList, recursive = FALSE)
  309. ```
  310. ####Format and save
  311. ```{r}
  312. # Extract from list
  313. densXY <- lapply(densList, function(x) cbind(error = x$x, density = x$y))
  314. densFrame <- data.frame(axonType = rep(c("Type1", "Type2", "Type3", "Type4"), each = 512*81),
  315. step = rep(rep(unique(allData$step), each = 512*9), 4),
  316. step = rep(rep(unique(allData$diams), each = 512), 4*9))
  317. # Store in long format data frame
  318. densFrame <- cbind(densFrame, do.call(rbind, densXY))
  319. # Save density as csv
  320. write.csv(densFrame, "dataPepo/stereoSpheres_error_Densities.csv", sep=",", row.names = FALSE)
  321. # Save original data as csv
  322. oriFrame <- cbind(axonType = allData[, "neurType"], diams = allData[, "diams"], step = allData[, "step"],
  323. error = allData[, "error2"], abserror = allData[, "error"])
  324. write.csv(oriFrame, "dataPepo/stereoSpheres_error_OriginalData.csv", sep=",", row.names = FALSE)
  325. ```
  326. ###Cumul probability
  327. ####Estimate, format and save
  328. ```{r}
  329. # Reformat as dataframe
  330. binProb <- 0.5
  331. probSeq <- seq(binProb, 100, binProb)
  332. axProb <- readRDS("EsferasAnalysis/errorProbs_RMSE.rds")
  333. probFrame <- data.frame(error = rep(probSeq, 81*4),
  334. neurType = rep(c("Type1", "Type2", "Type3", "Type4"), each = length(probSeq)*81),
  335. diams = rep(rep(unique(allData$diams), each = length(probSeq)*9), 4),
  336. step = rep(rep(unique(allData$step), each = length(probSeq)), 9*4),
  337. prob = unlist(axProb))
  338. n <- 200
  339. grid <- expand.grid(diams = seq(10, 50, length = n), step = seq(70, 150, length = n))
  340. # Smooth lm
  341. for (h in c(5, 10)) {
  342. for (i in unique(probFrame$neurType)) {
  343. axlm <- lm(prob ~ diams * step, data = probFrame[probFrame$error == h & probFrame$neurType == i, ])
  344. axfit <- cbind(grid, prediction = predict(axlm, grid))
  345. # Save as csv
  346. write.csv(axfit, paste("dataPepo/stereoSpheres_cumulProb_error_", h, "_smooth_", i, ".csv", sep=""))
  347. }
  348. }
  349. ```