Ignore some more files
[morpheus.git] / pkg / R / plot.R
... / ...
CommitLineData
1# extractParam
2#
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.
6#
7# @inheritParams plotHist
8#
9.extractParam <- function(mr, x=1, y=1)
10{
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])
19}
20
21#' plotHist
22#'
23#' Plot compared histograms of a single parameter (scalar)
24#'
25#' @name plotHist
26#'
27#' @param mr Output of multiRun(), list of lists of functions results
28#' @param x Row index of the element inside the aggregated parameter
29#' @param y Column index of the element inside the aggregated parameter
30#' @param ... Additional graphical parameters (xlab, ylab, ...)
31#'
32#' @examples
33#' \dontrun{
34#' β <- matrix(c(1,-2,3,1),ncol=2)
35#' mr <- multiRun(...) #see bootstrap example in ?multiRun
36#' #mr[[i]] is a list of estimated parameters matrices
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}
41#'
42#' @export
43plotHist <- function(mr, x, y, ...)
44{
45 params <- .extractParam(mr, x, y)
46 L <- length(params)
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))
49 args <- list(...)
50 for (i in 1:L)
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 }
56}
57
58# NOTE: roxygen2 bug, "@inheritParams plotHist" fails in next header:
59
60#' plotBox
61#'
62#' Draw compared boxplots of a single parameter (scalar)
63#'
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, ...)
70#'
71#' @examples
72#' \dontrun{
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#'
81#' @export
82plotBox <- function(mr, x, y, ...)
83{
84 params <- .extractParam(mr, x, y)
85 L <- length(params)
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))
88 args <- list(...)
89 for (i in 1:L)
90 {
91 boxplot(params[[i]], ...)
92 }
93}
94
95#' plotCoefs
96#'
97#' Draw a graph of (averaged) coefficients estimations with their standard,
98#' deviations ordered by mean values.
99#' Note that the drawing does not correspond to a function; it is just a
100#' convenient way to visualize the estimated parameters.
101#'
102#' @name plotCoefs
103#'
104#' @param mr List of parameters matrices
105#' @param params True value of the parameters matrix
106#' @param ... Additional graphical parameters
107#'
108#' @examples
109#' \dontrun{
110#' β <- matrix(c(1,-2,3,1),ncol=2)
111#' mr <- multiRun(...) #see bootstrap example in ?multiRun
112#' #mr[[i]] is a list of estimated parameters matrices
113#' μ <- normalize(β)
114#' for (i in 1:2)
115#' mr[[i]] <- alignMatrices(res[[i]], ref=μ, ls_mode="exact")
116#' params <- rbind( c(.5,.5), β, c(0,0) ) #p, β, b stacked in a matrix
117#' plotCoefs(mr[[1]], params)}
118#'
119#' @export
120plotCoefs <- function(mr, params, ...)
121{
122 d <- nrow(mr[[1]])
123 K <- ncol(mr[[1]])
124
125 params_hat <- matrix(nrow=d, ncol=K)
126 stdev <- matrix(nrow=d, ncol=K)
127 for (x in 1:d)
128 {
129 for (y in 1:K)
130 {
131 estims <- .extractParam(mr, x, y)
132 params_hat[x,y] <- mean(estims)
133 # Another way to compute stdev: using distances to true params
134# stdev[x,y] <- sqrt( mean( (estims - params[x,y])^2 ) )
135 # HACK remove extreme quantile in estims[[i]] before computing sd()
136 stdev[x,y] <- sd(estims) #[ estims < max(estims) & estims > min(estims) ] )
137 }
138 }
139
140 par(cex.axis=1.5, cex.lab=1.5, mar=c(4.7,5,1,1))
141 params <- as.double(params)
142 o <- order(params)
143 avg_param <- as.double(params_hat)
144 std_param <- as.double(stdev)
145 args <- list(...)
146 matplot(
147 cbind(params[o],avg_param[o],
148 avg_param[o]+std_param[o],avg_param[o]-std_param[o]),
149 col=1, lty=c(1,5,3,3), type="l", lwd=2,
150 xlab=ifelse("xlab" %in% names(args), args$xlab, "Parameter index"),
151 ylab=ifelse("ylab" %in% names(args), args$ylab, "") )
152
153 #print(o) #not returning o to avoid weird Jupyter issue... (TODO:)
154}