knitr::opts_chunk$set(echo = TRUE)
A properly formatted input dataset for streamMetabolizer models has:
metab_inputs()
to see the requirements for a specific model type.?mm_data
for definitions of each column.An input dataset may optionally include:
An example of a properly formatted input dataset is available in the streamMetabolizer package - data are from French Creek in Laramie, WY, courtesy of Bob Hall.
library(streamMetabolizer) dat <- data_metab(num_days='3', res='15')
Inspect the dimensions and column names of the data.
dim(dat) dat[c(1,48,96,240,288),] # some example rows
You can get additional information about the expected format of the data in the ?metab
help document.
When preparing your own data, make sure the class and units of your data match those specified in that document.
You can use other common R packages to graphically inspect the input data. Look for outliers and oddities to ensure the quality of your data.
library(dplyr) library(tidyr) library(ggplot2)
dat %>% mutate(DO.pctsat = 100 * (DO.obs / DO.sat)) %>% select(solar.time, starts_with('DO')) %>% gather(type, DO.value, starts_with('DO')) %>% mutate(units=ifelse(type == 'DO.pctsat', 'DO\n(% sat)', 'DO\n(mg/L)')) %>% ggplot(aes(x=solar.time, y=DO.value, color=type)) + geom_line() + facet_grid(units ~ ., scale='free_y') + theme_bw() + scale_color_discrete('variable')
labels <- c(depth='depth\n(m)', temp.water='water temp\n(deg C)', light='PAR\n(umol m^-2 s^-1)') dat %>% select(solar.time, depth, temp.water, light) %>% gather(type, value, depth, temp.water, light) %>% mutate( type=ordered(type, levels=c('depth','temp.water','light')), units=ordered(labels[type], unname(labels))) %>% ggplot(aes(x=solar.time, y=value, color=type)) + geom_line() + facet_grid(units ~ ., scale='free_y') + theme_bw() + scale_color_discrete('variable')
Your data need to have specific column names and units. To see what is required,
use the metab_inputs
function to get a description of the required inputs for
a given model type. The output of metab_inputs is a table describing the
required column names, the classes and units of the values in each column, and
whether that column is required or optional. The inputs are identical for the
model types 'mle', 'bayes', and 'night', so here we'll just print the
requriements for 'mle'.
metab_inputs('mle', 'data')
Also read through the help pages at ?metab
and ?mm_data
for more detailed variable definitions and requirements.
To prepare your timestamps for metabolism modeling, you need to convert from the initial number or text format into POSIXct with the correct timezone (tz), then to solar mean time.
Convert your logger-format data to POSIXct in a local timezone (with or without daylight savings, as long as you have that timezone scheme specified). Here are a few examples of specific scenarios and solutions.
If you have datetimes stored in seconds since 1/1/1970 at Greenwich (i.e., in UTC):
num.time <- 1471867200 (posix.time.localtz <- as.POSIXct(num.time, origin='1970-01-01', tz='UTC'))
If you have datetimes stored in seconds since 1/1/1970 at Laramie, WY (i.e., in MST, no daylight savings):
num.time <- 1471867200 (posix.time.nominalUTC <- as.POSIXct(num.time, origin='1970-01-01', tz='UTC')) # the numbers get treated as UTC no matter what tz you request (posix.time.localtz <- lubridate::force_tz(posix.time.nominalUTC, 'Etc/GMT+7')) # +7 = mountain standard time
If you have datetimes stored as text timestamps in UTC, you can bypass the conversion to local time and just start with UTC. Then rather than using calc_solar_time()
in Step 2, you'll use convert_UTC_to_solartime()
.
text.time <- '2016-08-22 12:00:00' (posix.time.utc <- as.POSIXct(text.time, tz='UTC'))
If you have datetimes stored as text timestamps in EST/EDT (with daylight savings):
text.time <- '2016-08-22 12:00:00' (posix.time.localtz <- as.POSIXct(text.time, format="%Y-%m-%d %H:%M:%S", tz='America/New_York'))
If you have datetimes stored as text timestamps in EST (no daylight savings):
text.time <- '2016-08-22 12:00:00' (posix.time.localtz <- as.POSIXct(text.time, format="%Y-%m-%d %H:%M:%S", tz='Etc/GMT+5'))
See https://en.wikipedia.org/wiki/List_of_tz_database_time_zones for a list of timezone names.
chron
datetimesIf you have datetimes stored in the chron
time format in EST (no daylight savings):
chron.time <- chron::chron('08/22/16', '12:00:00') time.format <- "%Y-%m-%d %H:%M:%S" text.time <- format(chron.time, time.format) # direct as.POSIXct time works poorly (posix.time.localtz <- as.POSIXct(text.time, format=time.format, tz='Etc/GMT+5'))
Now convert from local time to solar time. In streamMetabolizer
vocabulary,
solar.time
specifically means mean solar time, the kind where every day is
exactly 24 hours, in contrast to apparent solar time. You're ready for this step
when you have the correct time in a local timezone and lubridate::tz(yourtime)
reflects the correct timezone.
lubridate::tz(posix.time.localtz) # yep, we want and have the code for EST (posix.time.solar <- streamMetabolizer::calc_solar_time(posix.time.localtz, longitude=-106.3))
streamMetabolizer offers many functions to help you prepare your data for modeling. We'll add more to this section later. For now, we recommend that you explore the help pages for the following functions:
calc_depth
calc_DO_sat
calc_light
convert_date_to_doyhr
convert_localtime_to_UTC
convert_UTC_to_solartime
convert_k600_to_kGAS
convert_PAR_to_SW
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.