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