-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathREADME.Rmd
153 lines (104 loc) · 4.22 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
---
bibliography: /home/persican/Documents/library.bib
output:
github_document:
html_preview: true
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
fig.path = "inst/images/README-"
)
```
```{r setup, echo = FALSE}
library(ggplot2)
theme_set(theme_bw(base_size = 12, base_family = "Open Sans"))
```
## cdom [![Travis-CI Build Status](https://api.travis-ci.org/PMassicotte/cdom.svg?branch=master)](https://travis-ci.org/PMassicotte/cdom) [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/PMassicotte/cdom?branch=master&svg=true)](https://ci.appveyor.com/project/PMassicotte/cdom) [![Package-License](https://img.shields.io/badge/license-GPL%20%28%3E=%202%29-brightgreen.svg?style=flat)](http://www.gnu.org/licenses/gpl-2.0.html) [![CRAN](http://www.r-pkg.org/badges/version/cdom)](http://cran.rstudio.com/package=cdom) [![Downloads](http://cranlogs.r-pkg.org/badges/cdom?color=brightgreen)](http://www.r-pkg.org/pkg/cdom)
The **cdom** package implements various functions used to model and calculate metrics from absorption spectra of chromophotic dissolved organic matter (CDOM).
This package provides:
1. Simple wrappers to calculate common metrics found in the literature.
+ The **spectral curve** [@Loiselle2009].
+ The **slope ratio (Sr)** [@Helms2008].
+ The **spectral slope (S)** [@Jerlov1968; @Lundgren1976; @Bricaud1981].
2. The function to use the **Gaussian decomposition approach** proposed in Massicotte and Markager, (2015).
The package can be installed using the following command.
```{r install, eval = FALSE}
devtools::install_github("PMassicotte/cdom")
```
Please note that this is a developing version of the package for testing only. Please fill an issue when you find bugs.
All functions from the package start with the `cdom_` prefix.
```{r}
library(cdom)
ls("package:cdom")
```
# Examples
## The spectral slope (S)
The `cdom_fit_exponential()` function fits an exponential curve to CDOM data using the simple model proposed by @Jerlov1968, @Lundgren1976, @Bricaud1981.
```tex
a(\lambda) = a(\lambda0)e^{-S(\lambda - \lambda0)} + K
```
```{r exponential}
library(ggplot2)
library(cdom)
data("spectra")
fit <- cdom_exponential(wl = spectra$wavelength,
absorbance = spectra$spc3,
wl0 = 350,
startwl = 190,
endwl = 900)
coef(fit)
p <- plot(fit)
p
```
## The slope ratio (SR)
The `cdom_slope_ratio()` function calculates the slope ratio (S<sub>R</sub>) which is defined as: S<sub>275-295</sub>/S<sub>350-400</sub>. See @Helms2008 for detailed information.
```{r sr}
library(cdom)
data("spectra")
cdom_slope_ratio(spectra$wavelength, spectra$spc1)
```
## The spectral curve
The `cdom_spectral_curve()` function generates the spectral curve using the slope of the linear regression between the natural log absorption spectrum and wavelengths over a sliding window of 21 nm interval (default) at 1 nm resolution. See @Loiselle2009 for detailed information.
```{r spectral_curve}
library(cdom)
data("spectra")
res <- cdom_spectral_curve(wl = spectra$wavelength,
absorbance = spectra$spc10,
interval = 21,
r2threshold = 0.98) # Maybe to restrictive...
ggplot(res, aes(x = wl, y = s)) +
geom_point() +
geom_line() +
xlab("Wavelength (nm)") +
ylab(expression(paste("Spectral slope (", nm ^ {-1}, ")")))
```
## Using the pipe operator
```{r}
library(dplyr)
library(tidyr)
data(spectra)
spectra <- spectra %>%
gather(sample, absorption, starts_with("spc")) %>%
group_by(sample) %>%
nest() %>%
mutate(model = purrr::map(data, ~cdom_exponential(.$wavelength, .$absorption, wl0 = 350, startwl = 190, endwl = 900)))
#spectra %>% unnest(model %>% purrr::map(~.$data$.fitted))
```
# Data
A total 25 absorption spectra are provided in the package.
```{r data}
library(ggplot2)
library(tidyr)
data("spectra")
spectra <- gather(spectra, sample, absorption, -wavelength)
ggplot(spectra, aes(x = wavelength, y = absorption, group = sample)) +
geom_line(size = 0.1) +
xlab("Wavelength (nm)") +
ylab(expression(paste("Absorption (", m ^ {-1}, ")")))
```
# How to cite the package
```{r}
citation("cdom")
```
# References