Skip to content

Commit 2cf326c

Browse files
committed
Add vignette
1 parent ce07bd2 commit 2cf326c

3 files changed

Lines changed: 101 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
.Ruserdata
55

66
experiments/
7+
inst/doc

vignettes/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.html
2+
*.R
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
title: "Streamflow reconstruction with mass balance adjustment"
3+
output: rmarkdown::html_vignette
4+
vignette: >
5+
%\VignetteIndexEntry{Streamflow reconstruction with mass balance adjustment}
6+
%\VignetteEngine{knitr::rmarkdown}
7+
%\VignetteEncoding{UTF-8}
8+
---
9+
10+
```{r, include = FALSE}
11+
knitr::opts_chunk$set(
12+
collapse = TRUE,
13+
comment = "#>"
14+
)
15+
```
16+
17+
```{r setup}
18+
library(mbr)
19+
```
20+
21+
# Introduction
22+
23+
The package has two main functions, `mb_reconstruction()` for reconstruction, and `cv_mb()` for cross-validation. This vignette will demonstrate these functions using the two built-in data sets. First, let's look at the built-in data, taken from Nguyen et al (2021).
24+
25+
The data frame `p1Seasonal` contains the reconstruction targets, namely the dry season, wet season, and water year streamflow for the Ping River at station P.1 (Chiang Mai, Thailand). The data span from 1922 to 2003.
26+
27+
```{r}
28+
p1Seasonal
29+
```
30+
31+
As paleoclimate proxies, we use the principal components (PCs) of the Southeast Asian Dendrochronology Network. A set of PCs has been derived for each target (see details in Nguyen et al, 2020). These are provided in `pc3seasons`.
32+
33+
```{r}
34+
str(pc3seasons)
35+
```
36+
37+
The tree ring data spans from 1750 to 2003. Let us look at the first 10 rows of each principal component matrix.
38+
39+
```{r}
40+
lapply(pc3seasons, head, n = 10)
41+
```
42+
43+
# Reconstruction
44+
45+
We build a reconstruction with the full data set.
46+
47+
```{r}
48+
fit <- mb_reconstruction(
49+
instQ = p1Seasonal,
50+
pc.list = pc3seasons,
51+
start.year = 1750,
52+
lambda = 1,
53+
log.trans = 1:3
54+
)
55+
```
56+
57+
We need to provide the instrumental data (`instQ`) and the PC list (`pc.list`). Since the PC data do not have a time column, we need to provide `start.year`, 1750 in this case.
58+
59+
For the mass balance adjustment, we need to provide a penalty weight `lambda`. The default value is 1 and it works in this case. But for other applications you may need to test a few values for `lambda` to figure out the optimal value.
60+
61+
Finally, the argument `log.trans` provides the indices of the targets that need to be log transformed. Here we transform all three targets.
62+
63+
Let's look at the results.
64+
65+
```{r}
66+
fit
67+
```
68+
69+
# Cross-validation
70+
71+
Let us now cross-validate the model with a hold-out-25% scheme. The cross-validation folds can be created with the function `make_Z()`.
72+
73+
```{r}
74+
# Create hold-out chunks
75+
set.seed(24)
76+
cvFolds <- make_Z(
77+
obs = 1922:2003,
78+
nRuns = 50,
79+
frac = 0.25,
80+
contiguous = TRUE
81+
)
82+
# Run cross validation
83+
cv <- cv_mb(
84+
instQ = p1Seasonal,
85+
pc.list = pc3seasons,
86+
cv.folds = cvFolds,
87+
start.year = 1750,
88+
lambda = 1,
89+
log.trans = 1:3,
90+
return.type = 'metric means'
91+
)
92+
# Round up to two decimal places
93+
cv[, (2:6) := lapply(.SD, round, digits = 2), .SDcols = 2:6][]
94+
```
95+
96+
# References
97+
98+
Nguyen, H. T. T., Galelli, S., Xu, C., & Buckley, B. (2020). Multi-Proxy, Multi-Season Streamflow Reconstruction with Mass Balance Adjustment. Earth and Space Science Open Archive, 22. https://doi.org/10.1002/essoar.10504791.1

0 commit comments

Comments
 (0)