Skip to content

Commit d72f2e7

Browse files
committed
Update documentation
1 parent 3705af0 commit d72f2e7

9 files changed

Lines changed: 82 additions & 30 deletions

File tree

DESCRIPTION

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
Package: mbr
22
Type: Package
33
Title: Mass Balance Reconstruction
4-
Version: 0.1.0
4+
Version: 0.0.1
55
Author: Hung Nguyen
66
Maintainer: Hung Nguyen <ntthung@gmail.com>
7-
Description: Mass-balance-adjusted Reconstruction
8-
Streamflow reconstruction at sub-annual resolution (e.g., seasonal or monthly), with a penalty term
9-
to minimize the differences between the total sub-annual flows and the annual flow.
7+
Description: Mass-balance-adjusted Regression algorithm for streamflow reconstruction at sub-annual resolution (e.g., seasonal or monthly). The algorithm implements a penalty term to minimize the differences between the total sub-annual flows and the annual flow. The method is described in Nguyen et al (2020) <DOI: 10.1002/essoar.10504791.1>.
108
License: GPL (>= 2.0)
119
Encoding: UTF-8
1210
LazyData: true
@@ -20,12 +18,13 @@ Imports:
2018
Matrix,
2119
Rfast,
2220
stats
23-
RoxygenNote: 7.1.0
21+
RoxygenNote: 7.1.1
2422
Roxygen:
2523
list(markdown = TRUE)
2624
URL: https://github.com/ntthung/mbr
2725
BugReports: https://github.com/ntthung/mbr/issues
2826
Suggests:
27+
FWDselect,
2928
knitr,
3029
rmarkdown,
3130
testthat,

NAMESPACE

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,11 @@
33
export(KGE)
44
export(NSE)
55
export(RE)
6-
export(back_trans)
76
export(calculate_metrics)
8-
export(colScale)
9-
export(colUnscale)
107
export(cv_mb)
11-
export(cv_site_selection)
12-
export(input_selection)
13-
export(lsq_mb)
14-
export(mb_fit)
8+
export(make_Z)
159
export(mb_reconstruction)
1610
export(nRMSE)
17-
export(obj_fun)
18-
export(prepend_ones)
19-
export(rowScale)
20-
export(rowUnscale)
21-
export(wPCA)
2211
import(data.table)
2312
importFrom(Matrix,.bdiag)
2413
importFrom(Rfast,colVars)

R/R-utils.R

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#' @return The scaled matrix.
77
#' @section Reference:
88
#' This function was adopted from John Muschelli's code on \href{hopstat.wordpress.com/2016/02/23/a-faster-scale-function/}{StackOverflow}, but I changed the underlying functions to calculate mean and standard deviation from `matrixStats` to `Rfast`, which is much faster.
9-
#' @export
109
colScale <- function(x, add_attr = TRUE) {
1110

1211
# Get the column means
@@ -26,8 +25,8 @@ colScale <- function(x, add_attr = TRUE) {
2625
#' Scale rows of a Matrix
2726
#'
2827
#' Similar to [colScale]
28+
#' @inheritParams colScale
2929
#' @return The scaled matrix.
30-
#' @export
3130
rowScale <- function(x, add_attr = TRUE) {
3231

3332
# Get the column means
@@ -50,7 +49,6 @@ rowScale <- function(x, add_attr = TRUE) {
5049
#' @param cm A vector of column means
5150
#' @param csd A vector of column standard deviations
5251
#' @return The unscaled matrix
53-
#' @export
5452
colUnscale <- function(x, cm, csd) {
5553
t(t(x) * csd + cm)
5654
}
@@ -62,7 +60,6 @@ colUnscale <- function(x, cm, csd) {
6260
#' @param rm A vector of row means
6361
#' @param rsd A vector of row standard deviations
6462
#' @return The unscaled matrix
65-
#' @export
6663
rowUnscale <- function(x, rm, rsd) {
6764
x * rsd + rm
6865
}
@@ -95,6 +92,37 @@ calculate_metrics <- function(sim, obs, z, norm.fun = mean) {
9592
)
9693
}
9794

95+
#' Make cross-validation folds.
96+
#'
97+
#' Make a list of cross-validation folds. Each element of the list is a vector of the cross-validation points for one cross-validation run.
98+
#' @param obs Vector of observations.
99+
#' @param nRuns Number of repetitions.
100+
#' @param frac Fraction of left-out points. For leave-one-out, use `frac = 1`, otherwise use any value less than 1. Default is 0.1 (leave-10%-out).
101+
#' @param contiguous Logical. If `TRUE`, the default, the left-out points are made in contiguous blocks; otherwise, they are scattered randomly.
102+
#' @return A list of cross-validation folds
103+
#' @examples
104+
#' Z <- make_Z(p1Seasonal$Qa, nRuns = 30, frac = 0.25, contiguous = TRUE)
105+
#' @export
106+
make_Z <- function(obs, nRuns = 30, frac = 0.1, contiguous = TRUE) {
107+
obsInd <- which(!is.na(obs))
108+
if (frac == 1) {
109+
split(obsInd, obsInd)
110+
} else {
111+
n <- length(obsInd)
112+
k <- floor(n * frac) # leave-k-out
113+
if (contiguous) {
114+
maxInd <- n - k # Highest possible position in of obsInd
115+
if (maxInd < nRuns) { # Not enough samples, reduce k
116+
maxInd <- nRuns
117+
k <- n - nRuns
118+
}
119+
lapply(sort(sample(1:maxInd, nRuns)), function(x) obsInd[x:(x + k)])
120+
} else {
121+
replicate(nRuns, sort(sample(obsInd, k, replace = FALSE)), simplify = FALSE)
122+
}
123+
}
124+
}
125+
98126
#' Nash-Sutcliffe Efficiency
99127
#'
100128
#' @param yhat Model outputs
@@ -138,9 +166,9 @@ nRMSE <- function(yhat, y, normConst) {
138166
KGE <- function(yhat, y) {
139167
mu <- mean(y)
140168
mu_hat <- mean(yhat)
141-
sigma <- sd(y)
142-
sigma_hat <- sd(yhat)
143-
r <- cor(yhat, y)
169+
sigma <- stats::sd(y)
170+
sigma_hat <- stats::sd(yhat)
171+
r <- stats::cor(yhat, y)
144172
alpha <- sigma_hat / sigma
145173
beta <- mu_hat / mu
146174
EDsq <- (r - 1) * (r - 1) + (alpha - 1) * (alpha - 1) + (beta - 1) * (beta - 1)

man/back_trans.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cv_mb.Rd

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/make_Z.Rd

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/mb_reconstruction.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/mbr.Rd

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/rowScale.Rd

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)