Skip to content

Commit 37edc35

Browse files
committed
Updates for additional vignette
1 parent 9c23ddb commit 37edc35

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+4750
-238
lines changed

DESCRIPTION

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
Package: rloadest
22
Type: Package
33
Title: River Load Estimation
4-
Version: 0.4.0
5-
Date: 2015-02-27
4+
Version: 0.4.2
5+
Date: 2015-07-20
66
Author: Dave Lorenz, Rob Runkel, Laura De Cicco
77
Maintainer: Dave Lorenz <[email protected]>
88
Description: Collection of functions to make constituent load estimations
99
based on the LOADEST program.
1010
License: CC0
1111
Depends:
1212
R (>= 3.0.0),
13+
smwrBase,
1314
smwrGraphs,
1415
smwrStats,
1516
smwrQW
1617
Imports:
17-
stats
18+
lubridate,
19+
stats,
20+
segmented
1821
Suggests:
19-
smwrBase,
20-
dataRetrieval
22+
dataRetrieval,
23+
EGRET,
24+
survival
2125
LazyLoad: yes
2226
LazyData: yes

NAMESPACE

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# Generated by roxygen2 (4.0.2): do not edit by hand
1+
# Generated by roxygen2 (4.1.0): do not edit by hand
22

3+
S3method(censoring,Surv)
34
S3method(coef,loadReg)
45
S3method(fitted,loadReg)
56
S3method(logLik,loadReg)
@@ -25,8 +26,14 @@ export(nashSutcliffe)
2526
export(predConc)
2627
export(predLoad)
2728
export(resampleUVdata)
29+
export(seg)
30+
export(segLoadReg)
31+
export(segment)
2832
export(selBestModel)
2933
export(selBestSubset)
34+
import(lubridate)
35+
import(segmented)
36+
import(smwrBase)
3037
import(smwrGraphs)
3138
import(smwrQW)
3239
import(smwrStats)

R/Models.R

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#' Models
2+
3+
#' A dataset that describes the equivalent formula for each of the
4+
#'predefined model number.
5+
#' @name Models
6+
#' @docType data
7+
#' @usage Models
8+
#' @format Data frame with 9 rows and 2 columns\cr
9+
#'\tabular{lll}{
10+
#'Name \tab Type \tab Description\cr
11+
#'Number \tab integer \tab The predefined model number\cr
12+
#'Formula \tab factor \tab The equivalent formula for the predefined model number\cr
13+
#'}
14+
#' @keywords datasets
15+
#' @seealso \code{\link{model}}
16+
#' @examples
17+
#' data(Models)
18+
#' print(Models)
19+
NULL

R/censoring.Surv.R

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#' Describe Censoring
2+
#'
3+
#' Gets the type of censoring ("none," "left," "multiple") for an object.
4+
#'
5+
#'
6+
#' @param x the object to get the type of censoring. For an object of class
7+
#'"Surv," the type must be "interval."
8+
#' @return A character string "none," "left," or "multiple" describing the type
9+
#'of censoring present in the object.
10+
#' @note This function is mostly used within other functions to determine the
11+
#''best' technique to use for analysis.
12+
#' @keywords censored attribute
13+
#' @examples
14+
#'\dontrun{
15+
#'library(survival)
16+
#'censoring(Surv(2.3, 2.3, type="interval2"))
17+
#'}
18+
#' @export
19+
#' @method censoring Surv
20+
censoring.Surv <- function(x) {
21+
if(attr(x, "type") != "interval")
22+
stop("Only interval type censoring supported")
23+
# Strip NAs from x
24+
x <- x[!is.na(x)]
25+
if(all(x[, 3] == 1)) {
26+
return("none")
27+
} else if(any(x[, 3] %in% c(0,3))) {
28+
return("multiple")
29+
}
30+
return("left")
31+
}
32+

R/loadReg.R

