Skip to content

Commit

Permalink
Merge pull request #78 from ldecicco-USGS/master
Browse files Browse the repository at this point in the history
Include randomized residuals.
  • Loading branch information
ldecicco-USGS committed Apr 15, 2016
2 parents dfbf17e + 1ad237a commit 491c050
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 121 deletions.
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: EGRET
Type: Package
Title: Exploration and Graphics for RivEr Trends (EGRET)
Version: 2.4.3
Version: 2.5.0
Authors@R: c( person("Robert", "Hirsch", role = c("aut"),
email = "rhirsch@usgs.gov"),
person("Laura", "DeCicco", role = c("aut","cre"),
Expand All @@ -10,7 +10,7 @@ Description: Statistics and graphics for streamflow history,
water quality trends, and the statistical modeling algorithm: Weighted
Regressions on Time, Discharge, and Season (WRTDS).
License: CC0
Date: 2016-03-04
Date: 2016-03-07
Depends:
R (>= 3.0)
Imports:
Expand All @@ -22,7 +22,8 @@ Imports:
utils,
graphics,
stats,
grDevices
grDevices,
truncnorm
Suggests:
xtable,
knitr,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,4 @@ import(survival)
import(utils)
importFrom(fields,interp.surface)
importFrom(lubridate,decimal_date)
importFrom(truncnorm,rtruncnorm)
21 changes: 11 additions & 10 deletions R/boxResidMonth.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#' @param font.main font to be used for plot main titles
#' @param customPar logical defaults to FALSE. If TRUE, par() should be set by user before calling this function
#' @param las numeric in {0,1,2,3}; the style of axis labels
#' @param rResid logical. Show censored residuals as randomized.
#' @param \dots arbitrary graphical parameters that will be passed to genericEGRETDotPlot function (see ?par for options)
#' @keywords graphics water-quality statistics
#' @seealso \code{\link[graphics]{boxplot}}
Expand All @@ -35,7 +36,7 @@
#' boxResidMonth(eList)
boxResidMonth<-function(eList, stdResid = FALSE, las=1,
printTitle = TRUE, cex=0.8, cex.axis=1.1, cex.main=1.1,
font.main=2, tinyPlot=FALSE, customPar=FALSE,...) {
font.main=2, tinyPlot=FALSE, customPar=FALSE,rResid=FALSE,...) {

localINFO <- getInfo(eList)
localSample <- getSample(eList)
Expand Down Expand Up @@ -64,15 +65,15 @@ boxResidMonth<-function(eList, stdResid = FALSE, las=1,

plotTitle<-if(printTitle) paste(localINFO$shortName,"\n",localINFO$paramShortName,"\nBoxplots of residuals by month") else ""

# if(!rResid){
if(!rResid){
resid<-log(localSample$ConcAve) - localSample$yHat
# } else {
# if(!("rResid" %in% names(localSample))){
# eList <- makeAugmentedSample(eList)
# localSample <- eList$Sample
# }
# resid <- localSample$rResid
# }
} else {
if(!("rResid" %in% names(localSample))){
eList <- makeAugmentedSample(eList)
localSample <- eList$Sample
}
resid <- localSample$rResid
}

if(stdResid) {
resid<- resid/localSample$SE
Expand All @@ -96,5 +97,5 @@ boxResidMonth<-function(eList, stdResid = FALSE, las=1,
...)
abline(h=0)
if (!tinyPlot) mtext(title2,side=3,line=-1.5)
# invisible(eList)
invisible(eList)
}
11 changes: 6 additions & 5 deletions R/fluxBiasEight.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#' @param cex.axis magnification to be used for axis annotation relative to the current setting of cex
#' @param col color of points on plot, see ?par 'Color Specification'
#' @param lwd number line width
#' @param rResid logical. Show censored residuals as randomized.
#' @param \dots arbitrary graphical parameters that will be passed to genericEGRETDotPlot function (see ?par for options)
#' @keywords graphics water-quality statistics
#' @export
Expand All @@ -39,7 +40,7 @@
#' dev.off()
#' }
fluxBiasMulti<-function (eList, qUnit = 2, fluxUnit = 3, moreTitle = "WRTDS",
cex = 0.7, cex.axis = 1.1,cex.main=1.1,
cex = 0.7, cex.axis = 1.1,cex.main=1.1,rResid=FALSE,
col="black", lwd=1,...){

localINFO <- getInfo(eList)
Expand Down Expand Up @@ -71,16 +72,16 @@ fluxBiasMulti<-function (eList, qUnit = 2, fluxUnit = 3, moreTitle = "WRTDS",
par(oma = c(0, 10, 4, 10),mfrow=c(4,2))
plotResidPred(eList,
stdResid = FALSE, tinyPlot=TRUE, printTitle = FALSE,cex=cex,
cex.axis = cex.axis, col=col,lwd=lwd,...)
cex.axis = cex.axis, col=col,lwd=lwd,rResid=rResid,...)
plotResidQ(eList,
qUnit, tinyPlot = TRUE, printTitle = FALSE,cex=cex,
cex.axis = cex.axis, col=col,lwd=lwd,...)
cex.axis = cex.axis, col=col,lwd=lwd,rResid=rResid,...)
plotResidTime(eList,
printTitle = FALSE, tinyPlot=TRUE,cex=cex,
cex.axis = cex.axis, col=col,lwd=lwd,...)
cex.axis = cex.axis, col=col,rResid=rResid,lwd=lwd,...)
boxResidMonth(eList,
printTitle = FALSE, tinyPlot=TRUE,cex=cex,
cex.axis = cex.axis,lwd=lwd,...)
cex.axis = cex.axis,lwd=lwd,rResid=rResid,...)
boxConcThree(eList,
localINFO = localINFO, printTitle=FALSE, tinyPlot=TRUE,cex=cex,
cex.axis = cex.axis, lwd=lwd,...)
Expand Down
37 changes: 19 additions & 18 deletions R/makeRandomResiduals.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@
#'
#' @param eList named list with at least the Daily dataframe
#' @keywords water-quality statistics
#' @importFrom truncnorm rtruncnorm
#' @return eList named list with modified Daily data frame.
# makeAugmentedSample <- function(eList){
#
# if(all(c("SE","yHat") %in% names(eList$Sample))){
# localSample <- eList$Sample
# numSamples<-length(localSample$Uncen)
# a <- ifelse(localSample$Uncen==0&!is.na(localSample$ConcLow),log(localSample$ConcLow)-localSample$yHat,-Inf)
# b <- ifelse(localSample$Uncen==1,+Inf,log(localSample$ConcHigh) - localSample$yHat)
# mean <- ifelse(localSample$Uncen==1,log(localSample$ConcHigh) - localSample$yHat,0)
# sd <- ifelse(localSample$Uncen==1,0,localSample$SE)
# localSample$rResid<-truncnorm::rtruncnorm(numSamples,a,b,mean,sd)
# localSample$rObserved <- exp(localSample$rResid + localSample$yHat)
#
# eList <- as.egret(eList$INFO, eList$Daily, localSample, eList$surfaces)
# } else {
# message("Pseudo only supported after running modelEstimation")
# }
# return(eList)
# }
makeAugmentedSample <- function(eList){

if(all(c("SE","yHat") %in% names(eList$Sample))){
localSample <- eList$Sample
numSamples<-length(localSample$Uncen)
a <- ifelse(localSample$Uncen==0&!is.na(localSample$ConcLow),log(localSample$ConcLow)-localSample$yHat,-Inf)
b <- ifelse(localSample$Uncen==1,+Inf,log(localSample$ConcHigh) - localSample$yHat)
mean <- ifelse(localSample$Uncen==1,log(localSample$ConcHigh) - localSample$yHat,0)
sd <- ifelse(localSample$Uncen==1,0,localSample$SE)
localSample$rResid<-truncnorm::rtruncnorm(numSamples,a,b,mean,sd)
localSample$rObserved <- exp(localSample$rResid + localSample$yHat)

eList <- as.egret(eList$INFO, eList$Daily, localSample, eList$surfaces)
} else {
message("Pseudo only supported after running modelEstimation")
}
return(eList)
}
55 changes: 28 additions & 27 deletions R/plotResidPred.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#' (for example, adjusting margins with par(mar=c(5,5,5,5))). If customPar FALSE, EGRET chooses the best margins depending on tinyPlot.
#' @param col color of points on plot, see ?par 'Color Specification'
#' @param lwd number line width
#' @param rResid logical. Show censored residuals as randomized.
#' @param \dots arbitrary graphical parameters that will be passed to genericEGRETDotPlot function (see ?par for options)
#' @keywords water-quality statistics graphics
#' @export
Expand All @@ -35,7 +36,7 @@
#' plotResidPred(eList)
plotResidPred<-function(eList, stdResid = FALSE,
tinyPlot = FALSE, printTitle = TRUE, col="black",lwd=1,
cex=0.8, cex.axis=1.1,cex.main=1.1, customPar=FALSE,...){
cex=0.8, cex.axis=1.1,cex.main=1.1, customPar=FALSE,rResid=FALSE,...){
# this function shows residual versus estimated in log space
# estimated log concentration on the x-axis (these are prior to bias correction),
# observed log concentration on y-axis
Expand Down Expand Up @@ -78,7 +79,7 @@ plotResidPred<-function(eList, stdResid = FALSE,

####################

# if(!rResid){
if(!rResid){

yLow<-log(localSample$ConcLow)-localSample$yHat
yHigh<-log(localSample$ConcHigh)-localSample$yHat
Expand All @@ -96,33 +97,33 @@ plotResidPred<-function(eList, stdResid = FALSE,

censoredSegments(yInfo$bottom, yLow, yHigh, log(x), Uncen, col=col, lwd=lwd )

# } else {
# if(!("rResid" %in% names(localSample))){
# eList <- makeAugmentedSample(eList)
# localSample <- eList$Sample
# }
#
# yHigh <- localSample$rResid
# Uncen <- localSample$Uncen
#
# if(stdResid){
# yHigh <- yHigh/localSample$SE
# }
#
# yInfo <- generalAxis(x=yHigh, minVal=NA, maxVal=NA, tinyPlot=tinyPlot)
#
# genericEGRETDotPlot(x=log(x[Uncen == 1]), y=yHigh[Uncen == 1],
# xTicks=xInfo$ticks, yTicks=yInfo$ticks,col=col,
# xlim=c(xInfo$bottom,xInfo$top), ylim=c(yInfo$bottom,yInfo$top),
# xlab=xLab, ylab=yLab, plotTitle=plotTitle, customPar=customPar,cex=cex,
# hLine=TRUE,cex.axis=cex.axis,cex.main=cex.main, tinyPlot=tinyPlot,...
# )
# points(x=log(x[Uncen == 0]), y=yHigh[Uncen == 0], pch=1,cex=cex,col=col)
#
# }
} else {
if(!("rResid" %in% names(localSample))){
eList <- makeAugmentedSample(eList)
localSample <- eList$Sample
}

yHigh <- localSample$rResid
Uncen <- localSample$Uncen

if(stdResid){
yHigh <- yHigh/localSample$SE
}

yInfo <- generalAxis(x=yHigh, minVal=NA, maxVal=NA, tinyPlot=tinyPlot)

genericEGRETDotPlot(x=log(x[Uncen == 1]), y=yHigh[Uncen == 1],
xTicks=xInfo$ticks, yTicks=yInfo$ticks,col=col,
xlim=c(xInfo$bottom,xInfo$top), ylim=c(yInfo$bottom,yInfo$top),
xlab=xLab, ylab=yLab, plotTitle=plotTitle, customPar=customPar,cex=cex,
hLine=TRUE,cex.axis=cex.axis,cex.main=cex.main, tinyPlot=tinyPlot,...
)
points(x=log(x[Uncen == 0]), y=yHigh[Uncen == 0], pch=1,cex=cex,col=col)

}

if (!tinyPlot) mtext(title2,side=3,line=-1.5)

# invisible(eList)
invisible(eList)

}
55 changes: 28 additions & 27 deletions R/plotResidQ.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#' (for example, adjusting margins with par(mar=c(5,5,5,5))). If customPar FALSE, EGRET chooses the best margins depending on tinyPlot.
#' @param col color of points on plot, see ?par 'Color Specification'
#' @param lwd number line width
#' @param rResid logical. Show censored residuals as randomized.
#' @param \dots arbitrary graphical parameters that will be passed to genericEGRETDotPlot function (see ?par for options)
#' @keywords graphics water-quality statistics
#' @export
Expand All @@ -36,7 +37,7 @@
#' plotResidQ(eList)
plotResidQ<-function (eList, qUnit = 2,
tinyPlot = FALSE, stdResid = FALSE, printTitle = TRUE,col="black",lwd=1,
cex=0.8, cex.axis=1.1,cex.main=1.1,rmSciX=FALSE, customPar=FALSE,...){
cex=0.8, cex.axis=1.1,cex.main=1.1,rmSciX=FALSE, customPar=FALSE,rResid=FALSE,...){

localINFO <- getInfo(eList)
localSample <- getSample(eList)
Expand Down Expand Up @@ -83,7 +84,7 @@ plotResidQ<-function (eList, qUnit = 2,

xInfo <- generalAxis(x=x, minVal=NA, maxVal=NA, logScale=TRUE, tinyPlot=tinyPlot,padPercent=5)

# if(!rResid){
if(!rResid){

yLow <- log(localSample$ConcLow) - localSample$yHat
yHigh <- log(localSample$ConcHigh) - localSample$yHat
Expand All @@ -104,32 +105,32 @@ plotResidQ<-function (eList, qUnit = 2,
)

censoredSegments(yInfo$bottom, yLow, yHigh, x, Uncen,col=col, lwd=lwd )
# } else {
#
# if(!("rResid" %in% names(localSample))){
# eList <- makeAugmentedSample(eList)
# localSample <- eList$Sample
# }
# yHigh <- localSample$rResid
#
# if(stdResid){
# yHigh <- yHigh/localSample$SE
# }
#
# yInfo <- generalAxis(x=yHigh, minVal=NA, maxVal=NA, tinyPlot=tinyPlot,padPercent=5)
#
# genericEGRETDotPlot(x=x[Uncen == 1], y=yHigh[Uncen == 1],
# xTicks=xInfo$ticks, yTicks=yInfo$ticks,hLine=TRUE,
# xlim = c(xInfo$bottom, xInfo$top), ylim = c(yInfo$bottom, yInfo$top),
# xlab = xLab, ylab = yLab, plotTitle=plotTitle,cex=cex,
# log = "x", cex.axis=cex.axis,cex.main=cex.main, col=col,
# tinyPlot=tinyPlot,rmSciX=rmSciX, customPar=customPar,...
# )
# points(x=x[Uncen == 0], y=yHigh[Uncen == 0],cex=cex,col=col)
#
# }
} else {

if(!("rResid" %in% names(localSample))){
eList <- makeAugmentedSample(eList)
localSample <- eList$Sample
}
yHigh <- localSample$rResid

if(stdResid){
yHigh <- yHigh/localSample$SE
}

yInfo <- generalAxis(x=yHigh, minVal=NA, maxVal=NA, tinyPlot=tinyPlot,padPercent=5)

genericEGRETDotPlot(x=x[Uncen == 1], y=yHigh[Uncen == 1],
xTicks=xInfo$ticks, yTicks=yInfo$ticks,hLine=TRUE,
xlim = c(xInfo$bottom, xInfo$top), ylim = c(yInfo$bottom, yInfo$top),
xlab = xLab, ylab = yLab, plotTitle=plotTitle,cex=cex,
log = "x", cex.axis=cex.axis,cex.main=cex.main, col=col,
tinyPlot=tinyPlot,rmSciX=rmSciX, customPar=customPar,...
)
points(x=x[Uncen == 0], y=yHigh[Uncen == 0],cex=cex,col=col)

}

if (!tinyPlot) mtext(title2,side=3,line=-1.5)
# invisible(eList)
invisible(eList)

}
Loading

0 comments on commit 491c050

Please sign in to comment.