<%@include file="includes/setup.md.rsp"%>

<%@string fcnname="weightedMedian"%>
<% fcnname <- "<%@string name="fcnname"%>" %>
<%@meta title="${fcnname}() benchmarks"%>
<%@meta author="Henrik Bengtsson"%>
<%@meta date="2014-06-03"%>

<%@include file="${header}"%>

<%---
 'ergm' could be hard to install, because it imports
 'Rglpk', which requires GLPK library on the system.
 ---%>
<%@string test_ergm="FALSE"%>

<%
use("limma", how = "load")
limma_weighted.median <- limma::weighted.median

use("cwhmisc", how = "load")
cwhmisc_w.median <- cwhmisc::w.median

use("laeken", how = "load")
laeken_weightedMedian <- laeken::weightedMedian

<%@ifeq test_ergm="TRUE"%>
use("ergm", how = "load")
ergm_wtd.median <- ergm::wtd.median
<%@endif%>

weightedMedian <- matrixStats::weightedMedian
%>

# <%@meta name="title"%>

This report benchmark the performance of <%=fcnname%>() against alternative methods.

## Alternative methods

* apply() + limma::weighted.median()
* apply() + cwhmisc::w.median()
* apply() + laeken::weightedMedian()
<%@ifeq test_ergm="TRUE"%>
* apply() + ergm::wtd.median() --%>
<%@endif%>


## Data
```r
<%=withCapture({
<%@include file="R/random-vectors.R"%>
data <- rvectors(mode = "double")
data <- data[1:3]
})%>
```

## Results

<% for (ii in seq_along(data)) { %>
<%
dataLabel <- names(data)[ii]
message(dataLabel)
x <- data[[dataLabel]]
gc()
%>
### <%=dataLabel%> vector

```r
<%=withCapture({
x <- data[[.dataLabel.]]
w <- runif(length(x))
gc()

stats <- microbenchmark(
 "weightedMedian"         = weightedMedian(x, w = w, ties = "mean", na.rm = FALSE),
 "limma::weighted.median" = limma_weighted.median(x, w = w, na.rm = FALSE),
 "cwhmisc::w.median"      = cwhmisc_w.median(x, w = w),
 "laeken::weightedMedian" = laeken_weightedMedian(x, w = w),
<%@ifeq test_ergm="TRUE"%>
 "ergm::wtd.median"       = ergm_wtd.median(x, w = w),
<%@endif%>
  unit = "ms"
)
})%>
```

<% benchmarkResults(stats, tags=dataLabel) %>

<% } # for (ii ...) %>

<%@include file="${footer}"%>


<%---------------------------------------------------------------------------
HISTORY:
2014-12-17
o Made 'ergm::wtd.median' optional.
2014-06-03
o Created using benchmark snippet in incl/weightedMedian.Rex.
---------------------------------------------------------------------------%>