+49-27
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@
99
#'For un- or left-censored data, AMLE is used unless weights are specified in
1010
#'the model, then MLE is used, through a call to \code{survreg}. For any other
1111
#'censored data, MLE is used.\cr
12-
#'See \code{\link{loadConvFactor}} for details about valid values for
13-
#'\code{flow.units}, \code{con.units} and \code{load.units}.
12+
#'
13+
#'Typically, \code{loadReg} expects the response variable to have units of
14+
#'concentration, mass per volume. For these models, See \code{\link{loadConvFactor}}
15+
#'for details about valid values for \code{flow.units}, \code{conc.units} and
16+
#'\code{load.units}. For some applications, like bed load estimation, the response
17+
#'variable can have units of flux, mass per time. For these models, \code{conc.units}
18+
#'can be expressed as any valid \code{load.units} per day. The rate must be expressed
19+
#'in terms of "/d," "/dy," or "/day."
1420
#'
1521
#' @param formula a formula describing the regression model. See \bold{Details}.
1622
#' @param data the data to search for the variables in \code{formula}.
@@ -44,14 +50,22 @@
4450
#' flow = "FLOW", dates = "DATES", conc.units="mg/L",
4551
#' station="Illinois River at Marseilles, Ill.")
4652
#'print(app1.lr)
47-
#'
53+
#' @import smwrBase
4854
#' @import smwrQW
4955
#' @export
5056
loadReg <- function(formula, data, subset, na.action, flow, dates,
5157
flow.units="cfs", conc.units="", load.units="kg",
5258
time.step="day", station="") {
53-
## Coding history:
54-
## 2013May31 DLLorenz Original Coding
59+
## Function to check if conc.units are in terms of loads
60+
## returns logical with attribute of loads if TRUE
61+
ckLoad <- function(units) {
62+
# check if rate is /d, /dy, or /day (actually any /d)
63+
retval <- grepl("/d", units)
64+
if(retval) { # strip off rate
65+
attr(retval, "load") <- sub("/d.*", "", units)
66+
}
67+
return(retval)
68+
}
5569
##
5670
## Trap model number specification
5771
PredMod <- terms(formula, "model", data = data)
@@ -199,41 +213,49 @@ loadReg <- function(formula, data, subset, na.action, flow, dates,
199213
}
200214
Tdys <- min(Tdifs)
201215
if(Tdys < 7)
202-
warning("The minimum spacing between daily loads is ", Tdys,
216+
warning("The minimum spacing between daily loads is ", signif(Tdys, 2L),
203217
" days. The time between observations should be at least ",
204218
" 7 days to avoid autocorrelation issues.")
205219
} else { # Need unit checks too
206220
Tdys <- difftime(Time, shiftData(Time), units="days")
207221
Tdys <- min(Tdys, na.rm=TRUE)
208222
if(Tdys < 7)
209-
warning("The minimum spacing between daily loads is ", Tdys,
223+
warning("The minimum spacing between observed loads is ", signif(Tdys, 2L),
210224
" days. The time between observations should be at least ",
211225
" 7 days to avoid autocorrelation issues.")
212226
}
213-
## OK, construct the concentration fit
214-
if(class(Y) == "lcens") {
215-
cfit <- censReg_AMLE.fit(Y, X, "lognormal")
216-
fit1 <- censReg_AMLE.fit(Y, X[,1L, drop=FALSE], "lognormal")
217-
} else {
218-
cfit <- censReg_MLE.fit(Y, X, rep(1, length(Y)), "lognormal")
219-
fit1 <- censReg_MLE.fit(Y, X[,1L, drop=FALSE],
220-
rep(1, length(Y)), "lognormal")
221-
}
222-
cfit$LLR1 <- fit1$LLR
223-
cfit$call <- call
224-
cfit$terms <- Terms
225-
cfit$na.action <- saved.na.action
226-
cfit$na.message <- na.message
227-
cfit$xlevels <- xlevels
228-
class(cfit) <- "censReg"
229-
## Not the load model fit
230-
## Check on concentration units
227+
## Checks on concentration units
231228
if(conc.units == "") {
232229
warning("Concentration units assumed to be mg/L")
233230
conc.units <- "mg/L"
234231
}
235-
CF <- loadConvFactor(flow.units, conc.units, load.units)
236-
Y <- Y * CF * Flow
232+
load.only <- ckLoad(conc.units)
233+
if(load.only) {
234+
cfit <- NULL
235+
## Prepare for load model fit
236+
CF <- loadUnitConv(attr(load.only, "load"), load.units)
237+
Y <- Y * CF
238+
} else {
239+
## OK, construct the concentration fit
240+
if(class(Y) == "lcens") {
241+
cfit <- censReg_AMLE.fit(Y, X, "lognormal")
242+
fit1 <- censReg_AMLE.fit(Y, X[,1L, drop=FALSE], "lognormal")
243+
} else {
244+
cfit <- censReg_MLE.fit(Y, X, rep(1, length(Y)), "lognormal")
245+
fit1 <- censReg_MLE.fit(Y, X[,1L, drop=FALSE],
246+
rep(1, length(Y)), "lognormal")
247+
}
248+
cfit$LLR1 <- fit1$LLR
249+
cfit$call <- call
250+
cfit$terms <- Terms
251+
cfit$na.action <- saved.na.action
252+
cfit$na.message <- na.message
253+
cfit$xlevels <- xlevels
254+
class(cfit) <- "censReg"
255+
## Now prepare the load model fit
256+
CF <- loadConvFactor(flow.units, conc.units, load.units)
257+
Y <- Y * CF * Flow
258+
}
237259
if(class(Y) == "lcens") {
238260
lfit <- censReg_AMLE.fit(Y, X, "lognormal")
239261
fit1 <- censReg_AMLE.fit(Y, X[,1L, drop=FALSE], "lognormal")

R/plot.loadReg.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ plot.loadReg <- function(x, which='All', set.up=TRUE, span=1.0, ...) {
147147
if(doPlot[4L])
148148
corGram(dectime(x$Time), Resids)
149149
## Add details of call on regression model to next plots
150-
Mod <- format(x$lfit$call$formula)
150+
Mod <- paste0(as.character(x$lfit$call$formula[2L]), " ~ model(", x$model.no, ")")
151151
## 3rd plot, S-L
152152
RSE <- rmse(x$lfit) # now the resid std. error
153153
if(doPlot[3L]) {

R/predConc.R

+5-6
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,14 @@
3939
#' station="Illinois River at Marseilles, Ill.")
4040
#'predConc(app1.lr, app1.calib)
4141
#' @useDynLib rloadest estlday
42+
#' @import lubridate
4243
#' @export
4344
predConc <- function(fit, newdata, by="day",
4445
allow.incomplete=FALSE, conf.int=0.95) {
45-
## Coding history:
46-
## 2013Jul18 DLLorenz Original Coding from predLoad
47-
## 2013Dec05 DLLorenz Bug fix
48-
## 2014Sep23 DLLorenz Missing check on newdata
4946
##
5047
## By options and other preliminary code
51-
if(any(is.na(newdata)))
52-
stop("newdata contains missing values, remove before prediction")
48+
if(is.null(fit$cfit))
49+
stop("model cannot predict concentration values")
5350
if(nrow(newdata) > 176000L)
5451
stop("newdata has too many rows, the size limit is 176000")
5552
ByOpt <- c("unit", "day")
@@ -97,6 +94,8 @@ predConc <- function(fit, newdata, by="day",
9794
} else {
9895
model.inp <- setXLDat(newdata, flow, dates, Qadj, Tadj, model.no)
9996
}
97+
if(any(is.na(model.inp)))
98+
stop("Prediction data contains missing values, remove before prediction")
10099
## Construct the structure for aggregating the data
101100
## Deal with byn == 0 directly in the last estimation section
102101
checkDays <- list() # Create empty list for day checking

R/predLoad.R

+5-8
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ predLoad <- function(fit, newdata, load.units=fit$load.units, by="total",
6464
## 2014Sep23 DLLorenz Missing check on newdata
6565
##
6666
## By options and other preliminary code
67-
if(any(is.na(newdata)))
68-
stop("newdata contains missing values, remove before prediction")
6967
if(nrow(newdata) > 176000L)
7068
stop("newdata has too many rows, the size limit is 176000")
7169
ByOpt <- c("unit", "day", "month", "water year", "calendar year", "total")
@@ -106,12 +104,9 @@ predLoad <- function(fit, newdata, load.units=fit$load.units, by="total",
106104
newdata <- aggregate(newdata, list(time.step=gps), mean)
107105
attr(newdata[[dates]], "tzone") <- ntz
108106
} else { # Must be instantaneous
109-
gps <- format(newdata[[dates]])
110-
difdays <- range(newdata[[dates]])
111-
difdays <- difftime(ceiling_date(difdays[2L], unit="day"),
112-
floor_date(difdays[1L], unit="day"),
113-
units="days")
114-
gps.nday <- round(nrow(newdata)/as.numeric(difdays), 6L) # should get to near integer
107+
gps <- format(newdata[[dates]], format="%Y-%m-%d")
108+
numdays <- length(unique(gps)) # the number of days in the datset
109+
gps.nday <- round(nrow(newdata)/as.numeric(numdays), 6L) # should get to near integer
115110
if((gps.nday %% 1) != 0 && !(by %in% c("unit", "day")))
116111
stop("newdata must have the same number of observations each day")
117112
gps.nday <- as.integer(gps.nday)
@@ -124,6 +119,8 @@ predLoad <- function(fit, newdata, load.units=fit$load.units, by="total",
124119
} else {
125120
model.inp <- setXLDat(newdata, flow, dates, Qadj, Tadj, model.no)
126121
}
122+
if(any(is.na(model.inp)))
123+
stop("Prediction data contains missing values, remove before prediction")
127124
## Construct the structure for aggregating the data
128125
## Deal with byn == 0 directly in the last estimation section
129126
checkDays <- list() # Create empty list for day checking

0 commit comments

Comments
 (0)