Title: | Storing, Manipulating and Analysis Spectroscopy and Associated Data |
---|---|
Description: | Stores and eases the manipulation of spectra and associated data, with dedicated classes for spatial and soil-related data. |
Authors: | Pierre Roudier [aut, cre], Max Kuhn [ctb], Kristian Hovde Liland [ctb], Bjorn-Helge Mevik [ctb], Hadley Wickham [ctb], Raphael Viscarra Rossel [dtc] |
Maintainer: | Pierre Roudier <[email protected]> |
License: | GPL-3 |
Version: | 0.5-4 |
Built: | 2024-10-29 04:17:45 UTC |
Source: | https://github.com/pierreroudier/spectacles |
Stores and eases the manipulation of spectra and associated data.
Pierre Roudier
Aggregates spectral and data information of a Spectra
object using a
user-defined function
## S4 method for signature 'Spectra' aggregate_spectra(obj, fun = mean, ...) ## S4 method for signature 'SpectraDataFrame' aggregate_spectra(obj, fun = mean, id = NULL, ...)
## S4 method for signature 'Spectra' aggregate_spectra(obj, fun = mean, ...) ## S4 method for signature 'SpectraDataFrame' aggregate_spectra(obj, fun = mean, id = NULL, ...)
obj |
see below |
fun |
see below |
... |
see below |
id |
see below |
For SpectraDataFrame
objects, associated data is also aggregated using the function provided by the fun
option. Additionally, the method for SpectraDataFrame
has an id
option that allows to specify an attribute which will be used to split the object, apply sequentially the fun
function, and recombine the results in an unique object.
An object of the same class as obj
x=Spectra
aggregate_spectra(obj, fun=mean, ...)
obj |
A Spectra object |
|
fun |
An aggregation function | |
... |
Expressions
evaluated in the context of fun |
|
x=SpectraDataFrame
aggregate_spectra(obj, fun=mean, id=NULL, ...)
obj |
A SpectraDataFrame object |
|
fun |
An aggregation function | |
id |
Attribute(s) to split the object (character vector) | |
... |
Expressions evaluated in the context of fun |
|
Pierre Roudier [email protected]
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Aggregation on the whole collection m <- aggregate_spectra(australia, fun = mean) plot(m) # Aggregation factor-wise # Generate some kind of factor australia$fact <- sample( LETTERS[1:3], size = nrow(australia), replace = TRUE ) m <- aggregate_spectra(australia, fun = mean, id = 'fact') plot(m)
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Aggregation on the whole collection m <- aggregate_spectra(australia, fun = mean) plot(m) # Aggregation factor-wise # Generate some kind of factor australia$fact <- sample( LETTERS[1:3], size = nrow(australia), replace = TRUE ) m <- aggregate_spectra(australia, fun = mean, id = 'fact') plot(m)
Aggregates spectral and data information of a Spectra
object using a
user-defined function.
Apply a function and update the spectra of a Spectra
object. This
function is particularly interesting for pre-processing, e.g to derive the
spectra, or apply pre-processing functions such as snv
.
apply_spectra(obj, fun, ...)
apply_spectra(obj, fun, ...)
obj |
an object inheriting from class |
fun |
an aggregation function |
... |
expressions evaluated in the context of |
The philosophy of this function is to let the user free to use any function
to pre-process a spectra collection, using either functions from the stats
package, functions from other packages such as signal
, or personal
functions.
An object of the same class as obj
Pierre Roudier [email protected]
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Second derivative r <- apply_spectra(australia, diff, 2) plot(r) # Smoothing kernel k <- kernel("daniell", 20) # define a kernel r <- apply_spectra(australia, kernapply, k) plot(r) ## Not run: # Savitzky-Golay filter (from the signal package) library(signal) r <- apply_spectra(australia, sgolayfilt, n = 33, p = 4) plot(r) ## End(Not run)
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Second derivative r <- apply_spectra(australia, diff, 2) plot(r) # Smoothing kernel k <- kernel("daniell", 20) # define a kernel r <- apply_spectra(australia, kernapply, k) plot(r) ## Not run: # Savitzky-Golay filter (from the signal package) library(signal) r <- apply_spectra(australia, sgolayfilt, n = 33, p = 4) plot(r) ## End(Not run)
The australia
data set gathers 100 soil spectra, along side with their organic
carbon, pH and clay content values. This data set has been collected by
CSIRO.The oz
dataset is the first 5 samples of australia
, and is here to keep examples fast to run.
The data.frame
contains the following columns:
sr_noa unique identifier for each spectrum
carbonsoil organic carbon values
phsoil pH values
claysoil clay values
X350, X351, ...,X2499, X2500reflectance in wavelengths 350 to 2500nm
Data kindly contributed by Raphael Viscarra Rossel. To reduce the size of the package, the original dataset has been reduced to 100 spectra using the Kennard-Stone algorithm.
Viscarra Rossel, R. and Behrens, T. 2010.
data(australia) big.head(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 data(oz) big.head(oz) spectra(oz) <- sr_no ~ ... ~ 350:2500
data(australia) big.head(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 data(oz) big.head(oz) spectra(oz) <- sr_no ~ ... ~ 350:2500
Estimates baselines for the spectra in the obj
object, using the
algorithm named in 'method'.
## S4 method for signature 'Spectra' base_line(object, ...)
## S4 method for signature 'Spectra' base_line(object, ...)
object |
an object inheriting from class |
... |
additional arguments to be passed to the |
The baseline package implements various algorithms for the baseline correction. The following methods are available:
'als': Baseline correction by 2nd derivative constrained weighted regression
'fillPeaks': An iterative algorithm using suppression of baseline by means in local windows
'irls' (default): An algorithm with primary smoothing and repeated baseline suppressions and regressions with 2nd derivative constraint
'lowpass': An algorithm for removing baselines based on Fast Fourier Transform filtering
'medianWindow': An implementation and extention of Mark S. Friedrichs' model-free algorithm
'modpolyfit': An implementation of Chad A. Lieber and Anita Mahadevan-Jansen's algorithm for polynomial fiting
'peakDetection': A translation from Kevin R. Coombes et al.'s MATLAB code for detecting peaks and removing baselines
'rfbaseline': Wrapper for Andreas F. Ruckstuhl, Matthew P. Jacobson, Robert W. Field, James A. Dodd's algorithm based on LOWESS and weighted regression
'rollingBall': Ideas from Rolling Ball algorithm for X-ray spectra by M.A.Kneen and H.J. Annegarn. Variable window width has been left out
See baseline package documentation for more information and references.
Additionally, the baseline package provides a nice GUI that helps choosing
the good baseline method and the good parametrisation. This GUI can be used
with the inspectr
package. This is demonstrate in the Examples
section.
An object of the same class as obj
with the continuum removed
from its spectra.
Interface to the baseline package by Pierre Roudier [email protected], baseline package authored by Kristian Hovde Liland and Bjorn-Helge Mevik
Kristian Hovde Liland and Bjorn-Helge Mevik (2011). baseline: Baseline Correction of Spectra. R package version 1.0-1. http://CRAN.R-project.org/package=baseline
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Subsample for demo purposes australia <- australia[1:10,] # Correction using the default method (irls) bl <- base_line(australia) plot(bl) # Specifying another method for baseline calculation bl2 <- base_line(australia, method = "modpolyfit") plot(bl2) # Using the baseline package independently # (useful to plot the corrections) ## Not run: library(baseline) bl3 <- baseline(spectra(australia), method = 'irls') class(bl3) # this is a baseline object plot(bl3) # Affecting the baseline-corrected spectra back # to the SpectraDataFrame object spectra(australia) <- getCorrected(bl3) plot(australia) # Using the baselineGUI with inspectr baselineGUI(spectra(australia)) ## When happy with a configuration, clik "Apply to all" and ## save the results under a name, e.g. "corrected.spectra" spectra(australia) <- getCorrected(corrected.spectra) plot(australia) ## End(Not run)
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Subsample for demo purposes australia <- australia[1:10,] # Correction using the default method (irls) bl <- base_line(australia) plot(bl) # Specifying another method for baseline calculation bl2 <- base_line(australia, method = "modpolyfit") plot(bl2) # Using the baseline package independently # (useful to plot the corrections) ## Not run: library(baseline) bl3 <- baseline(spectra(australia), method = 'irls') class(bl3) # this is a baseline object plot(bl3) # Affecting the baseline-corrected spectra back # to the SpectraDataFrame object spectra(australia) <- getCorrected(bl3) plot(australia) # Using the baselineGUI with inspectr baselineGUI(spectra(australia)) ## When happy with a configuration, clik "Apply to all" and ## save the results under a name, e.g. "corrected.spectra" spectra(australia) <- getCorrected(corrected.spectra) plot(australia) ## End(Not run)
Return the First or Last Part of an Object
big.head(x, n = 5, l = 5, r = 5)
big.head(x, n = 5, l = 5, r = 5)
x |
a |
n |
a single, positive integer, number of rows for the object to return |
l |
a single, positive integer, the number of columns to include on the left |
r |
a single, positive integer, the number of columns to include on the right |
Returns the first or last rows of a data frame like head() and tail(), but also only returns the first and last columns. This has been implemented to check big data frames.
An object (usually) like 'x' but generally smaller.
Pierre Roudier [email protected]
big.head(mtcars) big.tail(mtcars) big.tail(mtcars, 10) big.head(mtcars, 10, 2, 4) big.head(mtcars, , , 1) data(australia) big.head(australia)
big.head(mtcars) big.tail(mtcars) big.tail(mtcars, 10) big.head(mtcars, 10, 2, 4) big.head(mtcars, , , 1) data(australia) big.head(australia)
Operates a continuum removal on a vector.
continuum_removal(x, wl = as.numeric(names(x)), upper = TRUE)
continuum_removal(x, wl = as.numeric(names(x)), upper = TRUE)
x |
a numeric vector |
wl |
the wavelengths of the spectra |
upper |
if TRUE, removes the upper convex hull from the spectra, if FALSE, takes the lower convex hull |
This operation is commonly done to normalize reflectance spectra and allow comparison of individual absorption features from a common baseline. The removal is based on the upper convex hull of the spectra.
This function is working on vectors. It may applied on matrix or data.frames
using the apply
function, or on Spectra*
objects using the
apply_spectra
function.
A numeric vector with its continuum removed.
Pierre Roudier [email protected], based on code from Raphael Viscarra-Rossel.
Clark, R.N., and Roush, T.L. 1984. Reflectance spectroscopy: Quantitative analysis techniques for remote sensing applications. Journal of Geophysical Research 89, 6329–6340.
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 s <- apply_spectra(australia, continuum_removal) plot(s) s <- apply_spectra(australia, continuum_removal, upper = FALSE) plot(s)
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 s <- apply_spectra(australia, continuum_removal) plot(s) s <- apply_spectra(australia, continuum_removal, upper = FALSE) plot(s)
Spectra
objectThis methods allows to either select or remove a specific range of
wavelengths from a Spectra
object.
## S4 method for signature 'Spectra' cut(x, ..., wl)
## S4 method for signature 'Spectra' cut(x, ..., wl)
x |
an object inheriting from class |
... |
ignored |
wl |
a vector of the wavelengths to either select or remove from
|
The wavelengths are extracted if wl > 0
, or removed if wl < 0
.
You can't mix negative and positive index in wl
.
An object of the same class as x
.
Pierre Roudier [email protected]
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Extracting a specific wavelengths range s <- cut(australia, wl = 450:550) plot(s) s <- cut(australia, wl = c(450:550, 1850:2050)) plot(s) # Removing a specific wavelengths range s <- cut(australia, wl = -1*450:550) plot(s) s <- cut(australia, wl = -1*c(450:550, 1850:2050)) plot(s)
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Extracting a specific wavelengths range s <- cut(australia, wl = 450:550) plot(s) s <- cut(australia, wl = c(450:550, 1850:2050)) plot(s) # Removing a specific wavelengths range s <- cut(australia, wl = -1*450:550) plot(s) s <- cut(australia, wl = -1*c(450:550, 1850:2050)) plot(s)
Retrieves the wavelengths units and the spectral resolution from
Spectra*
objects.
## S3 method for class 'Spectra' length(x) ## S4 method for signature 'Spectra' nrow(x) ## S4 method for signature 'Spectra' ncol(x) ## S3 method for class 'Spectra' dim(x)
## S3 method for class 'Spectra' length(x) ## S4 method for signature 'Spectra' nrow(x) ## S4 method for signature 'Spectra' ncol(x) ## S3 method for class 'Spectra' dim(x)
x |
For |
* Methods for Spectra
objects
nrow
returns the number of individuals in the collection
length
returns the number of wavelengths in the collection
ncol
returns NULL dim
returns a vector containing (1) the
number of individuals and (2) in the number of wavelengths in the collection
* Methods for Spectra
objects
nrow
returns the number of individuals in the collection
length
returns the number of wavelengths in the collection
ncol
returns the number of attributes in the collection dim
returns a vector containing (1) the number of individuals, (2) in the number
of wavelengths, and (3) the number of attributes in the collection
nrow
, ncol
, nwl
, and length
, return an
integer
, dim
returns a vector of length 2 or 3 (see Details section).
Pierre Roudier [email protected]
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 nrow(australia) ncol(australia) length(australia) dim(australia)
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 nrow(australia) ncol(australia) length(australia) dim(australia)
These methods emulates classic base methods '[', '[[' and '$' to extract or replace parts of Spectra* objects.
\S4method{[}{Spectra}(x,i,j,...,drop=FALSE) \S4method{[[}{Spectra}(x,i,j,...) \S4method{$}{SpectraDataFrame}(x,name) \S4method{$}{Spectra}(x,name) <- value \S4method{[[}{Spectra}(x,i,j,...) <- value
\S4method{[}{Spectra}(x,i,j,...,drop=FALSE) \S4method{[[}{Spectra}(x,i,j,...) \S4method{$}{SpectraDataFrame}(x,name) \S4method{$}{Spectra}(x,name) <- value \S4method{[[}{Spectra}(x,i,j,...) <- value
x |
an object of class |
i , j , ...
|
indices specifying elements to extract or replace |
drop |
currently ignored |
name |
A literal character string or a name (possibly backtick quoted) |
value |
typically an array-like R object of a similar class as x |
These methods either return an object of the same class as x
,
or can promote a Spectra
object to a SpectraDataFrame
object
by adding data ("[[<-" and "$<-" methods).
x=Spectra
x[i, j, ..., drop = FALSE]
x$name <- value
x[[name]] <- value
x |
A Spectra object |
|
i
|
Row index of the selected individuals | |
j |
Selected wavelengths | |
name |
A literal character string or a name | |
... |
Ignored | |
drop |
Ignored | |
x=SpectraDataFrame
x[i, j, k, ..., drop = FALSE]
x[[name]]
x[[name]] <- value
x$name
x$name <- value
x |
A SpectraDataFrame object |
|
i |
Row index of the selected individuals | |
j |
Selected wavelengths | |
k |
Selected columns in the @data slot | |
name |
A literal character string or a name | |
... |
Ignored | |
drop |
Ignored | |
Pierre Roudier [email protected]
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Getting features information from SpectraDataFrame australia$carbon australia[['carbon']] # Creating new features australia$foo <- runif(nrow(australia)) australia[['bar']] <- runif(nrow(australia)) # Replacing values australia$foo <- sample( LETTERS[1:5], size = nrow(australia), replace = TRUE ) australia[['bar']] <- sample( c(TRUE, FALSE), size = nrow(australia), replace = TRUE ) # Promote Spectra to SpectraDataFrame s <- as(australia, 'Spectra') class(s) s$foo <- runif(nrow(s))
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Getting features information from SpectraDataFrame australia$carbon australia[['carbon']] # Creating new features australia$foo <- runif(nrow(australia)) australia[['bar']] <- runif(nrow(australia)) # Replacing values australia$foo <- sample( LETTERS[1:5], size = nrow(australia), replace = TRUE ) australia[['bar']] <- sample( c(TRUE, FALSE), size = nrow(australia), replace = TRUE ) # Promote Spectra to SpectraDataFrame s <- as(australia, 'Spectra') class(s) s$foo <- runif(nrow(s))
Either retrieves the attributes values from the data slot of a
SpectraDataFrame object, or upgrades a Spectra object to a SpectraDataFrame
object by initialising its data slot by a suitable "data.frame"
object.
## S4 method for signature 'SpectraDataFrame' features(object,exclude_id) ## S4 replacement method for signature 'Spectra' features(object,safe,exclude_id,key,append) <- value
## S4 method for signature 'SpectraDataFrame' features(object,exclude_id) ## S4 replacement method for signature 'Spectra' features(object,safe,exclude_id,key,append) <- value
object |
a |
exclude_id |
see below |
value |
see below |
safe |
see below |
key |
see below |
append |
see below |
The features
methods return a data.frame
object, while
the "features<-"
methods return a SpectraDataFrame
object.
x=Spectra
features(object, safe=TRUE, key=NULL, exclude_id=TRUE) <- value
object |
A Spectra object |
|
safe |
Logical. If TRUE, data is being added to the object using a
SQL join (using a key field given by the key option), otherwise it is
assumed the order of the rows is consitent with the order of the rows in
object |
|
key |
Character, name of the column of the
data.frame storing the ids for the SQL join. Ignored if safe is
FALSE . |
|
exclude_id |
Logical, if TRUE , ids
used for the SQL join are removed from the data slot after the join. |
|
x=SpectraDataFrame
features(obj, exclude_id=TRUE)
features(obj, safe=TRUE, key=NULL, exclude_id=TRUE, append=TRUE) <-
value
object |
A SpectraDataFrame object |
|
safe |
Logical. If TRUE, data is being added to the object
using a SQL join (using a key field given by the key option),
otherwise it is assumed the order of the rows is consitent with the order of
the rows in object |
|
key |
Character, name of the
column of the data.frame storing the ids for the SQL join. Ignored if
safe is FALSE . |
|
exclude_id |
Logical. For
the features method, if TRUE , the spectra ids are added to the
data.frame that is returned. For the "features<-" method, If
TRUE , ids used for the SQL join are removed from the data slot after
the join. |
|
append |
Logical, if TRUE , the data is
appended to any existing data. if FALSE, the data provided is erasing any
existing data. |
|
Pierre Roudier [email protected]
spectra
, wl
,
SpectraDataFrame-class
# Loading example data data(oz) spectra(oz) <- sr_no ~ ... ~ 350:2500 # Printing available data features(oz) # Promoting a Spectra to a SpectraDataFrame object s <- as(oz, "Spectra") # Generating dummy data d <- data.frame( id = ids(oz), foo = runif(nrow(oz)), bar = sample(LETTERS[1:5], size = nrow(oz), replace = TRUE) ) head(d) # Affecting data to Spectra object features(s) <- d features(s) # Adding data to an existing SpectraDataFrame object features(oz) <- d features(oz)
# Loading example data data(oz) spectra(oz) <- sr_no ~ ... ~ 350:2500 # Printing available data features(oz) # Promoting a Spectra to a SpectraDataFrame object s <- as(oz, "Spectra") # Generating dummy data d <- data.frame( id = ids(oz), foo = runif(nrow(oz)), bar = sample(LETTERS[1:5], size = nrow(oz), replace = TRUE) ) head(d) # Affecting data to Spectra object features(s) <- d features(s) # Adding data to an existing SpectraDataFrame object features(oz) <- d features(oz)
Fill missing wavelengths of a Spectra* object with a given value. This is mostly usefull to include NA values in the spectra in order to show missing bits in plots.
## S4 method for signature 'Spectra' fill_spectra(obj, ref = NULL, fill = NA, ...)
## S4 method for signature 'Spectra' fill_spectra(obj, ref = NULL, fill = NA, ...)
obj |
an object inheriting from class |
ref |
a numeric vector, giving the reference wavelengths (ie the entire range of wavelengths expected to be in the spectra before some waveleng5ths have been cut out). If NULL, the function is trying to guess it. |
fill |
values to fill gaps in the data with |
... |
ignored |
At this stage removing gaps does not work well with irreguarly spaced wavelengths. Results might be odd for binned spectra.
An object of the same class as obj
Pierre Roudier [email protected]
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Cut wavelengths out of the collection oz <- cut(australia, wl=-1*c(355:400, 2480:2499)) big.head(spectra(oz), , 7) # Giving the wavelengths at which I want data oz_filled <- fill_spectra(oz, ref = 350:2500, fill = NA) big.head(spectra(oz_filled), , 7) plot(oz_filled) # Trying to guess ref values oz_filled <- fill_spectra(oz, fill = -999) big.head(spectra(oz_filled), , 7) plot(oz_filled)
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Cut wavelengths out of the collection oz <- cut(australia, wl=-1*c(355:400, 2480:2499)) big.head(spectra(oz), , 7) # Giving the wavelengths at which I want data oz_filled <- fill_spectra(oz, ref = 350:2500, fill = NA) big.head(spectra(oz_filled), , 7) plot(oz_filled) # Trying to guess ref values oz_filled <- fill_spectra(oz, fill = -999) big.head(spectra(oz_filled), , 7) plot(oz_filled)
Spectra*
object.Either retrieves the wavelengths from a Spectra*
object, or creates a
Spectra*
object from a "data.frame"
object by setting some of
its columns as the wavelengths. The "ids<-"
method for
SpectraDataFrame
objects allows to use a formula interface to use a
column in its data
slot as the object IDs (see the last example provided
in the Examples section).
ids(object, ...) ids(object) <- value ## S4 replacement method for signature 'Spectra' ids(object) <- value ## S4 replacement method for signature 'SpectraDataFrame' ids(object) <- value
ids(object, ...) ids(object) <- value ## S4 replacement method for signature 'Spectra' ids(object) <- value ## S4 replacement method for signature 'SpectraDataFrame' ids(object) <- value
object |
an object of class |
... |
|
value |
character vector for new IDs |
The ids
methods return a vector if as.vector
is TRUE,
a data.frame
otherwise. The "ids<-"
method return a
SpectraDataFrame
object (or a Spectra
object if the column in
the data slot that has been used to initiate the IDs was the only
attribute).
ids(object, ..., as.vector = TRUE)
ids(object) <- value
Pierre Roudier [email protected]
# Loading example data data(oz) spectra(oz) <- sr_no ~ ... ~ 350:2500 # Retrieving ids ids(oz) # Setting ids using a vector of values ids(oz) <- seq_len(nrow(oz)) ids(oz) # Setting ids using an attribute oz$new_id <- seq_len(nrow(oz)) + 1000 ids(oz) <- ~ new_id ids(oz)
# Loading example data data(oz) spectra(oz) <- sr_no ~ ... ~ 350:2500 # Retrieving ids ids(oz) # Setting ids using a vector of values ids(oz) <- seq_len(nrow(oz)) ids(oz) # Setting ids using an attribute oz$new_id <- seq_len(nrow(oz)) + 1000 ids(oz) <- ~ new_id ids(oz)
An implemnentation of the Kennard-Stone algorithm for calibration set selection.
kenstone(x, size, ...)
kenstone(x, size, ...)
x |
a |
size |
a positive number, the number of items to choose from |
... |
ignored |
A vector of length size giving the indices of the selected individuals in x.
Pierre Roudier [email protected]
Kennard, L.A. Stone, Technometrics 11 (1969) 137.
Loads the australia dataset as a SpectraDataFrame
load_oz(n = NULL)
load_oz(n = NULL)
n |
the number of spectra to return. By default, it returns all 100 spectra in the dataset. |
a SpectraDataFrame
Pierre Roudier
oz <- load_oz()
oz <- load_oz()
This function is very useful when wanting to plot spectra using the lattice or ggplot2 packages
melt_spectra(obj, attr=NULL, ...) ## S4 method for signature 'SpectraDataFrame' melt_spectra(obj, attr = NULL, ...)
melt_spectra(obj, attr=NULL, ...) ## S4 method for signature 'SpectraDataFrame' melt_spectra(obj, attr = NULL, ...)
obj |
an object of class |
attr |
vector of id variables against which the spectra will be melted (see |
... |
further arguments passed to or from other methods |
x=Spectra
melt_spectra(obj, ...)
obj |
A Spectra object |
|
... |
Ignored | |
x=SpectraDataFrame
melt_spectra(obj, attr=NULL, ...)
obj |
A SpectraDataFrame object |
|
attr |
Character, the name of an attribute in the object data to split the spectra against. | |
... |
Ignored | |
Pierre Roudier [email protected]
Hadley Wickham (2011). The Split-Apply-Combine Strategy for Data Analysis. Journal of Statistical Software, 40(1), 1-29. URL http://www.jstatsoft.org/v40/i01/.
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Simple melt r <- melt_spectra(australia) head(r) ## Not run: # Melt against some factor (or continuous data), and plot # using ggplot2 # Create some factor australia$fact <- sample( LETTERS[1:3], size = nrow(australia), replace = TRUE ) r <- melt_spectra(australia, attr = 'fact') # Create plot library(ggplot2) p <- ggplot(r) + geom_line(aes(x=wl, y=nir, group=id, colour=fact)) + theme_bw() print(p) ## End(Not run)
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Simple melt r <- melt_spectra(australia) head(r) ## Not run: # Melt against some factor (or continuous data), and plot # using ggplot2 # Create some factor australia$fact <- sample( LETTERS[1:3], size = nrow(australia), replace = TRUE ) r <- melt_spectra(australia, attr = 'fact') # Create plot library(ggplot2) p <- ggplot(r) + geom_line(aes(x=wl, y=nir, group=id, colour=fact)) + theme_bw() print(p) ## End(Not run)
This function is very similar to transform
but it executes the
transformations iteratively so that later transformations can use the
columns created by earlier transformations. Like transform, unnamed
components are silently dropped.
Either the spectra, and/or the attributes (if the .data
inherits from
the SpectraDataFrame
class) can be affected:
To
affect the spectra, one should use the nir
placeholder, eg nir
= log(1/nir)
To affect the attributes of the object, the definitions
of new columns are simply given using attributes names, newAttribute =
1/sqrt(attribute)
Both spectra and attrbutes can be transformed in one command.
## S4 method for signature 'Spectra' mutate(.data, ...)
## S4 method for signature 'Spectra' mutate(.data, ...)
.data |
an object inheriting from the |
... |
named parameters giving definitions of new columns |
Pierre Roudier [email protected], from code from Hadley Wickham
Hadley Wickham (2011). The Split-Apply-Combine Strategy for Data Analysis. Journal of Statistical Software, 40(1), 1-29. URL http://www.jstatsoft.org/v40/i01/.
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Modifying spectra m <- mutate(australia, nir = log1p(1/nir)) plot(m) # Modifying and creating attributes m <- mutate( australia, sqrt_carbon = sqrt(carbon), foo = clay + ph, nir = log1p(1/nir) ) plot(m)
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Modifying spectra m <- mutate(australia, nir = log1p(1/nir)) plot(m) # Modifying and creating attributes m <- mutate( australia, sqrt_carbon = sqrt(carbon), foo = clay + ph, nir = log1p(1/nir) ) plot(m)
Creates an offset plot of a collection of Spectra
plot_offset(x, offset = 1)
plot_offset(x, offset = 1)
x |
an object of class |
offset |
Offset between spectra |
Pierre Roudier
oz <- load_oz(3) plot_offset(oz) plot_offset(oz, 0.3) plot_offset(oz, 2)
oz <- load_oz(3) plot_offset(oz) plot_offset(oz, 0.3) plot_offset(oz, 2)
Creates a stacked plot of a collection of Spectra
plot_stack(x)
plot_stack(x)
x |
an object of class |
Pierre Roudier
oz <- load_oz(3) plot_stack(oz)
oz <- load_oz(3) plot_stack(oz)
Creates a summary plot of a collection of Spectra
plot_summary(x, fun = mean, se = TRUE, ...)
plot_summary(x, fun = mean, se = TRUE, ...)
x |
an object of class |
fun |
an aggregation function |
se |
if TRUE, plots the standard deviation around the summary spectra
(computed by function as given by |
... |
additional parameters, currently ignored |
Pierre Roudier
oz <- load_oz() plot_summary(oz)
oz <- load_oz() plot_summary(oz)
The philosophy of this plotting routine is to provide a "quick'n'dirty" way
to plot your spectra collection. For advanced visualisations, the use of
melt_spectra
alongside with ggplot2 or lattice is encouraged.
## S3 method for class 'Spectra' plot(x,gg,gaps,attr,...) ## S3 method for class 'Spectra' plot(x, gg = FALSE, gaps = TRUE, attr = NULL, ...)
## S3 method for class 'Spectra' plot(x,gg,gaps,attr,...) ## S3 method for class 'Spectra' plot(x, gg = FALSE, gaps = TRUE, attr = NULL, ...)
x |
an object of class |
gg |
if TRUE, uses the |
gaps |
if TRUE, gaps in the spectra are not plotted |
attr |
attribute against which lines are coloured (only for |
... |
additional parameters passed to |
Pierre Roudier [email protected]
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Default plotting method plot(australia[1:5,]) # Default plot using ggplot2 plot(australia[1:5,], gg = TRUE) ## Not run: # Managing gaps in the spectra s <- cut(australia, wl =c(-1*450:500, -1*1800:2050)) plot(s, gaps = TRUE) plot(s, gaps = FALSE) # passing various options to matplot plot( australia, lty = 1:5, col = 'blue', xlab = 'foo', ylab = 'bar', ylim = c(0.4,0.6), main = 'my plot' ) # Using colour ramps plot( australia, lty = 1, col = rainbow(10), main = "It is possible to create really ugly visualisations" ) # Example using colours given by ColorBrewer (http://colorbrewer2.org/) library(RColorBrewer) plot(australia, lty = 1, col = brewer.pal(n = 8, name = "Set2")) # Using an attribute to group spectra # Generate some kind of factor australia$fact <- sample( LETTERS[1:3], size = nrow(australia), replace = TRUE ) s <- aggregate_spectra(australia, fun = mean, id = 'fact') plot(s, gg = TRUE, attr = 'fact') ## End(Not run)
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Default plotting method plot(australia[1:5,]) # Default plot using ggplot2 plot(australia[1:5,], gg = TRUE) ## Not run: # Managing gaps in the spectra s <- cut(australia, wl =c(-1*450:500, -1*1800:2050)) plot(s, gaps = TRUE) plot(s, gaps = FALSE) # passing various options to matplot plot( australia, lty = 1:5, col = 'blue', xlab = 'foo', ylab = 'bar', ylim = c(0.4,0.6), main = 'my plot' ) # Using colour ramps plot( australia, lty = 1, col = rainbow(10), main = "It is possible to create really ugly visualisations" ) # Example using colours given by ColorBrewer (http://colorbrewer2.org/) library(RColorBrewer) plot(australia, lty = 1, col = brewer.pal(n = 8, name = "Set2")) # Using an attribute to group spectra # Generate some kind of factor australia$fact <- sample( LETTERS[1:3], size = nrow(australia), replace = TRUE ) s <- aggregate_spectra(australia, fun = mean, id = 'fact') plot(s, gg = TRUE, attr = 'fact') ## End(Not run)
Given two numeric vectors of data, the root mean squared error, the R-squared, the bias, the RPD, the RPIQ, the CCC and the standard error are calculated. For two factors, the overall agreement rate and Kappa are determined.
postResampleSpectro(pred, obs) spectroSummary(data, lev = NULL, model = NULL)
postResampleSpectro(pred, obs) spectroSummary(data, lev = NULL, model = NULL)
data |
a data frame or matrix with columns obs and pred for the observed and predicted outcomes |
lev |
a character vector of factors levels for the response. In regression cases, this would be NULL. |
model |
a character string for the model name |
pred |
A vector of numeric data |
obs |
A vector of numeric data |
This function extends postResample
in the caret
package.
Pierre Roudier, adapted from code from Max Kuhn
predicted <- matrix(rnorm(50), ncol = 5) observed <- rnorm(10) apply(predicted, 2, postResampleSpectro, obs = observed)
predicted <- matrix(rnorm(50), ncol = 5) observed <- rnorm(10) apply(predicted, 2, postResampleSpectro, obs = observed)
Spectra
objects togetherThis method stacks two or more Spectra*
objects together.
## S3 method for class 'Spectra' rbind(..., create_new_ids = FALSE, new_ids = NULL) ## S3 method for class 'SpectraDataFrame' rbind(..., create_new_ids = FALSE, new_ids = NULL)
## S3 method for class 'Spectra' rbind(..., create_new_ids = FALSE, new_ids = NULL) ## S3 method for class 'SpectraDataFrame' rbind(..., create_new_ids = FALSE, new_ids = NULL)
... |
The |
create_new_ids |
allows creation of new ids if the ids of the
|
new_ids |
vector of new ids to be given to the new object |
a Spectra*
object.
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 s <- rbind(australia, australia, create_new_ids = TRUE) l <- separate(australia, calibration = 0.6) s <- rbind(l$validation, l$calibration)
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 s <- rbind(australia, australia, create_new_ids = TRUE) l <- separate(australia, calibration = 0.6) s <- rbind(l$validation, l$calibration)
Returns the spectral resolution of an object
## S4 method for signature 'numeric' res(x) ## S4 method for signature 'integer' res(x) ## S4 method for signature 'Spectra' res(x)
## S4 method for signature 'numeric' res(x) ## S4 method for signature 'integer' res(x) ## S4 method for signature 'Spectra' res(x)
x |
object an object inheriting from |
a vector
Pierre Roudier [email protected]
Spectra*
object into a calibration and a validation set.Separates a Spectra*
object into a calibration and a validation set.
## S4 method for signature 'Spectra' separate(obj, calibration)
## S4 method for signature 'Spectra' separate(obj, calibration)
obj |
an object inheriting from class |
calibration |
The fraction of the dataset to be put in the calibration set |
An list with two SpectraDataFrame
objects, one for the
calibration, and the other for the validation.
Pierre Roudier [email protected]
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 l <- separate(australia, calibration=0.7) # The result is a list of two Spectra* objects str(l) lapply(l, nrow)
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 l <- separate(australia, calibration=0.7) # The result is a list of two Spectra* objects str(l) lapply(l, nrow)
Standard and Robust Normal Variate transformations are often used in chemometrics to normalise a spectra collection and remove the baseline effect.
The Standard Normal Variate transformation (SNV, Barnes et al., 1989) is a common method to reduce within-class variance.
The Robust Normal Variate transformation (RNV, Guo et al., 1999) is a modification of the SNV to make it more robust to closure problems.
These function are to be used in conjonction with
apply_spectra
.
snv(x) rnv(x, r)
snv(x) rnv(x, r)
x |
a vector of numeric values |
r |
the percentile to use in the RNV computation |
A vector of numeric values
Pierre Roudier [email protected]
Barnes, R.J., Dhanoa, M.S., Lister, S.J. 1989. Standard normal variate transformation and detrending of near-infra-red diffuse reflectance spectra. Applied Spectroscopy 43, 772–777.
Guo, Q., Wu, W., Massar, D.L. 1999. The robust normal variate transform for pattern recognition with near-infrared data. Analytica Chimica Acta 382:1–2, 87–103.
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Standard Normal Variate transform s <- apply_spectra(australia[1:10,], snv) plot(s) # The scale function in the base package is actually doing # the same thing! s <- apply_spectra(australia[1:10,], scale, center = TRUE, scale = TRUE) plot(s) # Robust Normal Variate transform s <- apply_spectra(australia[1:10,], rnv, r = 0.25) plot(s)
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Standard Normal Variate transform s <- apply_spectra(australia[1:10,], snv) plot(s) # The scale function in the base package is actually doing # the same thing! s <- apply_spectra(australia[1:10,], scale, center = TRUE, scale = TRUE) plot(s) # Robust Normal Variate transform s <- apply_spectra(australia[1:10,], rnv, r = 0.25) plot(s)
Constructor for the Spectra class. Creates a Spectra object from scratch.
Spectra(wl = numeric(), nir = matrix(), id = as.character(NA), units = "nm")
Spectra(wl = numeric(), nir = matrix(), id = as.character(NA), units = "nm")
wl |
a numeric vector giving the wavelengths at with the spectra have been measured |
nir |
a |
id |
a vector giving the unique id of each sample in the collection |
units |
a character giving the unit in which the wavelengths values are expressed |
a new "Spectra"
object
Pierre Roudier [email protected]
spectra
, wl
,
Spectra-class
, SpectraDataFrame
wls <- 350:2500 id <- c("A", "B") nir <- matrix(runif(2*length(wls)), nrow = 2) s <- Spectra(wl = wls, nir = nir, id = id, units = "nm")
wls <- 350:2500 id <- c("A", "B") nir <- matrix(runif(2*length(wls)), nrow = 2) s <- Spectra(wl = wls, nir = nir, id = id, units = "nm")
The spectacles package provides the user with S4 classes that have been developped to store and manipulate spectroscopy data.
The Spectra
class is storing the spectra matrix, along with the
wavelengths at which those have been measured, the units in whioch those
wavelengths are expressed, and a unique id for each sample in the
collection.
The SpectraDataFrame
class is extending the Spectra
class by
giving the opportunity to store attribute data along with the spectra - this
is mostly the case when we want to predict physical or chemical properties
from the spectra set.
The SpatialSpectra
and SpatialSpectraDataFrame
classes are
extending the Spectra
and SpectraDataFrame
classes using the
SpatialPoints
class from package sp. This allows to store spatial
information on the dataset: coordinates, coordinate reference system,
bounding box, etc.
Common generic methods implemented for these classes include:
summary
, show
, nrow
, length
, plot
,
[
, [[
, $
.
SpatialPoints
methods from the sp package can be applied to
SpatialSpectra
and SpatialSpectraDataFrame
objects are they
inherit from this class.
wl
object of class "numeric
"; the wavelengths at which the spectra has been measured
nir
object of class "matrix
"; the spectra, with as many columns as wavelengths, and as many rows as samples
id
object of class "data.frame
" with one attribute; the identification strings for each sample in the collection
units
object of class "character
"; units in which the wavelengths are expressed
data
object of class data.frame containing the attribute data
Objects can be created by calls of the form
new("Spectra", ...)
, with the constructor functions like
Spectra(...)
, or with the helper functions such as wl
and
spectra
.
Pierre Roudier [email protected]
showClass("Spectra") showClass("SpectraDataFrame")
showClass("Spectra") showClass("SpectraDataFrame")
Spectra*
objects.Either retrieves the spectra matrix from a Spectra*
object, or
creates a Spectra*
object from a "data.frame"
object different
interfaces detailed below.When applied to a Spectra*
object, this functions simply returns the spectra it is storing.
## S4 method for signature 'Spectra' spectra(object) ## S4 replacement method for signature 'data.frame' spectra(object, ...) <- value ## S4 replacement method for signature 'Spectra' spectra(object) <- value
## S4 method for signature 'Spectra' spectra(object) ## S4 replacement method for signature 'data.frame' spectra(object, ...) <- value ## S4 replacement method for signature 'Spectra' spectra(object) <- value
object |
an object of class |
... |
see details below |
value |
see details below |
If applied on a "data.frame"
object, it is an helper function to
create a Spectra*
object. Two kind of interfaces are then available.
value
can be:
Similarly to wl
, the wavelengths of the spectra can be passed by a "numeric"
vector. Alternatively, the names of the columns that contain the spectra information can be passed as a "character"
vector.
This interface is specific to inspectr. It follows a scheme where differents parts can be observed, the id column, the attributes columns, and the spectra columns, described by the wavelengths at which it has been measured:
Placeholders:
...
placeholder for all the columns of your data.frame object except those that have been already used in other parts of the formula. This can lead to errors. E.g. if object
has data one every wavelength between 350 and 2500 nm, spectra(object) <- id_field ~ ... ~ 500:2500
will stores the columns corresponding to the wavelengths 350-499 nm in its data slot!
id
For the creation of a SpectraDataFrame
, it is important to always specify an id field in the formula. If no id column is present, the id
placeholder will create one for you.
spectra(object) <- ~ 350:2500
will build a Spectra
object from the wavelengths between 350 and 2500, based on the column names.
spectra(object) <- ~ 350:2:2500
will build a Spectra
object from the wavelengths in seq(350, 2500, by = 2)
spectra(object) <- ~ 500:2350
will build a Spectra
object from the wavelengths between 500 and 2350, even though other wavelengths are present (they will be dropped)
In the three later cases, the id field has been dropped (it will be automatically created). If you want to use a column of "data.frame"
as an id filed, you can still use the first part of the formula:
spectra(object) <- id_field ~ 350:2500
spectra(object) <- id_field ~ 350:5:2500
Some data can also be added to the object, which will then be of SpectraDataFrame
class:
spectra(object) <- id_field ~ property1 ~ 500:2300
will create a SpectraDataFrame
with ids from the id_field column, data from the property1 column, and spectral information between 500 and 2300 nm. That means that data property2, and all spectral information from bands < 500 nm or > 2300 nm will be dropped
You can also combine the placeholders:
spectra(object) <- id_field ~ ... ~ 350:2500
will create a SpectraDataFrame
object with ids from the id_field column, all spectral bands between 350 and 2500 nm. The data slot is given all the remaining columns.
Pierre Roudier [email protected]
# Loading example data data(oz) class(oz) # this is a simple data.frame # structure of the data.frame: it is rowwise-formatted big.head(oz) ## CREATING Spectra OBJECTS ## # Using spectra() to initiate a Spectra from # the data.frame spectra(oz) <- sr_no ~ 350:2500 # It is possible to select wavelengths using the formula interface data(oz) spectra(oz) <- sr_no ~ 350:5:2500 data(oz) spectra(oz) <- sr_no ~ 500:1800 ## CREATING SpectraDataFrame OBJECTS ## # Using spectra() to initiate a SpectraDataFrame from # the data.frame data(oz) spectra(oz) <- sr_no ~ carbon + ph + clay ~ 350:2500 # Selecting data to be included in the SpectradataFrame object data(oz) spectra(oz) <- sr_no ~ carbon ~ 350:2500 # Forcing the creation of new ids using the id keyword in the # formula interface data(oz) spectra(oz) <- id ~ carbon ~ 350:2500 ids(oz, as.vector = TRUE) # Using the "..." short-hand to select all the remaining columns data(oz) spectra(oz) <- sr_no ~ ... ~ 350:2500 ## CREATING Spectra OBJECTS FROM ## BY-COLS-FORMATTED DATA ## # For data formatted in the colwise format, # use the "colwise" mode # Transforming data into colwise format # for demonstration's sake # m <- melt_spectra(oz) oz_by_col <- reshape2::acast(m, ... ~ sr_no) oz_by_col <- data.frame( wl = rownames(oz_by_col), oz_by_col, check.names = FALSE) # Here's colwise-formatted data big.head(oz_by_col) # Convert it into Spectra object spectra(oz_by_col, mode = "colwise") <- wl ~ ... # Then data can be added to promote it as a SpectraDataFrame my.data <- features(oz) features(oz_by_col) <- my.data
# Loading example data data(oz) class(oz) # this is a simple data.frame # structure of the data.frame: it is rowwise-formatted big.head(oz) ## CREATING Spectra OBJECTS ## # Using spectra() to initiate a Spectra from # the data.frame spectra(oz) <- sr_no ~ 350:2500 # It is possible to select wavelengths using the formula interface data(oz) spectra(oz) <- sr_no ~ 350:5:2500 data(oz) spectra(oz) <- sr_no ~ 500:1800 ## CREATING SpectraDataFrame OBJECTS ## # Using spectra() to initiate a SpectraDataFrame from # the data.frame data(oz) spectra(oz) <- sr_no ~ carbon + ph + clay ~ 350:2500 # Selecting data to be included in the SpectradataFrame object data(oz) spectra(oz) <- sr_no ~ carbon ~ 350:2500 # Forcing the creation of new ids using the id keyword in the # formula interface data(oz) spectra(oz) <- id ~ carbon ~ 350:2500 ids(oz, as.vector = TRUE) # Using the "..." short-hand to select all the remaining columns data(oz) spectra(oz) <- sr_no ~ ... ~ 350:2500 ## CREATING Spectra OBJECTS FROM ## BY-COLS-FORMATTED DATA ## # For data formatted in the colwise format, # use the "colwise" mode # Transforming data into colwise format # for demonstration's sake # m <- melt_spectra(oz) oz_by_col <- reshape2::acast(m, ... ~ sr_no) oz_by_col <- data.frame( wl = rownames(oz_by_col), oz_by_col, check.names = FALSE) # Here's colwise-formatted data big.head(oz_by_col) # Convert it into Spectra object spectra(oz_by_col, mode = "colwise") <- wl ~ ... # Then data can be added to promote it as a SpectraDataFrame my.data <- features(oz) features(oz_by_col) <- my.data
Constructor for the SpectraDataFrame class. Creates a SpectraDataFrame object, either from scratch, or from an existing Spectra object.
SpectraDataFrame( ..., wl = numeric(), nir = matrix(), id = as.character(NA), units = "nm", data = data.frame() )
SpectraDataFrame( ..., wl = numeric(), nir = matrix(), id = as.character(NA), units = "nm", data = data.frame() )
... |
an object inheriting from |
wl |
a numeric vector giving the wavelengths at with the spectra have been measured |
nir |
a |
id |
a vector giving the unique id of each sample in the collection |
units |
a character giving the unit in which the wavelengths values are expressed |
data |
object of class |
a new "SpectraDataFrame"
object
Pierre Roudier [email protected]
# Creating a SpectraDataFrame object from scratch my.wl <- 350:2500 my.id <- c("A", "B") my.nir <- matrix(runif(2*length(my.wl)), nrow=2) my.data <- data.frame(foo = runif(2), bar = LETTERS[1:2]) my.sdf <- SpectraDataFrame(wl = my.wl, nir = my.nir, id = my.id, data = my.data) # Creating a SpectraDataFrame object from an existing Spectra object my.s <- Spectra(wl = my.wl, nir = my.nir, id = my.id) my.sdf <- SpectraDataFrame(my.s, data = my.data)
# Creating a SpectraDataFrame object from scratch my.wl <- 350:2500 my.id <- c("A", "B") my.nir <- matrix(runif(2*length(my.wl)), nrow=2) my.data <- data.frame(foo = runif(2), bar = LETTERS[1:2]) my.sdf <- SpectraDataFrame(wl = my.wl, nir = my.nir, id = my.id, data = my.data) # Creating a SpectraDataFrame object from an existing Spectra object my.s <- Spectra(wl = my.wl, nir = my.nir, id = my.id) my.sdf <- SpectraDataFrame(my.s, data = my.data)
This is the correction method available in the ViewSpec Pro software from ASD, which aims at correcting steps in the data (see details).
## S4 method for signature 'Spectra' splice(x, locations = list(c(750, 1000), c(1830, 1950)))
## S4 method for signature 'Spectra' splice(x, locations = list(c(750, 1000), c(1830, 1950)))
x |
a |
locations |
the wavelengths to cut out and interpolate |
The SWIR1 part of the spectrum (1000-1800 nm) is taken as a reference for corrections as it is stable to the instrument sensitivity drift (Beal and Eamon, 2010)
This is based on a description of the splice correction algorithm described in:
Beal, D. and Eamon, M., 1996. Dynamic, Parabolic Linear Transformations of 'Stepped' Radiometric Data. Analytical Spectral Devices Inc., Boulder, CO.
an object of same class as x
Pierre Roudier [email protected]
data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 oz_spliced <- splice(australia) plot(oz_spliced)
data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 oz_spliced <- splice(australia) plot(oz_spliced)
Splits a Spectra* object into groups using a factor, either a provided as a vector or as an attribute in the features of the object.
## S4 method for signature 'Spectra' split(x, f, drop = FALSE, ...)
## S4 method for signature 'Spectra' split(x, f, drop = FALSE, ...)
x |
Spectra object |
f |
either a vector of factors (for objects inheriting from
|
drop |
ignored |
... |
further potential arguments passed to methods. |
This is an adaptation of the split
function in the base package.
A list of objects of same class as x
.
Pierre Roudier [email protected]
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # On a Spectra object, we need to provide a vector of factors # to split the object s <- as(australia, 'Spectra') # We make up some kind of factor to split the data. idx <- sample(letters[1:5], replace = TRUE, size = nrow(s)) # This is a vector r <- split(s, idx) str(r) # On a SpectradataFrame object, we can also provide the name or index # of an attribute # Generate some kind of factor australia$fact <- sample(LETTERS[1:3], size = nrow(australia), replace = TRUE) r <- split(australia, 'fact') str(r) # A list is returned, and is thus ready for use with lapply, or any # of the l*ply functions from the plyr package lapply(r, nrow)
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # On a Spectra object, we need to provide a vector of factors # to split the object s <- as(australia, 'Spectra') # We make up some kind of factor to split the data. idx <- sample(letters[1:5], replace = TRUE, size = nrow(s)) # This is a vector r <- split(s, idx) str(r) # On a SpectradataFrame object, we can also provide the name or index # of an attribute # Generate some kind of factor australia$fact <- sample(LETTERS[1:3], size = nrow(australia), replace = TRUE) r <- split(australia, 'fact') str(r) # A list is returned, and is thus ready for use with lapply, or any # of the l*ply functions from the plyr package lapply(r, nrow)
Returns subsets of a SpectraDataFrame object.
subset(x, ...)
subset(x, ...)
x |
SpectraDataFrame object |
... |
see details below |
Additional parameters:
logical expression indicating elements or rows to keep: missing values are taken as false
expression, indicating columns to select from the data slot
passed on to "[" indexing operator
Additional arguments
SpectraDataFrame object
Pierre Roudier [email protected]
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Subset on attributes s <- subset(australia, carbon < 5) # Subset and selection of attributes s <- subset(australia, carbon < 5, select = 1)
# Loading example data data(australia) spectra(australia) <- sr_no ~ ... ~ 350:2500 # Subset on attributes s <- subset(australia, carbon < 5) # Subset and selection of attributes s <- subset(australia, carbon < 5, select = 1)
Summarize a Spectra* object.
\method{summary}{Spectra}(object, ...) ## S3 method for class 'summary.Spectra' print(x, ...)
\method{summary}{Spectra}(object, ...) ## S3 method for class 'summary.Spectra' print(x, ...)
object |
an object of class |
... |
Additional arguments passed to |
x |
a result of |
A "summary.Spectra"
object
NULL
Pierre Roudier [email protected]
data(oz) spectra(oz) <- sr_no ~ ... ~ 350:2500 summary(oz)
data(oz) spectra(oz) <- sr_no ~ ... ~ 350:2500 summary(oz)
Spectra*
object.Either retrieves the wavelengths from a Spectra*
object, or creates a
Spectra*
object from a "data.frame"
object by setting some of
its columns as the wavelengths.
When applied to a Spectra*
object, this functions simply returns the
wavelengths of the spectra it is storing.
If applied on a "data.frame"
object, it is an helper function to
create a Spectra*
object. It then needs to be indicated the
wavelengths at which the spectra values are measured. The assumption is that
each row of the "data.frame"
is a spectra, and the column names of
the "data.frame"
contain the wavelengths values.
If all the columns are used to create the Spectra*
object, a
Spectra
object is created. If some attributes are left, they will be
used to generate a SpectraDataFrame
object.
## S4 method for signature 'Spectra' wl(object) ## S4 replacement method for signature 'data.frame' wl(object) <- value ## S4 replacement method for signature 'Spectra' wl(object) <- value
## S4 method for signature 'Spectra' wl(object) ## S4 replacement method for signature 'data.frame' wl(object) <- value ## S4 replacement method for signature 'Spectra' wl(object) <- value
object |
a |
value |
the wavelengths of the |
If applied on a "data.frame"
, either a Spectra
or a
SpectraDataFrame
object. If applied on a Spectra*
object, a
vector.
Pierre Roudier [email protected]
spectra
, Spectra-class
,
SpectraDataFrame-class
# Loading example data data(oz) spectra(oz) <- sr_no ~ ... ~ 350:2500 # Retrieving wavelengths from Spectra* object wl(oz) # Replacing wavelength values - USE WITH CAUTION! wl(oz) <- 1:length(oz) wl(oz) # Use to initiate a Spectra* object from a data.frame data(oz) wl(oz) <- 350:2500 ids(oz) <- ~ sr_no
# Loading example data data(oz) spectra(oz) <- sr_no ~ ... ~ 350:2500 # Retrieving wavelengths from Spectra* object wl(oz) # Replacing wavelength values - USE WITH CAUTION! wl(oz) <- 1:length(oz) wl(oz) # Use to initiate a Spectra* object from a data.frame data(oz) wl(oz) <- 350:2500 ids(oz) <- ~ sr_no
Retrieves the wavelengths units from Spectra*
object
wl_units(object) ## S4 replacement method for signature 'Spectra' wl_units(object) <- value
wl_units(object) ## S4 replacement method for signature 'Spectra' wl_units(object) <- value
object |
an object inheriting from class |
value |
a character string |
A vector
Pierre Roudier [email protected]
# Loading example data data(oz) spectra(oz) <- sr_no ~ ... ~ 350:2500 # Print wavelength information wl(oz) range(wl(oz)) # Manipulate wavelength information wl(oz) <- wl(oz) + 1000 wl(oz)
# Loading example data data(oz) spectra(oz) <- sr_no ~ ... ~ 350:2500 # Print wavelength information wl(oz) range(wl(oz)) # Manipulate wavelength information wl(oz) <- wl(oz) + 1000 wl(oz)