-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
257 lines (216 loc) · 7.27 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
---
output: github_document
always_allow_html: yes
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "figures/README-",
out.width = "100%"
)
```
## fxTWAPLS: An Improved Version of WA-PLS <img src="https://raw.githubusercontent.com/special-uor/fxTWAPLS/master/inst/images/logo.png" alt="logo" align="right" height=200px/>
<!-- badges: start -->
<!-- `r badger::badge_code_size("special-uor/fxTWAPLS")` -->
`r badger::badge_devel("special-uor/fxTWAPLS", "yellow")`
`r badger::badge_cran_release("fxTWAPLS", "black")`
`r badger::badge_github_actions("special-uor/fxTWAPLS")`
`r badger::badge_doi("10.1098/rspa.2020.0346", "blue")`
<!-- `r badger::badge_codecov("special-uor/fxTWAPLS", "Q6SYL7AOGR")` -->
<!-- [![R build status](https://github.com/special-uor/fxTWAPLS/workflows/R-CMD-check/badge.svg)](https://github.com/special-uor/fxTWAPLS/actions) -->
<!-- [![CRAN status](https://www.r-pkg.org/badges/version/fxTWAPLS)](https://CRAN.R-project.org/package=fxTWAPLS) -->
<!-- badges: end -->
## Overview
The goal of this package is to provide an improved version of WA-PLS by
including the tolerances of taxa and the frequency of the sampled climate
variable. This package also provides a way of leave-out cross-validation that
removes both the test site and sites that are both geographically close and
climatically close for each cycle, to avoid the risk of pseudo-replication.
## Installation
<!-- ### Create a Personal Access Token (PAT) for Github -->
<!-- This is needed to install packages from private repositories. Once configured, -->
<!-- there is no need to configure it again. -->
You can install the released version of fxTWAPLS from [CRAN](https://cran.r-project.org/package=fxTWAPLS) with:
``` r
install.packages("fxTWAPLS")
```
And the development version from [GitHub](https://github.com/special-uor/fxTWAPLS/) with:
<!-- You can install the development version from [GitHub](https://github.com/) with: -->
``` r
install.packages("remotes")
remotes::install_github("special-uor/fxTWAPLS", "dev")
```
## Publications
- ___Latest:___ Liu, M., Shen, Y., González-Sampériz, P., Gil-Romera, G.,
ter Braak, C. J. F., Prentice, I. C., and Harrison, S. P.: Holocene climates
of the Iberian Peninsula: pollen-based reconstructions of changes in the
west-east gradient of temperature and moisture, Clim. Past Discuss.
[preprint], <https://doi.org/10.5194/cp-2021-174>, in review, 2021.-
[`fxTWAPLS v0.1.0`](https://github.com/special-uor/fxTWAPLS/releases/tag/v0.1.0/)
``` r
install.packages("remotes")
remotes::install_github("special-uor/[email protected]")
```
- Liu Mengmeng, Prentice Iain Colin, ter Braak Cajo J. F., Harrison Sandy P..
2020 An improved statistical approach for reconstructing past climates from biotic
assemblages. _Proc. R. Soc. A._ __476__: 20200346.
https://doi.org/10.1098/rspa.2020.0346 - [`fxTWAPLS v0.0.2`](https://github.com/special-uor/fxTWAPLS/releases/tag/v0.0.2/)
```r
install.packages("remotes")
remotes::install_github("special-uor/[email protected]")
```
<!-- ## Example -->
<!-- This is a basic example which shows you how to solve a common problem: -->
## Notes
The following functions can be executed in parallel:
- [`cv.pr.w`](https://special-uor.github.io/fxTWAPLS/reference/cv.pr.w.html)
- [`cv.w`](https://special-uor.github.io/fxTWAPLS/reference/cv.w.html)
- [`get_distance`](https://special-uor.github.io/fxTWAPLS/reference/get_distance.html)
- [`get_pseudo`](https://special-uor.github.io/fxTWAPLS/reference/get_pseudo.html)
- [`sse.sample`](https://special-uor.github.io/fxTWAPLS/reference/sse.sample.html)
To do so, include the `cpus` parameter. For example:
```{r, eval = FALSE}
cv_pr_tf_Tmin2 <- fxTWAPLS::cv.pr.w(
taxa,
modern_pollen$Tmin,
nPLS = 5,
fxTWAPLS::TWAPLS.w2,
fxTWAPLS::TWAPLS.predict.w,
pseudo_Tmin,
usefx = TRUE,
fx_method = "pspline",
bin = 0.02,
cpus = 2
)
```
Optionally, a progress bar can be displayed for long computations. Just "pipe" the
function call to `fxTWAPLS::pb()`.
```{r, eval = FALSE}
`%>%` <- magrittr::`%>%`
cv_pr_tf_Tmin2 <- fxTWAPLS::cv.pr.w(
taxa,
modern_pollen$Tmin,
nPLS = 5,
fxTWAPLS::TWAPLS.w2,
fxTWAPLS::TWAPLS.predict.w,
pseudo_Tmin,
usefx = TRUE,
fx_method = "pspline",
bin = 0.02,
cpus = 2
) %>%
fxTWAPLS::pb()
```
Alternatively, if you are not familiar with the "pipe" operator, you can run the
following code:
```{r, eval = FALSE}
cv_pr_tf_Tmin2 <- fxTWAPLS::pb(
fxTWAPLS::cv.pr.w(
taxa,
modern_pollen$Tmin,
nPLS = 5,
fxTWAPLS::TWAPLS.w2,
fxTWAPLS::TWAPLS.predict.w,
pseudo_Tmin,
usefx = TRUE,
fx_method = "pspline",
bin = 0.02,
cpus = 2
)
)
```
## Example
#### Training
```{r, eval = FALSE}
# Load modern data
modern_pollen <- read.csv("/path/to/modern_pollen.csv")
# Extract modern pollen taxa
taxaColMin <- which(colnames(modern_pollen) == "taxa0")
taxaColMax <- which(colnames(modern_pollen) == "taxaN")
taxa <- modern_pollen[, taxaColMin:taxaColMax]
# Set the binwidth to get the sampling frequency of the climate (fx),
# the fit is almost insenitive to binwidth when choosing pspline method.
bin <- 0.02
# Use fxTWAPLSv2 to train
fit_tf_Tmin2 <- fxTWAPLS::TWAPLS.w2(
taxa,
modern_pollen$Tmin,
nPLS = 5,
usefx = TRUE,
fx_method = "pspline",
bin = bin
)
```
#### Cross validation
```{r, eval = FALSE}
# Set CPUS to run in parallel
CPUS <- 6
# Import pipe operator to use with the progress bar
`%>%` <- magrittr::`%>%`
# Get the location information of each sample
point <- modern_pollen[, c("Long", "Lat")]
# Get the distance between each point
dist <- fxTWAPLS::get_distance(point, cpus = CPUS)
# Get the pseudo sites (which are both geographically close and climatically
# close to the test site) which should be removed in cross validation
pseudo_Tmin <- fxTWAPLS::get_pseudo(
dist,
modern_pollen$Tmin,
cpus = CPUS
)
# Leave-out cross validation
cv_pr_tf_Tmin2 <- fxTWAPLS::cv.pr.w(
taxa,
modern_pollen$Tmin,
nPLS = 5,
fxTWAPLS::TWAPLS.w2,
fxTWAPLS::TWAPLS.predict.w,
pseudo_Tmin,
usefx = TRUE,
fx_method = "pspline",
bin = bin,
cpus = CPUS,
test_mode = FALSE
) %>%
fxTWAPLS::pb()
# Random t test to the cross validation result
rand_pr_tf_Tmin2 <-
fxTWAPLS::rand.t.test.w(cv_pr_tf_Tmin2, n.perm = 999)
```
#### Reconstruction
```{r, eval = FALSE}
# Load fossil data
Holocene <- read.csv("/path/to/Holocene.csv")
# Extract fossil pollen taxa
taxaColMin <- which(colnames(Holocene) == "taxa0")
taxaColMax <- which(colnames(Holocene) == "taxaN")
core <- Holocene[, taxaColMin:taxaColMax]
# Choose nsig (the last significant number of components) based on the p-value
nsig <- 3
# Predict
fossil_tf_Tmin2 <- fxTWAPLS::TWAPLS.predict.w(fit_tf_Tmin2, core)
# Get the sample specific errors
sse_tf_Tmin2 <- fxTWAPLS::sse.sample(
modern_taxa = taxa,
modern_climate = modern_pollen$Tmin,
fossil_taxa = core,
trainfun = fxTWAPLS::TWAPLS.w2,
predictfun = fxTWAPLS::TWAPLS.predict.w,
nboot = nboot,
nPLS = 5,
nsig = nsig,
usefx = TRUE,
fx_method = "pspline",
bin = bin,
cpus = CPUS
) %>%
fxTWAPLS::pb()
# Output
recon_result <-
cbind.data.frame(
recon_Tmin = fossil_tf_Tmin2[["fit"]][, nsig],
sse_recon_Tmin = sse_tf_Tmin2
)
```