1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693 |
- ---
- title: "Axonal Length"
- author: "Sergio D?ez Hermano"
- date: '`r format(Sys.Date(),"%e de %B, %Y")`'
- output:
- html_document:
- highlight: tango
- toc: yes
- toc_depth: 4
- toc_float:
- collapsed: no
- ---
- ```{r setup, include=FALSE}
- require(knitr)
- # include this code chunk as-is to set options
- 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)
- Sys.setlocale("LC_TIME", "C")
- ```
- ##Load libraries and functions
- ```{r}
- library(R.matlab)
- library(lattice)
- library(dplyr)
- library(ggplot2)
- library(ggpubr)
- library(ggridges)
- library(mltools)
- library(data.table)
- library(caret)
- library(interactions)
- library(data.table)
- options(digits = 10)
- # https://rstudio-pubs-static.s3.amazonaws.com/297778_5fce298898d64c81a4127cf811a9d486.html
- ##############################
- # Bootstrap PI per axon type # STEREOLOGY VERSION, LR vs LP
- ##############################
- the.replication.type2 <- function(reg, s, disti, stepj, trainset, x_Np1 = 0){
- # Make bootstrap residuals
- ep.star <- sample(s, size=length(reg$residuals),replace=TRUE)
- # Make bootstrap Y
- y.star <- fitted(reg) + ep.star
- # Do bootstrap regression
- lp <- model.frame(reg)[,2]
- bs.reg <- lm(y.star ~ lp - 1)
- # Create bootstrapped adjusted residuals
- bs.lev <- influence(bs.reg, do.coef = FALSE)$hat
- bs.s <- residuals(bs.reg)
-
- bs.levAdd <- bs.lev[which(trainset$dist == disti & trainset$step == stepj)]
- bs.sAdd <- bs.s[which(trainset$dist == disti & trainset$step == stepj)]
-
- bs.Stud <- bs.sAdd/sqrt(1-bs.levAdd)
- bs.add <- bs.Stud - mean(bs.Stud)
- # Calculate draw on prediction error
- # xb.xb <- coef(my.reg)["(Intercept)"] - coef(bs.reg)["(Intercept)"]
- xb.xb <- (coef(reg) - coef(bs.reg))*x_Np1
- return(unname(xb.xb + sample(bs.sAdd, size=1)))
-
- }
- ##############################
- # Bootstrap PI per axon type # STEREOLOGY VERSION, LR vs LP
- ##############################
- the.replication.type3 <- function(reg, s, x_Np1 = 0){
- # Make bootstrap residuals
- ep.star <- sample(s, size=length(reg$residuals),replace=TRUE)
- # Make bootstrap Y
- y.star <- fitted(reg) + ep.star
- # Do bootstrap regression
- lp <- model.frame(reg)[,2]
- bs.reg <- lm(y.star ~ lp - 1)
- # Estimation
- return(unname(coef(bs.reg)*x_Np1))
-
- }
- ##############################
- # Stratified random sampling #
- ##############################
- stratified <- function(df, group, size, select = NULL,
- replace = FALSE, bothSets = FALSE) {
- if (is.null(select)) {
- df <- df
- } else {
- if (is.null(names(select))) stop("'select' must be a named list")
- if (!all(names(select) %in% names(df)))
- stop("Please verify your 'select' argument")
- temp <- sapply(names(select),
- function(x) df[[x]] %in% select[[x]])
- df <- df[rowSums(temp) == length(select), ]
- }
- df.interaction <- interaction(df[group], drop = TRUE)
- df.table <- table(df.interaction)
- df.split <- split(df, df.interaction)
- if (length(size) > 1) {
- if (length(size) != length(df.split))
- stop("Number of groups is ", length(df.split),
- " but number of sizes supplied is ", length(size))
- if (is.null(names(size))) {
- n <- setNames(size, names(df.split))
- message(sQuote("size"), " vector entered as:\n\nsize = structure(c(",
- paste(n, collapse = ", "), "),\n.Names = c(",
- paste(shQuote(names(n)), collapse = ", "), ")) \n\n")
- } else {
- ifelse(all(names(size) %in% names(df.split)),
- n <- size[names(df.split)],
- stop("Named vector supplied with names ",
- paste(names(size), collapse = ", "),
- "\n but the names for the group levels are ",
- paste(names(df.split), collapse = ", ")))
- }
- } else if (size < 1) {
- n <- round(df.table * size, digits = 0)
- } else if (size >= 1) {
- if (all(df.table >= size) || isTRUE(replace)) {
- n <- setNames(rep(size, length.out = length(df.split)),
- names(df.split))
- } else {
- message(
- "Some groups\n---",
- paste(names(df.table[df.table < size]), collapse = ", "),
- "---\ncontain fewer observations",
- " than desired number of samples.\n",
- "All observations have been returned from those groups.")
- n <- c(sapply(df.table[df.table >= size], function(x) x = size),
- df.table[df.table < size])
- }
- }
- temp <- lapply(
- names(df.split),
- function(x) df.split[[x]][sample(df.table[x],
- n[x], replace = replace), ])
- set1 <- do.call("rbind", temp)
-
- if (isTRUE(bothSets)) {
- set2 <- df[!rownames(df) %in% rownames(set1), ]
- list(SET1 = set1, SET2 = set2)
- } else {
- set1
- }
- }
- ```
- ##Load data
- ```{r}
- # Get file paths
- axonNames <- list.files(paste("EstereoDataPlanos/", sep=""), full.names=T)
- # Load matlab file
- axonFiles <- lapply(axonNames, function(x) readMat(x))
- names(axonFiles) <- c("Type1", "Type2", "Type3", "Type4")
- # Extract data
- # errorMatrix contains 4 arrays, one per neuron type
- # within each array, the dimensions corresponds to step:dist:neuron
- # this means that each array has as many matrices as neuron of a certain type
- dist <- lapply(axonFiles, function(x) x$Distancias.Entre.Planos)[[1]]
- step <- lapply(axonFiles, function(x) x$Step.Lengths)[[1]]
- errorMatrix <- lapply(axonFiles, function(x) x$MATRIZ.ERROR.LENGTH)
- lpMatrix <- lapply(axonFiles, function(x) x$MATRIZ.ESTIMATED.AXON.LENGTH)
- lrVals <- lapply(axonFiles, function(x) x$AXON.REAL.LENGTH)
- # Get number of neurons per type
- numberTypes <- unlist(lapply(errorMatrix, function(x) dim(x)[3]))
- # Vectorizing the arrays goes as follows: neuron, dist, step
- errorVector <- unlist(lapply(errorMatrix, function(x) as.vector(x)))
- lpVector <- unlist(lapply(lpMatrix, function(x) as.vector(x)))
- lrVector <- unlist(lapply(lrVals, function(x) as.vector(x)))
- # Store in data frames
- allData <- data.frame(id = rep(1:sum(numberTypes), each = 90),
- neurType = rep(c("Type1", "Type2", "Type3", "Type4"), times = 90*numberTypes),
- dist = rep(rep(dist, each = 9), sum(numberTypes)),
- step = rep(rep(step, 10), sum(numberTypes)),
- error = abs(errorVector),
- error2 = errorVector,
- LP = lpVector,
- LR = rep(lrVector, each = 90))
- allData$errorRaw <- allData$LR - allData$LP
- ```
- ##Bootstrap PI - Prediction CV - LR vs LP
- This code is meant to be run independently for every neurType and in parallel
- ```{r}
- # Ensure reproducibility
- set.seed(123456)
- # Subset axon type
- dataCV <- allData[allData$neurType == "Type1", ]
- # List to store CV repetitions
- cvList <- list ()
- cvRand <- list()
- y.cv <- list()
- for (h in 1:10) {
- # Stratified random training/test sets
- stratDat <- stratified(dataCV, c("dist", "step"), 0.7, bothSets = TRUE, replace = FALSE)
- trainSet <- stratDat$SET1
- testSet <- stratDat$SET2
-
- # Store train and test set
- cvRand[[h]] <- list(trainSet = trainSet, testSet = testSet)
-
- # OLS with training set
- my.reg <- lm(LR ~ LP - 1, trainSet)
- # Create adjusted residuals
- leverage <- influence(my.reg)$hat
- my.s.resid <- residuals(my.reg)/sqrt(1-leverage)
- my.s.resid <- my.s.resid - mean(my.s.resid)
- my.s.resid <- residuals(my.reg)
-
- # List to store errors
- epList <- list()
- # Vector to store predicted LR
- y.p <- numeric()
-
-
- # Bootstrapping on test set
- for (i in unique(allData$dist)) {
-
- for (j in unique(allData$step)) {
-
- # Subset data
- dataSub <- testSet[testSet$dist == i & testSet$step == j, ]
-
- kList <- list()
- kCount <- 1
-
- # Predict on unseen LP
- for (k in dataSub[ , "LP"]) {
- # Do bootstrap with 100 replications
- kList[[kCount]] <- replicate(n=100, the.replication.type3(reg = my.reg, s = my.s.resid, x_Np1 = k))
- y.p <- c(y.p, kList[[kCount]])
- kCount <- kCount + 1
-
- }
-
- # Store in list
- epList[[paste(as.character(i), as.character(j), sep = ".")]] <- kList
- print(paste("Step", j, "finished"))
-
- }
-
- print(paste("Dist", i, "finished"))
-
- }
-
- cvList[[h]] <- epList
- y.cv[[h]] <- y.p
- print(paste("CV", h, "finished"))
-
- }
- saveRDS(list(y.cv, cvList, cvRand), paste("stereo_boot10CVPI_LRLP_pred_Type1.rds", sep = ""))
- ```
- ##Bootstrap PI - CV - LR vs LP
- This code is meant to be run independently for every neurType and in parallel
- ```{r}
- # Ensure reproducibility
- set.seed(123456)
- # Subset axon type
- dataCV <- allData[allData$neurType == "Type1", ]
- # List to store CV repetitions
- cvList <- list ()
- cvRand <- list()
- y.cv <- list()
- for (h in 1) {
- # Stratified random training/test sets
- stratDat <- stratified(dataCV, c("dist", "step"), 0.7, bothSets = TRUE, replace = FALSE)
- trainSet <- stratDat$SET1
- testSet <- stratDat$SET2
-
- # Store train and test set
- cvRand[[h]] <- list(trainSet = trainSet, testSet = testSet)
-
- # OLS with training set
- my.reg <- lm(LR ~ LP - 1, trainSet)
- # Create adjusted residuals
- leverage <- influence(my.reg)$hat
- my.s.resid <- residuals(my.reg)/sqrt(1-leverage)
- my.s.resid <- my.s.resid - mean(my.s.resid)
- my.s.resid <- residuals(my.reg)
-
- # List to store errors
- epList <- list()
- # Vector to store predicted LR
- y.p <- numeric()
-
-
- # Bootstrapping on test set
- for (i in unique(allData$dist)) {
-
- for (j in unique(allData$step)) {
-
- # Subset data
- dataSub <- testSet[testSet$dist == i & testSet$step == j, ]
-
- kList <- list()
- kCount <- 1
-
- # Predict on unseen LP
- for (k in dataSub[ , "LP"]) {
-
- # Store predicted error
- y.p <- c(y.p, coef(my.reg)*k)
-
- # Create adjusted residuals
- leverage <- influence(my.reg, do.coef = FALSE)$hat
- my.s.resid <- residuals(my.reg)/sqrt(1-leverage)
- my.s.resid <- my.s.resid - mean(my.s.resid)
-
- # Do bootstrap with 100 replications
- kList[[kCount]] <- replicate(n=100, the.replication.type2(reg = my.reg, s = my.s.resid, disti = i, stepj = j, trainset = trainSet,
- x_Np1 = k))
- kCount <- kCount + 1
-
- }
-
- # Store in list
- epList[[paste(as.character(i), as.character(j), sep = ".")]] <- kList
- print(paste("Step", j, "finished"))
-
- }
-
- print(paste("Dist", i, "finished"))
-
- }
-
- cvList[[h]] <- epList
- names(y.p) <- paste(rep(unique(allData$dist), each = 9), rep(unique(allData$step), 10), sep=".")
- y.cv[[h]] <- y.p
- print(paste("CV", h, "finished"))
-
- }
- saveRDS(list(y.cv, cvList, cvRand), paste("stereo_boot10CVPI_LRLP_notStud_Type1.rds", sep = ""))
- ```
- ##Plotting for bsAdd
- ```{r}
- bsAdd <- readRDS("stereo_boot10CVPI_LRLP_bsAdd_Type1.rds")
- y.cv <- bsAdd[[1]]
- cvList <- bsAdd[[2]]
- cvRand <- bsAdd[[3]]
- estimList <- list()
- for (h in 1) {
- lrSet <- cvRand[[h]][["testSet"]]
- lrOrder <- lrSet[with(lrSet, order(dist, step)), ]
-
- errorSet <- unlist(cvList[[h]])
-
- errorFrame <- data.frame(dist = rep(lrOrder$dist, each = 100),
- step = rep(lrOrder$step, each = 100),
- LR = rep(lrOrder$LR, each = 100),
- error = errorSet)
-
- estimList[[h]] <- errorFrame
- }
- estimTotal <- do.call(rbind, estimList)
- estimTotal$pe <- (estimTotal$error/estimTotal$LR)*100
- estimTotal$abspe <- abs(estimTotal$pe)
- estimTotal$interact <- interaction(estimTotal$dist, estimTotal$step, lex.order = TRUE)
- estimMean <- aggregate(abspe ~ dist + step, estimTotal, mean)
- ```
- ###Mean
- ```{r, fig.height=10, fig.width=20}
- meanFrame <- estimMean
- meanFrame$neurType <- rep("Type1", dim(meanFrame)[1])
- # Library
- library(latticeExtra)
- # Contour color palette
- colfunc <- colorRampPalette(c("navy","royalblue","springgreen","gold","yellow"))
- colfin <- colfunc(1000)
- library(viridisLite)
- coul <- viridis(1000)
- # Store heatmaps
- heatList <- list()
- smoothList <- list()
- for (i in c("Type1")) {
-
- heatList[[i]] <- levelplot(abspe ~ dist * step, data = meanFrame[meanFrame$neurType == i, ], col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(155, 65),
- colorkey = list(at = (seq(min(meanFrame[meanFrame$neurType == i, "abspe"]),
- max(meanFrame[meanFrame$neurType == i, "abspe"]),
- 0.05))),
- main = paste("Mean Abs Error,", i))
-
- smoothList[[i]] <- levelplot(abspe ~ dist * step, data = meanFrame[meanFrame$neurType == i, ], col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(150,70),
- xlim = c(3,30),
- cuts = 20,
- colorkey = list(at = (seq(min(meanFrame[meanFrame$neurType == i, "abspe"]),
- max(meanFrame[meanFrame$neurType == i, "abspe"]),
- 0.05))),
- main = paste("Mean Abs Error,", i),
- panel = panel.levelplot.points, cex = 0) +
- layer_(panel.2dsmoother(..., n = 200))
-
- }
- # Plot
- # setwd("EstereoAnalysis/")
- #
- # png(filename="meanProbabilities_heatmap_CV.png", type="cairo",
- # units="in", width=20, height=10, pointsize=12,
- # res=300)
- gridExtra::grid.arrange(grobs = c(heatList, smoothList), ncol = 4, nrow = 2)
- # dev.off()
- ```
- ###Density
- ```{r, fig.width=5, fig.height=20}
- ggplot(estimTotal, aes(x = `pe`, y = `interact`, fill = ifelse(..x.. >=-5 & ..x.. <=5, ">= -5", "< 5"))) +
- scale_fill_manual(values = c("gray70", "coral2"), name = NULL) +
- geom_density_ridges_gradient(alpha = 0.8) +
- theme_ridges() +
- theme(legend.position = "none") +
- xlab("% Error") +
- xlim(c(-40,40))
-
- ```
- ###Errors vs cumul probability
- ```{r, fig.height=5, fig.width=20}
- # Inverse quantile
- quantInv <- function(distr, value) ecdf(distr)(value)
- # Function to apply to all axon types
- quantType <- function(peFrame, probSeq) {
-
- probList <- list()
-
- for (i in unique(peFrame$interact)) {
-
- dataProb <- peFrame[peFrame$interact == i, "pe"]
- probVec <- numeric()
-
- for (j in probSeq) {
-
- errProb <- quantInv(dataProb, j) - quantInv(dataProb, -j)
- probVec <- c(probVec, errProb)
-
- }
-
- probList[[i]] <- probVec
-
- }
-
- return(probList)
-
- }
- # Define errors for which to calculate probability
- binProb <- 0.5
- probSeq <- seq(binProb, 100, binProb)
- # Store axon types in lists
- # axProb <- lapply(frameList, function(x) quantType(x, probSeq))
- axProb <- quantType(estimTotal, probSeq)
- # Save
- # saveRDS(axProb, "errorProbs_CVleaveout.rds")
- ```
- ######Plot heatmap
- ```{r, width=15, height=10}
- library(latticeExtra)
- # Reformat as dataframe
- binProb <- 0.5
- probSeq <- seq(binProb, 100, binProb)
- # axProb <- readRDS("errorProbs_CVleaveout.rds")
- # probFrame <- data.frame(error = rep(probSeq, 90*4),
- # neurType = rep(c("Type1", "Type2", "Type3", "Type4"), each = length(probSeq)*90),
- # dist = rep(rep(unique(allData$dist), each = length(probSeq)*9), 4),
- # step = rep(rep(unique(allData$step), each = length(probSeq)), 10*4),
- # prob = unlist(axProb))
- probFrame <- data.frame(error = rep(probSeq, 90),
- neurType = rep("Type1", length(probSeq)*90),
- dist = rep(unique(allData$dist), each = length(probSeq)*9),
- step = rep(rep(unique(allData$step), each = length(probSeq)), 10),
- prob = unlist(axProb))
- # Plot conttour plot for 5% error
- library(viridisLite)
- coul <- viridis(1000)
- # Store heatmaps
- heatList <- list()
- smoothList <- list()
- for (i in c("Type1")) {
-
- levelList1 <- list()
- levelList2 <- list()
-
- for (j in c(5, 10, 20)) {
-
- dataPlot <- probFrame[probFrame$neurType == i & probFrame$error == j, ]
-
- levelList1[[as.character(j)]] <- levelplot(prob ~ dist * step, data = dataPlot,
- col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(155,65),
- colorkey = list(at = (seq(min(dataPlot$prob),
- max(dataPlot$prob),
- 0.001))),
- main = paste("P(%Error <=", j, ")", sep = ""))
-
- levelList2[[as.character(j)]] <- levelplot(prob ~ dist * step, data = dataPlot,
- col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(150,70),
- xlim = c(3,30),
- cuts = 20,
- colorkey = list(at = (seq(min(dataPlot$prob),
- max(dataPlot$prob),
- 0.001))),
- main = paste("P(%Error <=", j, ")", sep = ""),
- panel = panel.levelplot.points, cex = 0) +
- layer_(panel.2dsmoother(..., n = 200))
- }
-
- heatList[[i]] <- levelList1
- smoothList[[i]] <- levelList2
- }
- # Plot
- # setwd("EstereoAnalysis/")
- for (i in c("Type1")) {
- png(filename=paste("prueba_", i, "_CVleaveout.png", sep=""), type="cairo",
- units="in", width=15, height=10, pointsize=12,
- res=300)
- gridExtra::grid.arrange(grobs = c(heatList[[i]], smoothList[[i]]), ncol = 3, nrow = 2)
- dev.off()
- }
- ```
- ###PI 1
- ```{r}
- jj <- allData[allData$neurType == "Type1", ]
- boxplot(LP ~ dist + step, jj)
- boxplot(LP ~ dist + step, cvRand[[1]]$trainSet)
- boxplot(LP ~ dist + step, cvRand[[1]]$testSet)
- boxplot(LR ~ dist + step, jj)
- boxplot(LR ~ dist + step, cvRand[[1]]$trainSet)
- boxplot(LR ~ dist + step, cvRand[[1]]$testSet)
- ```
- ```{r, fig.width=45, fig.height=50}
- piLimits <- list()
- cv1 <- cvList[[1]]
- for (i in 1:90) {
- piLimits[[i]] <- lapply(cv1[[i]], function(x) quantile(x, c(0.025, 0.975)))
- }
- names(piLimits) <- names(cv1)
- limVec <- unlist(unlist(piLimits))
- oddPos <- seq(1, length(limVec), 2)
- lowLims <- limVec[oddPos]
- upLims <- limVec[-oddPos]
- yp <- unlist(y.cv)
- testDat <- cvRand[[1]]$testSet
- testOrd <- testDat[order(testDat$dist, testDat$step), ]
- testOrd$interact <- interaction(testOrd$dist, testOrd$step, lex.order = T)
- lp <-testOrd$LP
- names(lp) <- testOrd$interact
- lr <- testOrd$LR
- names(lr) <- testOrd$interact
-
- # ySort <- sort(yp)
- # lpSort <- lp[order(yp)]
- # lrSort <- lr[order(yp)]
- # lowSort <- lowLims[order(yp)]
- # upSort <- upLims[order(yp)]
- lpSort <- sort(lp)
- ySort <- yp[order(lp)]
- lrSort <- lr[order(lp)]
- lowSort <- lowLims[order(lp)]
- upSort <- upLims[order(lp)]
- # plot(lpSort, ySort, type = "l")
- # polygon(c(lpSort, rev(lpSort)), c(ySort + lowSort, rev(ySort + upSort)), col = gray(0.5), border = NULL, lwd = 0.01)
- distep <- as.numeric(unlist(strsplit(jj, "[.]")))
- framePol <- data.frame(dist = distep[seq(1, length(distep), 2)],
- step = distep[-seq(1, length(distep), 2)],
- yp = ySort,
- lp = lpSort,
- lr = lrSort,
- lowLim = ySort + lowSort,
- upLim = ySort + upSort)
- par(mfcol=c(9,10))
- for (i in sort(unique(framePol$dist))) {
-
- for (j in sort(unique(framePol$step))) {
-
- ySort <- framePol[framePol$dist == i & framePol$step == j, "yp"]
- lpSort <- framePol[framePol$dist == i & framePol$step == j, "lp"]
- lrSort <- framePol[framePol$dist == i & framePol$step == j, "lr"]
- lowSort <- framePol[framePol$dist == i & framePol$step == j, "lowLim"]
- upSort <- framePol[framePol$dist == i & framePol$step == j, "upLim"]
-
- # ySort <- frollmean(framePol[framePol$dist == i & framePol$step == j, "yp"], 5)
- # lpSort <- frollmean(framePol[framePol$dist == i & framePol$step == j, "lp"], 5)
- # lrSort <- frollmean(framePol[framePol$dist == i & framePol$step == j, "lr"], 5)
- # lowSort <- frollmean(framePol[framePol$dist == i & framePol$step == j, "lowLim"], 5)
- # upSort <- frollmean(framePol[framePol$dist == i & framePol$step == j, "upLim"], 5)
-
- # lowesLP <- lowess(lpSort, ySort)
- # lowesLR <- lowess(lrSort, ySort)
- lowesPIlow <- lowess(lpSort, lowSort)
- lowesPIup <- lowess(lpSort, upSort)
-
- plot(lpSort, ySort, ylim = c(0,350000), pch = "",
- main = paste(i, j, sep="."), ylab = "LR", xlab = "LP", cex.main = 2.5, cex.axis = 2.5)
- polygon(c(lowesPIlow[[1]], rev(lowesPIup[[1]])), c(lowesPIlow[[2]], rev(lowesPIup[[2]])), col = gray(0.6), border = NULL, lwd = 0.01)
- # polygon(c(lpSort, rev(lpSort)), c(lowSort, rev(upSort)), col = gray(0.6), border = NULL, lwd = 0.01)
- lines(lpSort, ySort, type = "l")
- lines(lpSort, lrSort, col = "red")
-
- }
-
- }
- ```
- ###PI 2
- ```{r, fig.width=15, fig.height=15}
- piLimits <- list()
- cv1 <- cvList[[1]]
- for (i in 1:90) {
- piLimits[[i]] <- lapply(cv1[[i]], function(x) quantile(x, c(0.025, 0.975)))
- }
- names(piLimits) <- names(cv1)
- limVec <- unlist(unlist(piLimits))
- oddPos <- seq(1, length(limVec), 2)
- lowLims <- limVec[oddPos]
- upLims <- limVec[-oddPos]
- yp <- unlist(y.cv)
- testDat <- cvRand[[1]]$testSet
- testOrd <- testDat[order(testDat$dist, testDat$step), ]
- lp <-testOrd$LP
- lr <- testOrd$LR
-
- ySort <- sort(yp)
- lpSort <- lp[order(yp)]
- lrSort <- lr[order(yp)]
- lowSort <- lowLims[order(yp)]
- upSort <- upLims[order(yp)]
- # plot(lpSort, ySort, type = "l")
- # polygon(c(lpSort, rev(lpSort)), c(ySort + lowSort, rev(ySort + upSort)), col = gray(0.5), border = NULL, lwd = 0.01)
- distep <- as.numeric(unlist(strsplit(jj, "[.]")))
- framePol <- data.frame(dist = distep[seq(1, length(distep), 2)],
- step = distep[-seq(1, length(distep), 2)],
- yp = ySort,
- lp = lpSort,
- lr = lrSort,
- lowLim = ySort + lowSort,
- upLim = ySort + upSort)
- coul <- viridisLite::viridis(10)
- par(mfrow=c(3,3))
- for (j in sort(unique(framePol$step))) {
-
- colCount <- 1
- xLim <- c(min(framePol[framePol$step == j, "lp"]), max(framePol[framePol$step == j, "lp"]))
-
- for (i in sort(unique(framePol$dist))) {
-
- ySort <- framePol[framePol$dist == i & framePol$step == j, "yp"]
- lpSort <- framePol[framePol$dist == i & framePol$step == j, "lp"]
- lrSort <- framePol[framePol$dist == i & framePol$step == j, "lr"]
- lowSort <- framePol[framePol$dist == i & framePol$step == j, "lowLim"]
- upSort <- framePol[framePol$dist == i & framePol$step == j, "upLim"]
-
-
- lowesPIlow <- lowess(lpSort, lowSort)
- lowesPIup <- lowess(lpSort, upSort)
-
- if (colCount == 1) {
-
- plot(lpSort, ySort, ylim = c(0,350000), xlim = xLim, pch = "",
- main = j, ylab = "LR", xlab = "LP", cex.main = 2.5, cex.axis = 1.5, cex.lab = 1.5)
-
- polygon(c(lowesPIlow[[1]], rev(lowesPIup[[1]])), c(lowesPIlow[[2]], rev(lowesPIup[[2]])), col = alpha(coul[colCount], 0.5), border = NULL)
- lines(lpSort, ySort, type = "l")
- lines(lpSort, lrSort, col = "red")
-
-
- } else {
-
- polygon(c(lowesPIlow[[1]], rev(lowesPIup[[1]])), c(lowesPIlow[[2]], rev(lowesPIup[[2]])), col = alpha(coul[colCount], 0.5), border = NULL)
- lines(lpSort, ySort, type = "l")
- lines(lpSort, lrSort, col = "red")
-
-
- }
-
- abline(h = 23697.93, lty = "dashed", col ="gray")
- colCount <- colCount + 1
-
- }
-
- }
- ```
- ##Plotting for prueba
- ```{r}
- estimList <- list()
- for (h in 1:10) {
- lrSet <- cvRand[[h]][["testSet"]]
- lrOrder <- lrSet[with(lrSet, order(dist, step)), ]
- ySet <- y.cv[[h]]
-
- estimFrame <- data.frame(dist = lrOrder$dist, step = lrOrder$step, LR = lrOrder$LR, pred = ySet)
-
- estimList[[h]] <- estimFrame
- }
- estimTotal <- do.call(rbind, estimList)
- estimTotal$pe <- ((estimTotal$LR - estimTotal$pred)/estimTotal$LR)*100
- estimTotal$abspe <- abs(estimTotal$pe)
- estimTotal$interact <- interaction(estimFrame$dist, estimFrame$step, lex.order = TRUE)
- estimMean <- aggregate(abspe ~ dist + step, estimTotal, mean)
- ```
- ###Mean
- ```{r, fig.height=10, fig.width=20}
- meanFrame <- estimMean
- meanFrame$neurType <- rep("Type1", dim(meanFrame)[1])
- # Library
- library(latticeExtra)
- # Contour color palette
- colfunc <- colorRampPalette(c("navy","royalblue","springgreen","gold","yellow"))
- colfin <- colfunc(1000)
- library(viridisLite)
- coul <- viridis(1000)
- # Store heatmaps
- heatList <- list()
- smoothList <- list()
- for (i in c("Type1")) {
-
- heatList[[i]] <- levelplot(abspe ~ dist * step, data = meanFrame[meanFrame$neurType == i, ], col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(155, 65),
- colorkey = list(at = (seq(min(meanFrame[meanFrame$neurType == i, "abspe"]),
- max(meanFrame[meanFrame$neurType == i, "abspe"]),
- 0.05))),
- main = paste("Mean Abs Error,", i))
-
- smoothList[[i]] <- levelplot(abspe ~ dist * step, data = meanFrame[meanFrame$neurType == i, ], col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(150,70),
- xlim = c(3,30),
- cuts = 20,
- colorkey = list(at = (seq(min(meanFrame[meanFrame$neurType == i, "abspe"]),
- max(meanFrame[meanFrame$neurType == i, "abspe"]),
- 0.05))),
- main = paste("Mean Abs Error,", i),
- panel = panel.levelplot.points, cex = 0) +
- layer_(panel.2dsmoother(..., n = 200))
-
- }
- # Plot
- # setwd("EstereoAnalysis/")
- #
- # png(filename="meanProbabilities_heatmap_CV.png", type="cairo",
- # units="in", width=20, height=10, pointsize=12,
- # res=300)
- gridExtra::grid.arrange(grobs = c(heatList, smoothList), ncol = 4, nrow = 2)
- # dev.off()
- ```
- ###Density
- ```{r, fig.width=5, fig.height=20}
- ggplot(estimTotal, aes(x = `pe`, y = `interact`, fill = ifelse(..x.. >=-5 & ..x.. <=5, ">= -5", "< 5"))) +
- scale_fill_manual(values = c("gray70", "coral2"), name = NULL) +
- geom_density_ridges_gradient(alpha = 0.8) +
- theme_ridges() +
- theme(legend.position = "none") +
- xlab("% Error") +
- xlim(c(-40,40))
-
- ```
- ###Errors vs cumul probability
- ```{r, fig.height=5, fig.width=20}
- # Inverse quantile
- quantInv <- function(distr, value) ecdf(distr)(value)
- # Function to apply to all axon types
- quantType <- function(peFrame, probSeq) {
-
- probList <- list()
-
- for (i in unique(peFrame$interact)) {
-
- dataProb <- peFrame[peFrame$interact == i, "pe"]
- probVec <- numeric()
-
- for (j in probSeq) {
-
- errProb <- quantInv(dataProb, j) - quantInv(dataProb, -j)
- probVec <- c(probVec, errProb)
-
- }
-
- probList[[i]] <- probVec
-
- }
-
- return(probList)
-
- }
- # Define errors for which to calculate probability
- binProb <- 0.5
- probSeq <- seq(binProb, 100, binProb)
- # Store axon types in lists
- # axProb <- lapply(frameList, function(x) quantType(x, probSeq))
- axProb <- quantType(estimTotal, probSeq)
- # Save
- # saveRDS(axProb, "errorProbs_CVleaveout.rds")
- ```
- ######Plot heatmap
- ```{r, width=15, height=10}
- library(latticeExtra)
- # Reformat as dataframe
- binProb <- 0.5
- probSeq <- seq(binProb, 100, binProb)
- # axProb <- readRDS("errorProbs_CVleaveout.rds")
- # probFrame <- data.frame(error = rep(probSeq, 90*4),
- # neurType = rep(c("Type1", "Type2", "Type3", "Type4"), each = length(probSeq)*90),
- # dist = rep(rep(unique(allData$dist), each = length(probSeq)*9), 4),
- # step = rep(rep(unique(allData$step), each = length(probSeq)), 10*4),
- # prob = unlist(axProb))
- probFrame <- data.frame(error = rep(probSeq, 90),
- neurType = rep("Type1", length(probSeq)*90),
- dist = rep(unique(allData$dist), each = length(probSeq)*9),
- step = rep(rep(unique(allData$step), each = length(probSeq)), 10),
- prob = unlist(axProb))
- # Plot conttour plot for 5% error
- library(viridisLite)
- coul <- viridis(1000)
- # Store heatmaps
- heatList <- list()
- smoothList <- list()
- for (i in c("Type1")) {
-
- levelList1 <- list()
- levelList2 <- list()
-
- for (j in c(5, 10, 20)) {
-
- dataPlot <- probFrame[probFrame$neurType == i & probFrame$error == j, ]
-
- levelList1[[as.character(j)]] <- levelplot(prob ~ dist * step, data = dataPlot,
- col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(155,65),
- colorkey = list(at = (seq(min(dataPlot$prob),
- max(dataPlot$prob),
- 0.001))),
- main = paste("P(%Error <=", j, ")", sep = ""))
-
- levelList2[[as.character(j)]] <- levelplot(prob ~ dist * step, data = dataPlot,
- col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(150,70),
- xlim = c(3,30),
- cuts = 20,
- colorkey = list(at = (seq(min(dataPlot$prob),
- max(dataPlot$prob),
- 0.001))),
- main = paste("P(%Error <=", j, ")", sep = ""),
- panel = panel.levelplot.points, cex = 0) +
- layer_(panel.2dsmoother(..., n = 200))
- }
-
- heatList[[i]] <- levelList1
- smoothList[[i]] <- levelList2
- }
- # Plot
- # setwd("EstereoAnalysis/")
- for (i in c("Type1")) {
- png(filename=paste("prueba_", i, "_CVleaveout.png", sep=""), type="cairo",
- units="in", width=15, height=10, pointsize=12,
- res=300)
- gridExtra::grid.arrange(grobs = c(heatList[[i]], smoothList[[i]]), ncol = 3, nrow = 2)
- dev.off()
- }
- ```
- ##Plotting for preds
- ```{r}
- # predsData <- readRDS("D:/Sergio/Cremoto Drive/stereo_boot10CVPI_LRLP_predReal100_Type1.rds")
- # jjMse <- jj %>%
- # group_by(dist, step) %>%
- # summarise(val=mltools::mse(pred, LR))
- testSets <- lapply(predsData, function(x) do.call(rbind, x$testSets))
- testJoin <- do.call(rbind, testSets)
- testJoin$pe <- ((testJoin$LR - testJoin$pred)/testJoin$LR)*100
- testJoin$abspe <- abs(testJoin$pe)
- testJoin$interact <- interaction(testJoin$dist, testJoin$step, lex.order = TRUE)
- testMean <- aggregate(abspe ~ dist + step, testJoin, mean)
- ```
- ```{r}
- # predsData <- readRDS("D:/Sergio/Cremoto Drive/stereo_boot10CVPI_LRLP_predReal100_Type1.rds")
- # jjMse <- jj %>%
- # group_by(dist, step) %>%
- # summarise(val=mltools::mse(pred, LR))
- testSets <- lapply(predsData, function(x) do.call(rbind, x$testSets))
- testPE <- lapply(testSets, function(x) x$pe <- ((x$LR - x$pred)/x$LR)*100)
- testJoin <- do.call(rbind, testSets)
- testJoin$pe <- ((testJoin$LR - testJoin$pred)/testJoin$LR)*100
- testJoin$abspe <- abs(testJoin$pe)
- testJoin$interact <- interaction(testJoin$dist, testJoin$step, lex.order = TRUE)
- testMean <- aggregate(abspe ~ dist + step, testJoin, mean)
- ```
- ###Mean
- ```{r, fig.height=10, fig.width=20}
- meanFrame <- testMean
- meanFrame$neurType <- rep("Type1", dim(meanFrame)[1])
- # Library
- library(latticeExtra)
- # Contour color palette
- colfunc <- colorRampPalette(c("navy","royalblue","springgreen","gold","yellow"))
- colfin <- colfunc(1000)
- library(viridisLite)
- coul <- viridis(1000)
- # Store heatmaps
- heatList <- list()
- smoothList <- list()
- for (i in c("Type1")) {
-
- heatList[[i]] <- levelplot(abspe ~ dist * step, data = meanFrame[meanFrame$neurType == i, ], col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(155, 65),
- colorkey = list(at = (seq(min(meanFrame[meanFrame$neurType == i, "abspe"]),
- max(meanFrame[meanFrame$neurType == i, "abspe"]),
- 0.05))),
- main = paste("Mean Abs Error,", i))
-
- smoothList[[i]] <- levelplot(abspe ~ dist * step, data = meanFrame[meanFrame$neurType == i, ], col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(150,70),
- xlim = c(3,30),
- cuts = 20,
- colorkey = list(at = (seq(min(meanFrame[meanFrame$neurType == i, "abspe"]),
- max(meanFrame[meanFrame$neurType == i, "abspe"]),
- 0.05))),
- main = paste("Mean Abs Error,", i),
- panel = panel.levelplot.points, cex = 0) +
- layer_(panel.2dsmoother(..., n = 200))
-
- }
- # Plot
- # setwd("EstereoAnalysis/")
- #
- # png(filename="meanProbabilities_heatmap_CV.png", type="cairo",
- # units="in", width=20, height=10, pointsize=12,
- # res=300)
- gridExtra::grid.arrange(grobs = c(heatList, smoothList), ncol = 4, nrow = 2)
- # dev.off()
- ```
- ###Density
- ```{r, fig.width=5, fig.height=20}
- ggplot(testJoin, aes(x = `pe`, y = `interact`, fill = ifelse(..x.. >=-5 & ..x.. <=5, ">= -5", "< 5"))) +
- scale_fill_manual(values = c("gray70", "coral2"), name = NULL) +
- geom_density_ridges_gradient(alpha = 0.8) +
- theme_ridges() +
- theme(legend.position = "none") +
- xlab("% Error") +
- xlim(c(-40,40))
-
- ```
- ###Errors vs cumul probability
- ```{r, fig.height=5, fig.width=20}
- # Inverse quantile
- quantInv <- function(distr, value) ecdf(distr)(value)
- # Function to apply to all axon types
- quantType <- function(peFrame, probSeq) {
-
- probList <- list()
-
- for (i in unique(peFrame$interact)) {
-
- dataProb <- peFrame[peFrame$interact == i, "pe"]
- probVec <- numeric()
-
- for (j in probSeq) {
-
- errProb <- quantInv(dataProb$pe, j) - quantInv(dataProb$pe, -j)
- probVec <- c(probVec, errProb)
-
- }
-
- probList[[i]] <- probVec
-
- }
-
- return(probList)
-
- }
- # Define errors for which to calculate probability
- binProb <- 0.5
- probSeq <- seq(binProb, 100, binProb)
- # Store axon types in lists
- # axProb <- lapply(frameList, function(x) quantType(x, probSeq))
- axProb <- quantType(testJoin, probSeq)
- # Save
- # saveRDS(axProb, "errorProbs_CVleaveout.rds")
- ```
- ######Plot heatmap
- ```{r, width=15, height=10}
- library(latticeExtra)
- # Reformat as dataframe
- binProb <- 0.5
- probSeq <- seq(binProb, 100, binProb)
- # axProb <- readRDS("errorProbs_CVleaveout.rds")
- # probFrame <- data.frame(error = rep(probSeq, 90*4),
- # neurType = rep(c("Type1", "Type2", "Type3", "Type4"), each = length(probSeq)*90),
- # dist = rep(rep(unique(allData$dist), each = length(probSeq)*9), 4),
- # step = rep(rep(unique(allData$step), each = length(probSeq)), 10*4),
- # prob = unlist(axProb))
- probFrame <- data.frame(error = rep(probSeq, 90),
- neurType = rep("Type1", length(probSeq)*90),
- dist = rep(unique(allData$dist), each = length(probSeq)*9),
- step = rep(rep(unique(allData$step), each = length(probSeq)), 10),
- prob = unlist(axProb))
- # Plot conttour plot for 5% error
- library(viridisLite)
- coul <- viridis(1000)
- # Store heatmaps
- heatList <- list()
- smoothList <- list()
- for (i in c("Type1")) {
-
- levelList1 <- list()
- levelList2 <- list()
-
- for (j in c(5, 10, 20)) {
-
- dataPlot <- probFrame[probFrame$neurType == i & probFrame$error == j, ]
-
- levelList1[[as.character(j)]] <- levelplot(prob ~ dist * step, data = dataPlot,
- col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(155,65),
- colorkey = list(at = (seq(min(dataPlot$prob),
- max(dataPlot$prob),
- 0.001))),
- main = paste("P(%Error <=", j, ")", sep = ""))
-
- levelList2[[as.character(j)]] <- levelplot(prob ~ dist * step, data = dataPlot,
- col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(150,70),
- xlim = c(3,30),
- cuts = 20,
- colorkey = list(at = (seq(min(dataPlot$prob),
- max(dataPlot$prob),
- 0.001))),
- main = paste("P(%Error <=", j, ")", sep = ""),
- panel = panel.levelplot.points, cex = 0) +
- layer_(panel.2dsmoother(..., n = 200))
- }
-
- heatList[[i]] <- levelList1
- smoothList[[i]] <- levelList2
- }
- # Plot
- # setwd("EstereoAnalysis/")
- for (i in c("Type1")) {
- png(filename=paste("prueba_", i, "_CVleaveout.png", sep=""), type="cairo",
- units="in", width=15, height=10, pointsize=12,
- res=300)
- gridExtra::grid.arrange(grobs = c(heatList[[i]], smoothList[[i]]), ncol = 3, nrow = 2)
- dev.off()
- }
- ```
- ##Plotting for predMean
- ```{r}
- predsData <- readRDS("D:/Sergio/Cremoto Drive/stereo_boot10CVPI_LRLP_predRealMean_GEE_Type1.rds")
- # jjMse <- jj %>%
- # group_by(dist, step) %>%
- # summarise(val=mltools::mse(pred, LR))
- testJoin <- do.call(rbind, predsData[[1]])
- testJoin$interact <- interaction(testJoin$dist, testJoin$step, lex.order = TRUE)
- testMean <- aggregate(abspe ~ dist + step, testJoin, mean)
- peJoin <- do.call(rbind, predsData[[2]])
- peJoin$interact <- interaction(peJoin$dist, peJoin$step, lex.order = TRUE)
- ```
- ###Mean
- ```{r, fig.height=10, fig.width=20}
- meanFrame <- testMean
- meanFrame$neurType <- rep("Type1", dim(meanFrame)[1])
- # Library
- library(latticeExtra)
- # Contour color palette
- colfunc <- colorRampPalette(c("navy","royalblue","springgreen","gold","yellow"))
- colfin <- colfunc(1000)
- library(viridisLite)
- coul <- viridis(1000)
- # Store heatmaps
- heatList <- list()
- smoothList <- list()
- for (i in c("Type1")) {
-
- heatList[[i]] <- levelplot(abspe ~ dist * step, data = meanFrame[meanFrame$neurType == i, ], col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(155, 65),
- colorkey = list(at = (seq(min(meanFrame[meanFrame$neurType == i, "abspe"]),
- max(meanFrame[meanFrame$neurType == i, "abspe"]),
- 0.05))),
- main = paste("Mean Abs Error,", i))
-
- smoothList[[i]] <- levelplot(abspe ~ dist * step, data = meanFrame[meanFrame$neurType == i, ], col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(150,70),
- xlim = c(3,30),
- cuts = 20,
- colorkey = list(at = (seq(min(meanFrame[meanFrame$neurType == i, "abspe"]),
- max(meanFrame[meanFrame$neurType == i, "abspe"]),
- 0.05))),
- main = paste("Mean Abs Error,", i),
- panel = panel.levelplot.points, cex = 0) +
- layer_(panel.2dsmoother(..., n = 200))
-
- }
- # Plot
- # setwd("EstereoAnalysis/")
- #
- # png(filename="meanProbabilities_heatmap_CV.png", type="cairo",
- # units="in", width=20, height=10, pointsize=12,
- # res=300)
- gridExtra::grid.arrange(grobs = c(heatList, smoothList), ncol = 4, nrow = 2)
- # dev.off()
- ```
- ###Density
- ```{r, fig.width=5, fig.height=20}
- # https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html
- ggplot(peJoin, aes(x = `pe`, y = `interact`, fill = ifelse(..x.. >=-5 & ..x.. <=5, ">= -5", "< 5"))) +
- scale_fill_manual(values = c("gray70", "coral2"), name = NULL) +
- stat_density_ridges(geom = "density_ridges_gradient") +
- theme_ridges() +
- theme(legend.position = "none") +
- xlab("% Error") +
- xlim(c(-40,40))
- ```
- ```{r, fig.width=50, fig.height=45}
- histogram(~ pe | factor(dist) + factor(step), data = peJoin)
- ```
- ###Errors vs cumul probability
- ```{r, fig.height=5, fig.width=20}
- # Inverse quantile
- quantInv <- function(distr, value) ecdf(distr)(value)
- # Function to apply to all axon types
- quantType <- function(peFrame, probSeq) {
-
- probList <- list()
-
- for (i in unique(peFrame$interact)) {
-
- dataProb <- peFrame[peFrame$interact == i, "pe"]
- probVec <- numeric()
-
- for (j in probSeq) {
-
- errProb <- quantInv(dataProb$pe, j) - quantInv(dataProb$pe, -j)
- probVec <- c(probVec, errProb)
-
- }
-
- probList[[i]] <- probVec
-
- }
-
- return(probList)
-
- }
- # Define errors for which to calculate probability
- binProb <- 0.5
- probSeq <- seq(binProb, 100, binProb)
- # Store axon types in lists
- # axProb <- lapply(frameList, function(x) quantType(x, probSeq))
- axProb <- quantType(peJoin, probSeq)
- # Save
- # saveRDS(axProb, "errorProbs_CVleaveout.rds")
- ```
- ######Plot heatmap
- ```{r, width=15, height=10}
- library(latticeExtra)
- # Reformat as dataframe
- binProb <- 0.5
- probSeq <- seq(binProb, 100, binProb)
- # axProb <- readRDS("errorProbs_CVleaveout.rds")
- # probFrame <- data.frame(error = rep(probSeq, 90*4),
- # neurType = rep(c("Type1", "Type2", "Type3", "Type4"), each = length(probSeq)*90),
- # dist = rep(rep(unique(allData$dist), each = length(probSeq)*9), 4),
- # step = rep(rep(unique(allData$step), each = length(probSeq)), 10*4),
- # prob = unlist(axProb))
- probFrame <- data.frame(error = rep(probSeq, 90),
- neurType = rep("Type1", length(probSeq)*90),
- dist = rep(unique(allData$dist), each = length(probSeq)*9),
- step = rep(rep(unique(allData$step), each = length(probSeq)), 10),
- prob = unlist(axProb))
- # Plot conttour plot for 5% error
- library(viridisLite)
- coul <- viridis(1000)
- # Store heatmaps
- heatList <- list()
- smoothList <- list()
- for (i in c("Type1")) {
-
- levelList1 <- list()
- levelList2 <- list()
-
- for (j in c(5, 10, 20)) {
-
- dataPlot <- probFrame[probFrame$neurType == i & probFrame$error == j, ]
-
- levelList1[[as.character(j)]] <- levelplot(prob ~ dist * step, data = dataPlot,
- col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(155,65),
- colorkey = list(at = (seq(min(dataPlot$prob),
- max(dataPlot$prob),
- 0.001))),
- main = paste("P(%Error <=", j, ")", sep = ""))
-
- levelList2[[as.character(j)]] <- levelplot(prob ~ dist * step, data = dataPlot,
- col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(150,70),
- xlim = c(3,30),
- cuts = 20,
- colorkey = list(at = (seq(min(dataPlot$prob),
- max(dataPlot$prob),
- 0.001))),
- main = paste("P(%Error <=", j, ")", sep = ""),
- panel = panel.levelplot.points, cex = 0) +
- layer_(panel.2dsmoother(..., n = 200))
- }
-
- heatList[[i]] <- levelList1
- smoothList[[i]] <- levelList2
- }
- # Plot
- # setwd("EstereoAnalysis/")
- for (i in c("Type1")) {
- png(filename=paste("prueba_", i, "_CVleaveout.png", sep=""), type="cairo",
- units="in", width=15, height=10, pointsize=12,
- res=300)
- gridExtra::grid.arrange(grobs = c(heatList[[i]], smoothList[[i]]), ncol = 3, nrow = 2)
- dev.off()
- }
- ```
- ##Plotting for CVloo
- ```{r}
- predsData <- readRDS("D:/Sergio/Cremoto Drive/stereo_bootCVlooPI_LRLP_predReal_Type1.rds")
- # jjMse <- jj %>%
- # group_by(dist, step) %>%
- # summarise(val=mltools::mse(pred, LR))
- testJoin <- predsData
- testJoin$interact <- interaction(testJoin$dist, testJoin$step, lex.order = TRUE)
- testMean <- aggregate(abspe ~ dist + step, testJoin, mean)
- peJoin <- predsData
- peJoin$interact <- interaction(peJoin$dist, peJoin$step, lex.order = TRUE)
- ```
- ###Mean
- ```{r, fig.height=10, fig.width=20}
- meanFrame <- testMean
- meanFrame$neurType <- rep("Type1", dim(meanFrame)[1])
- # Library
- library(latticeExtra)
- # Contour color palette
- colfunc <- colorRampPalette(c("navy","royalblue","springgreen","gold","yellow"))
- colfin <- colfunc(1000)
- library(viridisLite)
- coul <- viridis(1000)
- # Store heatmaps
- heatList <- list()
- smoothList <- list()
- for (i in c("Type1")) {
-
- heatList[[i]] <- levelplot(abspe ~ dist * step, data = meanFrame[meanFrame$neurType == i, ], col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(155, 65),
- colorkey = list(at = (seq(min(meanFrame[meanFrame$neurType == i, "abspe"]),
- max(meanFrame[meanFrame$neurType == i, "abspe"]),
- 0.05))),
- main = paste("Mean Abs Error,", i))
-
- smoothList[[i]] <- levelplot(abspe ~ dist * step, data = meanFrame[meanFrame$neurType == i, ], col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(150,70),
- xlim = c(3,30),
- cuts = 20,
- colorkey = list(at = (seq(min(meanFrame[meanFrame$neurType == i, "abspe"]),
- max(meanFrame[meanFrame$neurType == i, "abspe"]),
- 0.05))),
- main = paste("Mean Abs Error,", i),
- panel = panel.levelplot.points, cex = 0) +
- layer_(panel.2dsmoother(..., n = 200))
-
- }
- # Plot
- # setwd("EstereoAnalysis/")
- #
- # png(filename="meanProbabilities_heatmap_CV.png", type="cairo",
- # units="in", width=20, height=10, pointsize=12,
- # res=300)
- gridExtra::grid.arrange(grobs = c(heatList, smoothList), ncol = 4, nrow = 2)
- # dev.off()
- ```
- ###Density
- ```{r, fig.width=5, fig.height=20}
- # https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html
- print(ggplot(peJoin, aes(x = `pe`, y = `interact`, fill = ifelse(..x.. >=-5 & ..x.. <=5, ">= -5", "< 5"))) +
- scale_fill_manual(values = c("gray70", "coral2"), name = NULL) +
- stat_density_ridges(geom = "density_ridges_gradient") +
- theme_ridges() +
- theme(legend.position = "none") +
- xlab("% Error") +
- xlim(c(-40,40)))
- ```
- ```{r, fig.width=50, fig.height=45}
- histogram(~ pe | factor(dist) + factor(step), data = peJoin)
- ```
- ###Errors vs cumul probability
- ```{r, fig.height=5, fig.width=20}
- # Inverse quantile
- quantInv <- function(distr, value) ecdf(distr)(value)
- # Function to apply to all axon types
- quantType <- function(peFrame, probSeq) {
-
- probList <- list()
-
- for (i in unique(peFrame$interact)) {
-
- dataProb <- peFrame[peFrame$interact == i, "pe"]
- probVec <- numeric()
-
- for (j in probSeq) {
-
- errProb <- quantInv(dataProb$pe, j) - quantInv(dataProb$pe, -j)
- probVec <- c(probVec, errProb)
-
- }
-
- probList[[i]] <- probVec
-
- }
-
- return(probList)
-
- }
- # Define errors for which to calculate probability
- binProb <- 0.5
- probSeq <- seq(binProb, 100, binProb)
- # Store axon types in lists
- # axProb <- lapply(frameList, function(x) quantType(x, probSeq))
- axProb <- quantType(peJoin, probSeq)
- # Save
- # saveRDS(axProb, "errorProbs_CVleaveout.rds")
- ```
- ######Plot heatmap
- ```{r, width=15, height=10}
- library(latticeExtra)
- # Reformat as dataframe
- binProb <- 0.5
- probSeq <- seq(binProb, 100, binProb)
- # axProb <- readRDS("errorProbs_CVleaveout.rds")
- # probFrame <- data.frame(error = rep(probSeq, 90*4),
- # neurType = rep(c("Type1", "Type2", "Type3", "Type4"), each = length(probSeq)*90),
- # dist = rep(rep(unique(allData$dist), each = length(probSeq)*9), 4),
- # step = rep(rep(unique(allData$step), each = length(probSeq)), 10*4),
- # prob = unlist(axProb))
- probFrame <- data.frame(error = rep(probSeq, 90),
- neurType = rep("Type1", length(probSeq)*90),
- dist = rep(unique(allData$dist), each = length(probSeq)*9),
- step = rep(rep(unique(allData$step), each = length(probSeq)), 10),
- prob = unlist(axProb))
- # Plot conttour plot for 5% error
- library(viridisLite)
- coul <- viridis(1000)
- # Store heatmaps
- heatList <- list()
- smoothList <- list()
- for (i in c("Type1")) {
-
- levelList1 <- list()
- levelList2 <- list()
-
- for (j in c(5, 10, 20)) {
-
- dataPlot <- probFrame[probFrame$neurType == i & probFrame$error == j, ]
-
- levelList1[[as.character(j)]] <- levelplot(prob ~ dist * step, data = dataPlot,
- col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(155,65),
- colorkey = list(at = (seq(min(dataPlot$prob),
- max(dataPlot$prob),
- 0.001))),
- main = paste("P(%Error <=", j, ")", sep = ""))
-
- levelList2[[as.character(j)]] <- levelplot(prob ~ dist * step, data = dataPlot,
- col.regions = coul,
- scales = list(y = list(at = unique(allData$step), labels = unique(allData$step)),
- x = list(at = unique(allData$dist), labels = unique(allData$dist))),
- ylim = c(150,70),
- xlim = c(3,30),
- cuts = 20,
- colorkey = list(at = (seq(min(dataPlot$prob),
- max(dataPlot$prob),
- 0.001))),
- main = paste("P(%Error <=", j, ")", sep = ""),
- panel = panel.levelplot.points, cex = 0) +
- layer_(panel.2dsmoother(..., n = 200))
- }
-
- heatList[[i]] <- levelList1
- smoothList[[i]] <- levelList2
- }
- # Plot
- # setwd("EstereoAnalysis/")
- for (i in c("Type1")) {
- png(filename=paste("prueba_", i, "_CVleaveout.png", sep=""), type="cairo",
- units="in", width=15, height=10, pointsize=12,
- res=300)
- gridExtra::grid.arrange(grobs = c(heatList[[i]], smoothList[[i]]), ncol = 3, nrow = 2)
- dev.off()
- }
- ```
|