Adjustments + bugs fixing
[morpheus.git] / pkg / R / plot.R
CommitLineData
cbd88fe5
BA
1# extractParam
2#
2b3a6af5
BA
3# Extract successive values of a projection of the parameter(s).
4# The method works both on a list of lists of results,
5# or on a single list of parameters matrices.
cbd88fe5
BA
6#
7# @inheritParams plotHist
8#
2b3a6af5 9.extractParam <- function(mr, x=1, y=1)
cbd88fe5 10{
2b3a6af5
BA
11 if (is.list(mr[[1]]))
12 {
13 # Obtain L vectors where L = number of res lists in mr
14 return ( lapply( mr, function(mr_list) {
15 sapply(mr_list, function(m) m[x,y])
16 } ) )
17 }
18 sapply(mr, function(m) m[x,y])
cbd88fe5
BA
19}
20
21#' plotHist
22#'
2b3a6af5 23#' Plot compared histograms of a single parameter (scalar)
cbd88fe5 24#'
ab35f610
BA
25#' @name plotHist
26#'
cbd88fe5
BA
27#' @param mr Output of multiRun(), list of lists of functions results
28#' @param x Row index of the element inside the aggregated parameter
5fc1b9d9 29#' @param y Column index of the element inside the aggregated parameter
2b3a6af5 30#' @param ... Additional graphical parameters (xlab, ylab, ...)
cbd88fe5
BA
31#'
32#' @examples
1b53e3a5 33#' \donttest{
cbd88fe5 34#' β <- matrix(c(1,-2,3,1),ncol=2)
2b3a6af5
BA
35#' mr <- multiRun(...) #see bootstrap example in ?multiRun
36#' #mr[[i]] is a list of estimated parameters matrices
cbd88fe5
BA
37#' μ <- normalize(β)
38#' for (i in 1:2)
39#' mr[[i]] <- alignMatrices(res[[i]], ref=μ, ls_mode="exact")
40#' plotHist(mr, 2, 1) #second row, first column}
2b3a6af5 41#'
cbd88fe5 42#' @export
2b3a6af5 43plotHist <- function(mr, x, y, ...)
cbd88fe5 44{
2b3a6af5 45 params <- .extractParam(mr, x, y)
ab35f610 46 L <- length(params)
6dd5c2ac
BA
47 # Plot histograms side by side
48 par(mfrow=c(1,L), cex.axis=1.5, cex.lab=1.5, mar=c(4.7,5,1,1))
2b3a6af5 49 args <- list(...)
6dd5c2ac 50 for (i in 1:L)
2b3a6af5
BA
51 {
52 hist(params[[i]], breaks=40, freq=FALSE,
53 xlab=ifelse("xlab" %in% names(args), args$xlab, "Parameter value"),
54 ylab=ifelse("ylab" %in% names(args), args$ylab, "Density"))
55 }
cbd88fe5
BA
56}
57
ab35f610
BA
58# NOTE: roxygen2 bug, "@inheritParams plotHist" fails in next header:
59
cbd88fe5
BA
60#' plotBox
61#'
2b3a6af5 62#' Draw compared boxplots of a single parameter (scalar)
cbd88fe5 63#'
ab35f610
BA
64#' @name plotBox
65#'
66#' @param mr Output of multiRun(), list of lists of functions results
67#' @param x Row index of the element inside the aggregated parameter
68#' @param y Column index of the element inside the aggregated parameter
69#' @param ... Additional graphical parameters (xlab, ylab, ...)
cbd88fe5
BA
70#'
71#' @examples
2b3a6af5
BA
72#' \donttest{
73#' β <- matrix(c(1,-2,3,1),ncol=2)
74#' mr <- multiRun(...) #see bootstrap example in ?multiRun
75#' #mr[[i]] is a list of estimated parameters matrices
76#' μ <- normalize(β)
77#' for (i in 1:2)
78#' mr[[i]] <- alignMatrices(res[[i]], ref=μ, ls_mode="exact")
79#' plotBox(mr, 2, 1) #second row, first column}
80#'
cbd88fe5 81#' @export
2b3a6af5 82plotBox <- function(mr, x, y, ...)
cbd88fe5 83{
2b3a6af5 84 params <- .extractParam(mr, x, y)
ab35f610 85 L <- length(params)
6dd5c2ac
BA
86 # Plot boxplots side by side
87 par(mfrow=c(1,L), cex.axis=1.5, cex.lab=1.5, mar=c(4.7,5,1,1))
2b3a6af5 88 args <- list(...)
6dd5c2ac 89 for (i in 1:L)
2b3a6af5
BA
90 {
91 boxplot(params[[i]],
92 ifelse("ylab" %in% names(args), args$ylab, "Parameter value"))
93 }
cbd88fe5
BA
94}
95
96#' plotCoefs
97#'
2b3a6af5
BA
98#' Draw a graph of (averaged) coefficients estimations with their standard,
99#' deviations ordered by mean values.
100#' Note that the drawing does not correspond to a function; it is just a
101#' convenient way to visualize the estimated parameters.
cbd88fe5 102#'
ab35f610
BA
103#' @name plotCoefs
104#'
2b3a6af5
BA
105#' @param mr List of parameters matrices
106#' @param params True value of the parameters matrix
107#' @param ... Additional graphical parameters
cbd88fe5
BA
108#'
109#' @examples
2b3a6af5
BA
110#' \donttest{
111#' β <- matrix(c(1,-2,3,1),ncol=2)
112#' mr <- multiRun(...) #see bootstrap example in ?multiRun
113#' #mr[[i]] is a list of estimated parameters matrices
114#' μ <- normalize(β)
115#' for (i in 1:2)
116#' mr[[i]] <- alignMatrices(res[[i]], ref=μ, ls_mode="exact")
117#' params <- rbind( c(.5,.5), β, c(0,0) ) #p, β, b stacked in a matrix
118#' plotCoefs(mr[[1]], params)}
119#'
cbd88fe5 120#' @export
2b3a6af5 121plotCoefs <- function(mr, params, ...)
cbd88fe5 122{
2b3a6af5
BA
123 d <- nrow(mr[[1]])
124 K <- ncol(mr[[1]])
cbd88fe5 125
2b3a6af5
BA
126 params_hat <- matrix(nrow=d, ncol=K)
127 stdev <- matrix(nrow=d, ncol=K)
128 for (x in 1:d)
6dd5c2ac
BA
129 {
130 for (y in 1:K)
131 {
2b3a6af5
BA
132 estims <- .extractParam(mr, x, y)
133 params_hat[x,y] <- mean(estims)
134 # Another way to compute stdev: using distances to true params
135# stdev[x,y] <- sqrt( mean( (estims - params[x,y])^2 ) )
6dd5c2ac 136 # HACK remove extreme quantile in estims[[i]] before computing sd()
2b3a6af5 137 stdev[x,y] <- sd(estims) #[ estims < max(estims) & estims > min(estims) ] )
6dd5c2ac
BA
138 }
139 }
cbd88fe5 140
6dd5c2ac
BA
141 par(cex.axis=1.5, cex.lab=1.5, mar=c(4.7,5,1,1))
142 params <- as.double(params)
143 o <- order(params)
144 avg_param <- as.double(params_hat)
145 std_param <- as.double(stdev)
2b3a6af5
BA
146 args <- list(...)
147 matplot(
148 cbind(params[o],avg_param[o],
149 avg_param[o]+std_param[o],avg_param[o]-std_param[o]),
150 col=1, lty=c(1,5,3,3), type="l", lwd=2,
151 xlab=ifelse("xlab" %in% names(args), args$xlab, "Parameter index"),
152 ylab=ifelse("ylab" %in% names(args), args$ylab, "") )
cbd88fe5 153
6dd5c2ac 154 #print(o) #not returning o to avoid weird Jupyter issue... (TODO:)
cbd88fe5 155}