Title: | Irucka Embry's Miscellaneous USGS Functions |
---|---|
Description: | A collection of Irucka Embry's miscellaneous USGS functions (processing .exp and .psf files, statistical error functions, "+" dyadic operator for use with NA, creating ADAPS and QW spreadsheet files, calculating saturated enthalpy). Irucka created these functions while a Cherokee Nation Technology Solutions (CNTS) United States Geological Survey (USGS) Contractor and/or USGS employee. |
Authors: | Irucka Embry [aut, cre], Anne Hoos [ctb], and Timothy H. Diehl [ctb] |
Maintainer: | Irucka Embry <[email protected]> |
License: | CC0 |
Version: | 0.9.1 |
Built: | 2024-11-10 03:14:13 UTC |
Source: | https://gitlab.com/iembry/ie2misc |
This function "defines a dyadic operator that will behave differently than
the "+" operator with regards to missing values". (Quote by 42- in Source
1 below). In this case, add x
and y
so that NA
is ignored
when x
is a number and x = NA
when x = 0
.
x %+na% y
x %+na% y
x |
numeric vector that does not contain any |
y |
numeric vector which contains at least one |
numeric vector resulting from the sum
of x
and y
Using ':=' in data.table to sum the values of two columns in R, ignoring NAs - Stack Overflow answered by 42- on Oct 28 2012. See https://stackoverflow.com/questions/13106645/using-in-data-table-to-sum-the-values-of-two-columns-in-r-ignoring-nas.
R - merge unequal regular (15 min intervals) and irregular time series within 10 minutes - Stack Overflow asked by iembry on Jan 12 2015 and edited by iembry on Feb 2 2015. See https://stackoverflow.com/questions/27911643/r-merge-unequal-regular-15-min-intervals-and-irregular-time-series-within-10.
r - How to not run an example using roxygen2? - Stack Overflow answered and edited by samkart on Jul 9 2017. (Also see the additional comments in response to the answer.) See https://stackoverflow.com/questions/12038160/how-to-not-run-an-example-using-roxygen2.
devtools - Issues in R package after CRAN asked to replace dontrun by dontrun - Stack Overflow answered by Hong Ooi on Sep 1 2020. (Also see the additional comments in response to the answer.) See https://stackoverflow.com/questions/63693563/issues-in-r-package-after-cran-asked-to-replace-dontrun-by-dontrun.
library("ie2misc") a <- 1:10 b <- c(98:106, NA) a %+na% b ## Not run: # See Source 3 and Source 4 # Note that if the vector with the NA value(s) is first, then an error # message will be displayed as now there are checks for both x and y. a <- 1:10 b <- c(98:106, NA) try(b %+na% a) ## End(Not run) a1 <- 1:12 b1 <- c(98:106, rep(NA, 3)) a1 %+na% b1 # using a matrix of the numeric vectors a and b mat1 <- matrix(data = c(a, b), nrow = length(b), ncol = 2, byrow = FALSE, dimnames = list(c(rep("", length(b))), c("a", "b"))) mat1[, 1] %+na% mat1[, 2] # using a data.frame of the numeric vectors a and b df1 <- data.frame(a, b) df1[, 1] %+na% df1[, 2] library("data.table") # using a data.table of the numeric vectors a and b a <- 1:10 b <- c(98:106, NA) df2 <- data.table(a, b) df2[, 1, with = FALSE][[1]] %+na% df2[, 2, with = FALSE][[1]]
library("ie2misc") a <- 1:10 b <- c(98:106, NA) a %+na% b ## Not run: # See Source 3 and Source 4 # Note that if the vector with the NA value(s) is first, then an error # message will be displayed as now there are checks for both x and y. a <- 1:10 b <- c(98:106, NA) try(b %+na% a) ## End(Not run) a1 <- 1:12 b1 <- c(98:106, rep(NA, 3)) a1 %+na% b1 # using a matrix of the numeric vectors a and b mat1 <- matrix(data = c(a, b), nrow = length(b), ncol = 2, byrow = FALSE, dimnames = list(c(rep("", length(b))), c("a", "b"))) mat1[, 1] %+na% mat1[, 2] # using a data.frame of the numeric vectors a and b df1 <- data.frame(a, b) df1[, 1] %+na% df1[, 2] library("data.table") # using a data.table of the numeric vectors a and b a <- 1:10 b <- c(98:106, NA) df2 <- data.table(a, b) df2[, 1, with = FALSE][[1]] %+na% df2[, 2, with = FALSE][[1]]
adaps, adaps2, and adapsBATCH process raw Automated Data Processing System (ADAPS) .rdb files from the U.S. Geological Survey (USGS) National Water Information System (NWIS). For these functions, it is only for continuous ADAPS data of the following parameters: discharge (00060), FNU turbidity (63680), and NTRU turbidity (63676 from 63680).
adaps( file = tk_choose.files(default = "", caption = "Select file(s) to open & hold down Ctrl to choose more than 1 file", multi = TRUE, filters = matrix(c("ADAPS file", ".rdb", "ADAPS file", ".RDB"), 4, 2, byrow = TRUE)), interactive = TRUE, overwrite = TRUE ) adapsBATCH( path = tk_choose.dir(caption = "Select directory with the ADAPS .rdb files"), overwrite = TRUE ) adaps2(file, overwrite = TRUE)
adaps( file = tk_choose.files(default = "", caption = "Select file(s) to open & hold down Ctrl to choose more than 1 file", multi = TRUE, filters = matrix(c("ADAPS file", ".rdb", "ADAPS file", ".RDB"), 4, 2, byrow = TRUE)), interactive = TRUE, overwrite = TRUE ) adapsBATCH( path = tk_choose.dir(caption = "Select directory with the ADAPS .rdb files"), overwrite = TRUE ) adaps2(file, overwrite = TRUE)
file |
Input ADAPS .rdb file(s) to be selected through a file dialog. |
interactive |
If interactive is |
overwrite |
If |
path |
Directory path of ADAPS .rdb files to be selected through a directory dialog. The user will be asked where to find the ADAPS .rdb files & then the user will be asked where to save the ADAPS .xlsx files. |
adaps function opens single or multiple raw ADAPS .rdb file(s) to modify the format and then exports the file(s) in .xlsx format. This is done for a single file or multiple files that the user selects with a file dialog.
adaps2 function opens a single raw ADAPS .rdb file to modify the format and then exports the file in .xlsx format. This is done for a single file that the user selects without a file dialog.
adapsBATCH function opens raw ADAPS .rdb files, from a directory, to modify the format and then exports the files in .xlsx format. This is done in a BATCH mode (whole directory of ADAPS .rdb files) using a directory dialog.
adaps, adaps2, and adapsBATCH functions perform the same processes on the raw ADAPS .rdb files: 1) Read in the file and remove the 1st 4 or 5 lines depending on whether NTRU data are present or not, 2) create 4 or 5 columns (depending on whether NTRU data are present or not) based on the 1st 4 or 5 lines, and 3) export the modified file in .xlsx format.
The following lines are representative of the .rdb format used in the files that these functions can operate on. Note: ntru may not be present. If so, then there will only be 3 cases of 16N in the last row. The last row will be removed in the final spreadsheet.
DATETIME | ght | |||
cfs | ||||
fnu | ||||
ntru | ||||
19D | 16N | 16N | 16N | 16N |
ADAPS .xlsx file(s)
r - How can I check if a file is empty? - Stack Overflow answered by Konrad Rudolph and edited by Geekuna Matata on Apr 23 2014. See https://stackoverflow.com/questions/23254002/how-can-i-check-if-a-file-is-empty.
r - Better error message for stopifnot? - Stack Overflow answered by Andrie on Dec 1 2011. See https://stackoverflow.com/questions/8343509/better-error-message-for-stopifnot.
RDocumentation: TclInterface tcltk. See https://www.rdocumentation.org/packages/tcltk/versions/3.3.1.
James Wettenhall & Philippe Grosjean, File Open/Save dialogs in R tcltk, December 01, 2015. See https://web.archive.org/web/20160521051207/http://www.sciviews.org/recipes/tcltk/TclTk-file-open-save-dialogs/. Retrieved thanks to the Internet Archive: Wayback Machine
r - read csv files and perform function, then bind together - Stack Overflow answered by bjoseph on Jan 8 2015. See https://stackoverflow.com/questions/27846715/read-csv-files-and-perform-function-then-bind-together.
multiple output filenames in R - Stack Overflow asked and edited by Gabelins on Feb 1 2013. See https://stackoverflow.com/questions/14651594/multiple-output-filenames-in-r.
r - Regex return file name, remove path and file extension - Stack Overflow answered and edited by Ananda Mahto on Feb 25 2013. See https://stackoverflow.com/questions/15073753/regex-return-file-name-remove-path-and-file-extension/15073919.
R help - How to change the default Date format for write.csv function? answered by William Dunlap on Dec 28, 2009. See https://hypatia.math.ethz.ch/pipermail/r-help/2009-December/416010.html.
RDocumentation: strptime base. See https://www.rdocumentation.org/packages/base/versions/3.3.1/topics/strptime.
convert date and time string to POSIX in R - Stack Overflow commented by cryo111 on Sep 18 2013. See https://stackoverflow.com/questions/18874400/convert-date-and-time-string-to-posix-in-r/18874863.
## Not run: library("ie2misc") # Example to check the input file format # Copy and paste the following code into the R console if you # wish to see the ADAPS .rdb input file format. # Note the number of lines and the row headings. file.show(system.file("extdata", "spring_creek_partial.rdb", package = "ie2misc"), title = paste("spring_creek_partial.rdb")) # opens the .rdb file using the default text editor # Examples to change (an) ADAPS .rdb file(s) interactively and # non-interactively adaps2(system.file("extdata", "spring_creek_partial.rdb", package = "ie2misc")) adaps() # default where interactive = TRUE # Follow the file dialog instructions adaps(interactive = FALSE) # Follow the file dialog instructions # Example to change a directory of ADAPS .rdb files adapsBATCH() # Follow the file dialog instructions ## End(Not run)
## Not run: library("ie2misc") # Example to check the input file format # Copy and paste the following code into the R console if you # wish to see the ADAPS .rdb input file format. # Note the number of lines and the row headings. file.show(system.file("extdata", "spring_creek_partial.rdb", package = "ie2misc"), title = paste("spring_creek_partial.rdb")) # opens the .rdb file using the default text editor # Examples to change (an) ADAPS .rdb file(s) interactively and # non-interactively adaps2(system.file("extdata", "spring_creek_partial.rdb", package = "ie2misc")) adaps() # default where interactive = TRUE # Follow the file dialog instructions adaps(interactive = FALSE) # Follow the file dialog instructions # Example to change a directory of ADAPS .rdb files adapsBATCH() # Follow the file dialog instructions ## End(Not run)
This function computes the "index of agreement (dr)".
dr(predicted, observed, na.rm = FALSE)
dr(predicted, observed, na.rm = FALSE)
predicted |
numeric vector that contains the predicted data points (1st parameter) |
observed |
numeric vector that contains the observed data points (2nd parameter) |
na.rm |
logical vector that determines whether the missing values should be removed or not. |
dr is expressed as
the "index of agreement (dr)"
the number of observations
the "model estimates or predictions"
the "pairwise-matched observations that are judged to be reliable"
the "true" mean of the observations
Note: Both P and O should have the same units.
The "index of agreement (dr)" is fully discussed in the Willmott reference.
"index of agreement (dr)" as a numeric vector. The default choice
is that any NA values will be kept (na.rm = FALSE). This can be
changed by specifying na.rm = TRUE
, such as dr(pre, obs, na.rm = TRUE)
.
Cort J. Willmott, Scott M. Robeson, and Kenji Matsuura, "A refined index of model performance", International Journal of Climatology, Volume 32, Issue 13, pages 2088-2094, 15 November 2012, article from ResearchGate: https://www.researchgate.net/publication/235961403_A_refined_index_of_model_performance.
mape
for mean absolute percent error (MAPE), mae
for
mean-absolute error (MAE), madstat
for mean-absolute deviation (MAD), vnse
for Nash-Sutcliffe model efficiency (NSE), and rmse
for root mean square error (RMSE).
library("ie2misc") obs <- 1:10 # observed pre <- 2:11 # predicted dr(pre, obs) library("rando") set_n(100) # makes the example reproducible obs1 <- r_norm(.seed = 400) # observed pre1 <- r_norm(.seed = 500) # predicted # using the vectors pre1 and obs1 dr(pre1, obs1) # using a matrix of the numeric vectors pre1 and obs1 mat1 <- matrix(data = c(obs1, pre1), nrow = length(pre1), ncol = 2, byrow = FALSE, dimnames = list(c(rep("", length(pre1))), c("Predicted", "Observed"))) dr(mat1[, 2], mat1[, 1]) # mat1[, 1] # observed values from column 1 of mat1 # mat1[, 2] # predicted values from column 2 of mat1 # using a data.frame of the numeric vectors pre1 and obs1 df1 <- data.frame(obs1, pre1) dr(df1[, 2], df1[, 1]) # df1[, 1] # observed values from column 1 of df1 # df1[, 2] # predicted values from column 2 of df1 library("data.table") # using a data.table of the numeric vectors pre1 and obs1 df2 <- data.table(obs1, pre1) dr(df2[, 2, with = FALSE][[1]], df2[, 1, with = FALSE][[1]]) # df2[, 1, with = FALSE][[1]] # observed values from column 1 of df2 # df2[, 2, with = FALSE][[1]] # predicted values from column 2 of df2
library("ie2misc") obs <- 1:10 # observed pre <- 2:11 # predicted dr(pre, obs) library("rando") set_n(100) # makes the example reproducible obs1 <- r_norm(.seed = 400) # observed pre1 <- r_norm(.seed = 500) # predicted # using the vectors pre1 and obs1 dr(pre1, obs1) # using a matrix of the numeric vectors pre1 and obs1 mat1 <- matrix(data = c(obs1, pre1), nrow = length(pre1), ncol = 2, byrow = FALSE, dimnames = list(c(rep("", length(pre1))), c("Predicted", "Observed"))) dr(mat1[, 2], mat1[, 1]) # mat1[, 1] # observed values from column 1 of mat1 # mat1[, 2] # predicted values from column 2 of mat1 # using a data.frame of the numeric vectors pre1 and obs1 df1 <- data.frame(obs1, pre1) dr(df1[, 2], df1[, 1]) # df1[, 1] # observed values from column 1 of df1 # df1[, 2] # predicted values from column 2 of df1 library("data.table") # using a data.table of the numeric vectors pre1 and obs1 df2 <- data.table(obs1, pre1) dr(df2[, 2, with = FALSE][[1]], df2[, 1, with = FALSE][[1]]) # df2[, 1, with = FALSE][[1]] # observed values from column 1 of df2 # df2[, 2, with = FALSE][[1]] # predicted values from column 2 of df2
expFileOutput and expFileOutputBATCH process raw .exp files generated by the USGS PeakFQ program (http://water.usgs.gov/software/PeakFQ/). The .exp output file from running PeakFQ is a shortened version of the longer narrative .prt file, more suitable for tabulating the model results than the .prt file; however, the information must be changed to a fixed field format.
expFileOutput( file = tk_choose.files(default = "", caption = "Select file(s) to open & hold down Ctrl to choose more than 1 file", multi = TRUE, filters = matrix(c("Text file", ".exp", "Text file", ".EXP"), 4, 2, byrow = TRUE)), output = c("csv", "xlsx", "both"), overwrite = TRUE ) expFileOutputBATCH( path = tk_choose.dir(caption = "Select the directory with the .exp files"), output = c("csv", "xlsx", "both"), overwrite = TRUE )
expFileOutput( file = tk_choose.files(default = "", caption = "Select file(s) to open & hold down Ctrl to choose more than 1 file", multi = TRUE, filters = matrix(c("Text file", ".exp", "Text file", ".EXP"), 4, 2, byrow = TRUE)), output = c("csv", "xlsx", "both"), overwrite = TRUE ) expFileOutputBATCH( path = tk_choose.dir(caption = "Select the directory with the .exp files"), output = c("csv", "xlsx", "both"), overwrite = TRUE )
file |
Input .exp file(s), using a file dialog, to obtain 1) the goodness-of-fit and trend results & 2) the exceedance probability values. |
output |
The exported format for each set of results (options are .csv, .xlsx, and both file types). |
overwrite |
logical vector that determines whether the existing should be overwritten or not. |
path |
Directory path of .exp files, to be selected through a directory dialog, to obtain 1) the goodness-of-fit and trend results & 2) the exceedance probability values. The user will be asked where to find the .exp files & then the user will be asked where to save the results files. |
expFileOutput converts the user-selected .exp file to 1) a tab-delimited .csv file for a single station with the goodness-of-fit and trend results (all station information exists in a single row) and 2) a tab-delimited .csv file for a single station with the exceedance probability values on each row. For the .xlsx file, tab 1 contains the goodness-of-fit and trend results and tab 2 contains the exceedance probability values.
expFileOutputBATCH converts the user-selected directory of .exp files into 1) a tab-delimited .csv file as a single table, with one record for each station, with the goodness-of-fit and trend results and 2) a tab-delimited .csv file as a single table, with a set of records for each station, with the exceedance probability values on each row. For the .xlsx file, tab 1 contains the goodness-of-fit and trend results and tab 2 contains the exceedance probability values.
In the Examples section, there is R code for the user to view the example .exp file.
.csv, .xlsx, or both file types with 1) the goodness-of-fit and trend results & 2) the exceedance probability values for individual sites in separate files. Currently, in the BATCH mode, all stations in the given directory are combined in a single data.frame with both 1) and 2).
Irucka Embry, Anne Hoos
r - How can I check if a file is empty? - Stack Overflow answered by Konrad Rudolph and edited by Geekuna Matata on Apr 23 2014. See https://stackoverflow.com/questions/23254002/how-can-i-check-if-a-file-is-empty.
r - Better error message for stopifnot? - Stack Overflow answered by Andrie on Dec 1 2011. See https://stackoverflow.com/questions/8343509/better-error-message-for-stopifnot.
RDocumentation: TclInterface tcltk. See https://www.rdocumentation.org/packages/tcltk/versions/3.3.1.
James Wettenhall & Philippe Grosjean, File Open/Save dialogs in R tcltk, December 01, 2015. See https://web.archive.org/web/20160521051207/http://www.sciviews.org/recipes/tcltk/TclTk-file-open-save-dialogs/. Retrieved thanks to the Internet Archive: Wayback Machine
r - read csv files and perform function, then bind together - Stack Overflow answered by bjoseph on Jan 8 2015. See https://stackoverflow.com/questions/27846715/read-csv-files-and-perform-function-then-bind-together.
r - Convert column classes in data.table - Stack Overflow answered by Matt Dowle on Dec 27 2013. See https://stackoverflow.com/questions/7813578/convert-column-classes-in-data-table.
r - Transpose rows to columns and remove only NAs in resulting columns - Stack Overflow answered by David Arenburg on April 22, 2014. See https://stackoverflow.com/questions/23225977/transpose-rows-to-columns-and-remove-only-nas-in-resulting-columns.
r - Add a Column to a Dataframe From a List of Values - Stack Overflow answered by Matthew Plourde on Jun 21 2012. See https://stackoverflow.com/questions/11130037/add-a-column-to-a-dataframe-from-a-list-of-values/11130178.
r - Why does is.vector() return TRUE for list? - Stack Overflow answered by Andrie on May 17 2011. See https://stackoverflow.com/questions/6032772/why-does-is-vector-return-true-for-list/6032909.
warnings - How do I get rid of the NOTE's generated by R CMD check when using for example ddply in my package? - Stack Overflow answered by shadow on Mar 4 2015 and edited by shadow on Mar 5 2015. See https://stackoverflow.com/questions/28851812/how-do-i-get-rid-of-the-notes-generated-by-r-cmd-check-when-using-for-example-d.
multiple output filenames in R - Stack Overflow asked and edited by Gabelins on Feb 1 2013. See https://stackoverflow.com/questions/14651594/multiple-output-filenames-in-r.
r - Regex return file name, remove path and file extension - Stack Overflow answered and edited by Ananda Mahto on Feb 25 20134. See https://stackoverflow.com/questions/15073753/regex-return-file-name-remove-path-and-file-extension/15073919.
## Not run: # Example to check the input file format library("ie2misc") # Copy and paste the following code into the R console if you # wish to see the .exp input file format. # Note the number of lines and the row headings. file.show(system.file("extdata", "01110000_PEAK_WATSTORE.EXP", package = "ie2misc"), title = paste("01110000_PEAK_WATSTORE.EXP")) # opens the .exp file using the default text editor or within RStudio # Examples to show you different output cases expFileOutput(output = "csv") # returns .csv files # Follow the file dialog instructions expFileOutput(output = "xlsx") # returns .xlsx files # Follow the file dialog instructions expFileOutput(output = "both") # returns .csv and .xlsx files # Follow the file dialog instructions # Examples to show you different output cases (BATCH) expFileOutputBATCH(output = "csv") # returns .csv files # Follow the file dialog instructions expFileOutputBATCH(output = "xlsx") # returns .xlsx files # Follow the file dialog instructions expFileOutputBATCH(output = "both") # returns .csv and .xlsx files # Follow the file dialog instructions ## End(Not run)
## Not run: # Example to check the input file format library("ie2misc") # Copy and paste the following code into the R console if you # wish to see the .exp input file format. # Note the number of lines and the row headings. file.show(system.file("extdata", "01110000_PEAK_WATSTORE.EXP", package = "ie2misc"), title = paste("01110000_PEAK_WATSTORE.EXP")) # opens the .exp file using the default text editor or within RStudio # Examples to show you different output cases expFileOutput(output = "csv") # returns .csv files # Follow the file dialog instructions expFileOutput(output = "xlsx") # returns .xlsx files # Follow the file dialog instructions expFileOutput(output = "both") # returns .csv and .xlsx files # Follow the file dialog instructions # Examples to show you different output cases (BATCH) expFileOutputBATCH(output = "csv") # returns .csv files # Follow the file dialog instructions expFileOutputBATCH(output = "xlsx") # returns .xlsx files # Follow the file dialog instructions expFileOutputBATCH(output = "both") # returns .csv and .xlsx files # Follow the file dialog instructions ## End(Not run)
This function computes the mean-absolute deviation (MAD) – "the average of the magnitudes of the errors or deviations."
madstat(observed, na.rm = FALSE)
madstat(observed, na.rm = FALSE)
observed |
numeric vector, matrix, data.frame, or data.table that contains the observed data points. |
na.rm |
logical vector that determines whether the missing values should be removed or not. |
MAD is expressed as
the number of observations
the "pairwise-matched observations that are judged to be reliable"
the "true" mean of the observations
Reference 1 fully discusses MAD, while Reference 2 provides the formula used to calculate the MAD.
mean-absolute deviation (MAD) as a numeric vector
or a named
numeric vector if using a named object (matrix
, data.frame
,
or data.table
). MAD has the same units as the observed values. The default choice is that any NA values
will be kept (na.rm = FALSE
). This can be changed by specifying na.rm = TRUE
, such as
madstat(obs, na.rm = TRUE)
.
Cort J. Willmott, Kenji Matsuura, and Scott M. Robeson, "Ambiguities inherent in sums-of-squares-based error statistics", Atmospheric Environment, vol. 43, no. 3, pp. 749-752, 2009, https://www.sciencedirect.com/science/article/pii/S1352231008009564.
Cort J. Willmott, Scott M. Robeson, and Kenji Matsuura, "Short Communication: A refined index of model performance", International Journal of Climatology, Volume 32, Issue 13, pages 2088-2094, 15 November 2012, article from ResearchGate: https://www.researchgate.net/publication/235961403_A_refined_index_of_model_performance.
Nathabandu T. Kottegoda and Renzo Rosso, Statistics, Probability, and Reliability for Civil and Environmental Engineers, New York City, New York: The McGraw-Hill Companies, Inc., 1997, page 15.
mad
for median absolute deviation (MAD)
mape
for mean absolute percent error (MAPE), mae
for
mean-absolute error (MAE), dr
for "index of agreement (dr)", vnse
for Nash-Sutcliffe model efficiency (NSE), and rmse
for
root mean square error (RMSE).
library("ie2misc") # Example 1.18 from Kottegoda (page 15) obs <- c(50, 56, 42, 53, 49) # annual rainfall in cm madstat(obs) library("rando") set_n(100) # makes the example reproducible obs1 <- r_norm(.seed = 300) # observed # using the numeric vector obs1 madstat(obs1) # using a matrix of the numeric vector obs1 mat1 <- matrix(data = obs1, nrow = length(obs1), ncol = 1, byrow = FALSE, dimnames = list(c(rep("", length(obs1))), "Observed")) madstat(mat1) # using a data.frame of the numeric vector obs1 df1 <- data.frame(obs1) madstat(df1) library("data.table") # using a data.table of the numeric vector obs1 df2 <- data.table(obs1) madstat(df2)
library("ie2misc") # Example 1.18 from Kottegoda (page 15) obs <- c(50, 56, 42, 53, 49) # annual rainfall in cm madstat(obs) library("rando") set_n(100) # makes the example reproducible obs1 <- r_norm(.seed = 300) # observed # using the numeric vector obs1 madstat(obs1) # using a matrix of the numeric vector obs1 mat1 <- matrix(data = obs1, nrow = length(obs1), ncol = 1, byrow = FALSE, dimnames = list(c(rep("", length(obs1))), "Observed")) madstat(mat1) # using a data.frame of the numeric vector obs1 df1 <- data.frame(obs1) madstat(df1) library("data.table") # using a data.table of the numeric vector obs1 df2 <- data.table(obs1) madstat(df2)
This function computes the mean-absolute error (MAE).
mae(predicted, observed, na.rm = FALSE)
mae(predicted, observed, na.rm = FALSE)
predicted |
numeric vector that contains the model predicted data points (1st parameter) |
observed |
numeric vector that contains the observed data points (2nd parameter) |
na.rm |
logical vector that determines whether the missing values should be removed or not. |
(MAE) is expressed as
the number of observations
the "model estimates or predictions"
the "thought-to-be reliable and pairwise matched observations"
MAE is fully discussed in the Willmott reference, including a comparison to root mean square error (RMSE).
mean-absolute error (MAE) as a numeric vector using the same
units as the given variables. The default choice is that any NA values
will be kept (na.rm = FALSE
). This can be changed by
specifying na.rm = TRUE
, such as mae(pre, obs, na.rm = TRUE)
.
Cort J. Willmott and Kenji Matsuura, "Advantages of the mean-absolute error (MAE) over the root mean square error (RMSE) in assessing average model performance", Climate Research, Vol. 30: 79-82, 2005, https://web.archive.org/web/20230119121852/climate.geog.udel.edu/~climate/publication_html/Pdf/WM_CR_05.pdf. Retrieved thanks to the Internet Archive: Wayback Machine
mape
for mean absolute percent error (MAPE), madstat
for
mean-absolute deviation (MAD), dr
for "index of agreement (dr)", vnse
for Nash-Sutcliffe model efficiency (NSE), and rmse
for
root mean square error (RMSE).
library("ie2misc") obs <- 1:10 # observed pre <- 2:11 # predicted mae(pre, obs) library("rando") set_n(100) # makes the example reproducible obs1 <- r_norm(.seed = 103) # observed pre1 <- r_norm(.seed = 102) # predicted # using the vectors pre1 and obs1 mae(pre1, obs1) # using a matrix of the numeric vectors pre1 and obs1 mat1 <- matrix(data = c(obs1, pre1), nrow = length(pre1), ncol = 2, byrow = FALSE, dimnames = list(c(rep("", length(pre1))), c("Predicted", "Observed"))) mae(mat1[, 2], mat1[, 1]) # mat1[, 1] # observed values from column 1 of mat1 # mat1[, 2] # predicted values from column 2 of mat1 # using a data.frame of the numeric vectors pre1 and obs1 df1 <- data.frame(obs1, pre1) mae(df1[, 2], df1[, 1]) # df1[, 1] # observed values from column 1 of df1 # df1[, 2] # predicted values from column 2 of df1 library("data.table") # using a data.table of the numeric vectors pre1 and obs1 df2 <- data.table(obs1, pre1) mae(df2[, 2, with = FALSE][[1]], df2[, 1, with = FALSE][[1]]) # df2[, 1, with = FALSE][[1]] # observed values from column 1 of df2 # df2[, 2, with = FALSE][[1]] # predicted values from column 2 of df2
library("ie2misc") obs <- 1:10 # observed pre <- 2:11 # predicted mae(pre, obs) library("rando") set_n(100) # makes the example reproducible obs1 <- r_norm(.seed = 103) # observed pre1 <- r_norm(.seed = 102) # predicted # using the vectors pre1 and obs1 mae(pre1, obs1) # using a matrix of the numeric vectors pre1 and obs1 mat1 <- matrix(data = c(obs1, pre1), nrow = length(pre1), ncol = 2, byrow = FALSE, dimnames = list(c(rep("", length(pre1))), c("Predicted", "Observed"))) mae(mat1[, 2], mat1[, 1]) # mat1[, 1] # observed values from column 1 of mat1 # mat1[, 2] # predicted values from column 2 of mat1 # using a data.frame of the numeric vectors pre1 and obs1 df1 <- data.frame(obs1, pre1) mae(df1[, 2], df1[, 1]) # df1[, 1] # observed values from column 1 of df1 # df1[, 2] # predicted values from column 2 of df1 library("data.table") # using a data.table of the numeric vectors pre1 and obs1 df2 <- data.table(obs1, pre1) mae(df2[, 2, with = FALSE][[1]], df2[, 1, with = FALSE][[1]]) # df2[, 1, with = FALSE][[1]] # observed values from column 1 of df2 # df2[, 2, with = FALSE][[1]] # predicted values from column 2 of df2
This function computes the mean absolute percent error (MAPE).
mape(predicted, observed, na.rm = FALSE)
mape(predicted, observed, na.rm = FALSE)
predicted |
numeric vector that contains the predicted data points (1st parameter) |
observed |
numeric vector that contains the observed data points (2nd parameter) |
na.rm |
logical vector that determines whether the missing values should be removed or not. |
MAPE is expressed as
the number of observations
the observations
the predictions
Below are some points to remember about MAPE from the Ji reference:
MAPE is "a measure to validate forecast models",
MAPE is "a standardized value and is independent of the unit of the measurement",
MAPE is "meaningful only if all values are positive",
MAPE is "unstable when values are near zero", and
"If X and Y are interchanged, the MAPE will result in a different value."
mean absolute percent error (MAPE) as a numeric vector. The default
choice is that any NA values will be kept (na.rm = FALSE
). This can be
changed by specifying na.rm = TRUE
, such as mape(pre, obs, na.rm = TRUE)
.
Lei Ji and Kevin Gallo, "An Agreement Coefficient for Image Comparison", Photogrammetric Engineering & Remote Sensing, Vol. 72, No. 7, July 2006, p. 823-8335, https://www.ingentaconnect.com/content/asprs/pers/2006/00000072/00000007/art00006.
mae
for mean-absolute error (MAE), madstat
for
mean-absolute deviation (MAD), dr
for "index of agreement (dr)", vnse
for Nash-Sutcliffe model efficiency (NSE), and rmse
for
root mean square error (RMSE).
library("ie2misc") obs <- 1:10 # observed pre <- 2:11 # predicted mape(pre, obs) library("rando") set_n(100) # makes the example reproducible obs1 <- r_norm(.seed = 109) # observed pre1 <- r_norm(.seed = 124) # predicted # using the vectors pre1 and obs1 mape(pre1, obs1) # using a matrix of the numeric vectors pre1 and obs1 mat1 <- matrix(data = c(obs1, pre1), nrow = length(pre1), ncol = 2, byrow = FALSE, dimnames = list(c(rep("", length(pre1))), c("Predicted", "Observed"))) mape(mat1[, 2], mat1[, 1]) # mat1[, 1] # observed values from column 1 of mat1 # mat1[, 2] # predicted values from column 2 of mat1 # using a data.frame of the numeric vectors pre1 and obs1 df1 <- data.frame(obs1, pre1) mape(df1[, 2], df1[, 1]) # df1[, 1] # observed values from column 1 of df1 # df1[, 2] # predicted values from column 2 of df1 library("data.table") # using a data.table of the numeric vectors pre1 and obs1 df2 <- data.table(obs1, pre1) mape(df2[, 2, with = FALSE][[1]], df2[, 1, with = FALSE][[1]]) # df2[, 1, with = FALSE][[1]] # observed values from column 1 of df2 # df2[, 2, with = FALSE][[1]] # predicted values from column 2 of df2
library("ie2misc") obs <- 1:10 # observed pre <- 2:11 # predicted mape(pre, obs) library("rando") set_n(100) # makes the example reproducible obs1 <- r_norm(.seed = 109) # observed pre1 <- r_norm(.seed = 124) # predicted # using the vectors pre1 and obs1 mape(pre1, obs1) # using a matrix of the numeric vectors pre1 and obs1 mat1 <- matrix(data = c(obs1, pre1), nrow = length(pre1), ncol = 2, byrow = FALSE, dimnames = list(c(rep("", length(pre1))), c("Predicted", "Observed"))) mape(mat1[, 2], mat1[, 1]) # mat1[, 1] # observed values from column 1 of mat1 # mat1[, 2] # predicted values from column 2 of mat1 # using a data.frame of the numeric vectors pre1 and obs1 df1 <- data.frame(obs1, pre1) mape(df1[, 2], df1[, 1]) # df1[, 1] # observed values from column 1 of df1 # df1[, 2] # predicted values from column 2 of df1 library("data.table") # using a data.table of the numeric vectors pre1 and obs1 df2 <- data.table(obs1, pre1) mape(df2[, 2, with = FALSE][[1]], df2[, 1, with = FALSE][[1]]) # df2[, 1, with = FALSE][[1]] # observed values from column 1 of df2 # df2[, 2, with = FALSE][[1]] # predicted values from column 2 of df2
The .psf file is a script file that records the specifications used to run the USGS PeakFQ program (http://water.usgs.gov/software/PeakFQ/) for a station. psfFileChange and psfFileChangeBATCH modify the original .psf settings.
psfFileChange( file = tk_choose.files(default = "", caption = "Select file(s) to open & hold down Ctrl to choose more than 1 file", multi = TRUE, filters = matrix(c("Text file", ".psf", "Text file", ".PSF"), 4, 2, byrow = TRUE)), interactive = TRUE ) psfFileChangeBATCH( path = tk_choose.dir(caption = "Select the directory with the .psf files") )
psfFileChange( file = tk_choose.files(default = "", caption = "Select file(s) to open & hold down Ctrl to choose more than 1 file", multi = TRUE, filters = matrix(c("Text file", ".psf", "Text file", ".PSF"), 4, 2, byrow = TRUE)), interactive = TRUE ) psfFileChangeBATCH( path = tk_choose.dir(caption = "Select the directory with the .psf files") )
file |
Input .psf file(s) to change specific SkewSE, GenSkew, and SkewOpt information to be selected through a file dialog. |
interactive |
If interactive is TRUE, then the user will select the
filenames(s) to use for saving with the file dialog. In order to select
more than one file, the user must hold down the Ctrl (Control) button
while mouse clicking the chosen files. If interactive is |
path |
Directory path of .psf files, to be selected through a directory dialog, to change specific SkewSE, GenSkew, and SkewOpt information. The user will be asked where to find the .psf files & then the user will be asked where to save the revised .psf files. |
psfFileChange searches for a character vector of patterns (SkewSE, GenSkew, and SkewOpt) in single or multiple .psf file(s) to replace. If the patterns are missing, then the patterns are added to the file(s). This is done for a single file or multiple files that the user selects. Although these changes are currently pre-determined, future versions may allow the user to change particular settings (for example, how outliers are handled).
psfFileChangeBATCH searches for a character vector of patterns (SkewSE, GenSkew, and SkewOpt) in a directory of .psf files to replace. If the patterns are missing, then the patterns are added to the files. This is done in a BATCH mode (whole directory of .psf files). Although these changes are currently pre-determined, future versions may allow the user to change particular settings (for example, how outliers are handled).
Revised .psf text file(s)
Irucka Embry, Anne Hoos
r - How can I check if a file is empty? - Stack Overflow answered by Konrad Rudolph and edited by Geekuna Matata on Apr 23 2014. See https://stackoverflow.com/questions/23254002/how-can-i-check-if-a-file-is-empty.
r - Better error message for stopifnot? - Stack Overflow answered by Andrie on Dec 1 2011. See https://stackoverflow.com/questions/8343509/better-error-message-for-stopifnot.
RDocumentation: TclInterface tcltk. See https://www.rdocumentation.org/packages/tcltk/versions/3.3.1.
James Wettenhall & Philippe Grosjean, File Open/Save dialogs in R tcltk, December 01, 2015. See https://web.archive.org/web/20160521051207/http://www.sciviews.org/recipes/tcltk/TclTk-file-open-save-dialogs/. Retrieved thanks to the Internet Archive: Wayback Machine
Replacing nth line in a text file in R - Stack Overflow answered by Spacedman on Aug 1 2012. See https://stackoverflow.com/questions/11756353/replacing-nth-line-in-a-text-file-in-r.
r - read csv files and perform function, then bind together - Stack Overflow answered by bjoseph on Jan 8 2015. See https://stackoverflow.com/questions/27846715/read-csv-files-and-perform-function-then-bind-together.
multiple output filenames in R - Stack Overflow asked and edited by Gabelins on Feb 1 2013. See https://stackoverflow.com/questions/14651594/multiple-output-filenames-in-r.
r - Regex return file name, remove path and file extension - Stack Overflow answered and edited by Ananda Mahto on Feb 25 20134. See https://stackoverflow.com/questions/15073753/regex-return-file-name-remove-path-and-file-extension/15073919.
## Not run: # Examples to change (a) .psf file(s) interactively and non-interactively library("ie2misc") psfFileChange() # default where interactive = TRUE # Follow the file dialog instructions # These are the rows that have been added or changed as a result of this function: # SkewSE 0.361804179633127 # GenSkew 0.0104293904 # SkewOpt Weighted psfFileChange(interactive = FALSE) # Follow the file dialog instructions # These are the rows that have been added or changed as a result of this function: # SkewSE 0.361804179633127 # GenSkew 0.0104293904 # SkewOpt Weighted psfFileChangeBATCH() # Follow the file dialog instructions # These are the rows that have been added or changed as a result of this function: # SkewSE 0.361804179633127 # GenSkew 0.0104293904 # SkewOpt Weighted ## End(Not run)
## Not run: # Examples to change (a) .psf file(s) interactively and non-interactively library("ie2misc") psfFileChange() # default where interactive = TRUE # Follow the file dialog instructions # These are the rows that have been added or changed as a result of this function: # SkewSE 0.361804179633127 # GenSkew 0.0104293904 # SkewOpt Weighted psfFileChange(interactive = FALSE) # Follow the file dialog instructions # These are the rows that have been added or changed as a result of this function: # SkewSE 0.361804179633127 # GenSkew 0.0104293904 # SkewOpt Weighted psfFileChangeBATCH() # Follow the file dialog instructions # These are the rows that have been added or changed as a result of this function: # SkewSE 0.361804179633127 # GenSkew 0.0104293904 # SkewOpt Weighted ## End(Not run)
qw, qw2 and qwBATCH process raw QW files. The QW files can contain "selected water-quality data for stations in the U.S. Geological Survey (USGS) National Water Information System (NWIS) water-quality database. The data you have secured from the USGS NWISWeb database may include data that have not received Director's approval and as such are provisional and subject to revision."
qw( file = tk_choose.files(default = "", caption = "Select file(s) to open & hold down Ctrl to choose more than 1 file", multi = TRUE, filters = matrix(c("QW file", "*", "QW file", ".rdb", "QW file", ".RDB"), 6, 2, byrow = TRUE)), interactive = TRUE, overwrite = TRUE ) qwBATCH( path = tk_choose.dir(caption = "Select directory with the QW files"), pattern = "*.rdb", overwrite = TRUE ) qw2(file, overwrite = TRUE)
qw( file = tk_choose.files(default = "", caption = "Select file(s) to open & hold down Ctrl to choose more than 1 file", multi = TRUE, filters = matrix(c("QW file", "*", "QW file", ".rdb", "QW file", ".RDB"), 6, 2, byrow = TRUE)), interactive = TRUE, overwrite = TRUE ) qwBATCH( path = tk_choose.dir(caption = "Select directory with the QW files"), pattern = "*.rdb", overwrite = TRUE ) qw2(file, overwrite = TRUE)
file |
Input QW file(s) to be selected through a file dialog. |
interactive |
If interactive is |
overwrite |
If |
path |
Directory path of QW files to be selected through a directory dialog. The user will be asked where to find the QW files & then the user will be asked where to save the QW .xlsx files. |
pattern |
The default pattern is |
qw function opens a single or multiple raw, QW file(s) to modify the format and then exports the file(s) in .xlsx format. This is done for a single file or multiple files that the user selects with a file dialog.
qw2 function opens a single QW file to modify the format and then exports the file in .xlsx format. This is done for a single file that the user selects without a file dialog.
qwBATCH function opens raw QW files, from a directory, to modify the format and then exports the files in .xlsx format. This is done in a BATCH mode (whole directory of QW files) using a directory dialog.
qw, qw2 and qwBATCH functions perform the same processes on the raw QW files: 1) remove the first row, 2) create a single column with the Date and Time rather than 2 separate Date and Time columns (if needed), and 3) exports the QW file as a spreadsheet with three sheets (sheet1 corrects the timezones for Daylight Savings Times, sheet2 includes everything except for the meta data, and sheet3 provides the meta data from the top of the file). The supported US timezones are the following: Eastern, Central, Mountain, Pacific, Alaska, Hawai'i, and Atlantic (Puerto Rico and US Virgin Islands).
QW .xlsx file(s).
r - How can I check if a file is empty? - Stack Overflow answered by Konrad Rudolph and edited by Geekuna Matata on Apr 23 2014. See https://stackoverflow.com/questions/23254002/how-can-i-check-if-a-file-is-empty.
r - Better error message for stopifnot? - Stack Overflow answered by Andrie on Dec 1 2011. See https://stackoverflow.com/questions/8343509/better-error-message-for-stopifnot.
RDocumentation: TclInterface tcltk. See https://www.rdocumentation.org/packages/tcltk/versions/3.3.1.
James Wettenhall & Philippe Grosjean, File Open/Save dialogs in R tcltk, December 01, 2015. See https://web.archive.org/web/20160521051207/http://www.sciviews.org/recipes/tcltk/TclTk-file-open-save-dialogs/. Retrieved thanks to the Internet Archive: Wayback Machine
r - read csv files and perform function, then bind together - Stack Overflow answered by bjoseph on Jan 8 2015. See https://stackoverflow.com/questions/27846715/read-csv-files-and-perform-function-then-bind-together.
r - Convert column classes in data.table - Stack Overflow answered by Matt Dowle on Dec 27 2013. See https://stackoverflow.com/questions/7813578/convert-column-classes-in-data-table.
Does column exist and how to rearrange columns in R data frame - Stack Overflow answered and edited by Peter McMahan on Aug 2 2009. See https://stackoverflow.com/questions/1177919/does-column-exist-and-how-to-rearrange-columns-in-r-data-frame.
time - Dealing with timestamps in R - Stack Overflow answered by Dirk Eddelbuettel on Dec 26 2009. See https://stackoverflow.com/questions/1962278/dealing-with-timestamps-in-r/1962336.
R help - How to change the default Date format for write.csv function? answered by William Dunlap on Dec 28, 2009. See https://hypatia.math.ethz.ch/pipermail/r-help/2009-December/416010.html.
RDocumentation: strptime base. See https://www.rdocumentation.org/packages/base/versions/3.3.1/topics/strptime.
National Water Information System: Help System Time Zone Codes. See https://help.waterdata.usgs.gov/code/tz_query?fmt=html.
multiple output filenames in R - Stack Overflow asked and edited by Gabelins on Feb 1 2013. See https://stackoverflow.com/questions/14651594/multiple-output-filenames-in-r.
r - Regex return file name, remove path and file extension - Stack Overflow answered and edited by Ananda Mahto on Feb 25 2013. See https://stackoverflow.com/questions/15073753/regex-return-file-name-remove-path-and-file-extension/15073919.
warnings - How do I get rid of the NOTE's generated by R CMD check when using for example ddply in my package? - Stack Overflow answered by shadow on Mar 4 2015 and edited by shadow on Mar 5 2015. See https://stackoverflow.com/questions/28851812/how-do-i-get-rid-of-the-notes-generated-by-r-cmd-check-when-using-for-example-d.
trinker/qdapRegex - dictionary_maintenance.R. See https://github.com/trinker/qdapRegex/blob/master/inst/dictionary_maintenance.R.
excel - Interconverting POSIXct and numeric in R - Stack Overflow by LauraS on May 6 2016. See https://stackoverflow.com/questions/37078772/interconverting-posixct-and-numeric-in-r.
convert date and time string to POSIX in R - Stack Overflow commented by cryo111 on Sep 18 2013. See https://stackoverflow.com/questions/18874400/convert-date-and-time-string-to-posix-in-r/18874863.
## Not run: library("ie2misc") # Examples to change (an) QW file(s) interactively and non-interactively file1 <- "https://waterdata.usgs.gov/nwis/dv?cb_00060=on&format=rdb&site_no=03584500" file2 <- "&period=&begin_date=1904-07-01&end_date=2016-06-22" file3 <- paste0(file1, file2) # used to truncate the file name qw2(file3) # USGS 03584500 ELK RIVER NEAR PROSPECT, TN # Discharge, cubic feet per second (Mean) qw() # default where interactive = TRUE # Follow the file dialog instructions qw(interactive = FALSE) # Follow the file dialog instructions # Example to change QW files in batch mode qwBATCH() # Follow the file dialog instructions ## End(Not run)
## Not run: library("ie2misc") # Examples to change (an) QW file(s) interactively and non-interactively file1 <- "https://waterdata.usgs.gov/nwis/dv?cb_00060=on&format=rdb&site_no=03584500" file2 <- "&period=&begin_date=1904-07-01&end_date=2016-06-22" file3 <- paste0(file1, file2) # used to truncate the file name qw2(file3) # USGS 03584500 ELK RIVER NEAR PROSPECT, TN # Discharge, cubic feet per second (Mean) qw() # default where interactive = TRUE # Follow the file dialog instructions qw(interactive = FALSE) # Follow the file dialog instructions # Example to change QW files in batch mode qwBATCH() # Follow the file dialog instructions ## End(Not run)
This function computes the root mean square error (RMSE).
rmse(predicted, observed, na.rm = FALSE)
rmse(predicted, observed, na.rm = FALSE)
predicted |
numeric vector that contains the predicted data points (1st parameter) |
observed |
numeric vector that contains the observed data points (2nd parameter) |
na.rm |
logical vector that determines whether the missing values should be removed or not. |
RMSE is expressed as
the number of observations
the "model estimates or predictions"
the "thought-to-be reliable and pairwise matched observations"
RMSE is fully discussed in the Willmott reference, including a comparison to mean-absolute error (MAE).
mean absolute percent error (RMSE) as a numeric vector. The default
choice is that any NA values will be kept (na.rm = FALSE
). This can be
changed by specifying na.rm = TRUE
, such as rmse(pre, obs, na.rm = TRUE)
.
Cort J. Willmott and Kenji Matsuura, "Advantages of the mean-absolute error (MAE) over the root mean square error (RMSE) in assessing average model performance", Climate Research, Vol. 30: 79-82, 2005, https://web.archive.org/web/20230119121852/climate.geog.udel.edu/~climate/publication_html/Pdf/WM_CR_05.pdf. Retrieved thanks to the Internet Archive: Wayback Machine
mape
for mean absolute percent error (MAPE), mae
for
mean-absolute error (MAE), madstat
for mean-absolute deviation (MAD), dr
for "index of agreement (dr)", and vnse
for
Nash-Sutcliffe model efficiency (NSE).
library("ie2misc") obs <- 1:10 # observed pre <- 2:11 # predicted rmse(pre, obs) library("rando") set_n(100) # makes the example reproducible obs1 <- r_norm(.seed = 209) # observed pre1 <- r_norm(.seed = 224) # predicted # using the vectors pre1 and obs1 rmse(pre1, obs1) # using a matrix of the numeric vectors pre1 and obs1 mat1 <- matrix(data = c(obs1, pre1), nrow = length(pre1), ncol = 2, byrow = FALSE, dimnames = list(c(rep("", length(pre1))), c("Predicted", "Observed"))) rmse(mat1[, 2], mat1[, 1]) # mat1[, 1] # observed values from column 1 of mat1 # mat1[, 2] # predicted values from column 2 of mat1 # using a data.frame of the numeric vectors pre1 and obs1 df1 <- data.frame(obs1, pre1) rmse(df1[, 2], df1[, 1]) # df1[, 1] # observed values from column 1 of df1 # df1[, 2] # predicted values from column 2 of df1 library("data.table") # using a data.table of the numeric vectors pre1 and obs1 df2 <- data.table(obs1, pre1) rmse(df2[, 2, with = FALSE][[1]], df2[, 1, with = FALSE][[1]]) # df2[, 1, with = FALSE][[1]] # observed values from column 1 of df2 # df2[, 2, with = FALSE][[1]] # predicted values from column 2 of df2
library("ie2misc") obs <- 1:10 # observed pre <- 2:11 # predicted rmse(pre, obs) library("rando") set_n(100) # makes the example reproducible obs1 <- r_norm(.seed = 209) # observed pre1 <- r_norm(.seed = 224) # predicted # using the vectors pre1 and obs1 rmse(pre1, obs1) # using a matrix of the numeric vectors pre1 and obs1 mat1 <- matrix(data = c(obs1, pre1), nrow = length(pre1), ncol = 2, byrow = FALSE, dimnames = list(c(rep("", length(pre1))), c("Predicted", "Observed"))) rmse(mat1[, 2], mat1[, 1]) # mat1[, 1] # observed values from column 1 of mat1 # mat1[, 2] # predicted values from column 2 of mat1 # using a data.frame of the numeric vectors pre1 and obs1 df1 <- data.frame(obs1, pre1) rmse(df1[, 2], df1[, 1]) # df1[, 1] # observed values from column 1 of df1 # df1[, 2] # predicted values from column 2 of df1 library("data.table") # using a data.table of the numeric vectors pre1 and obs1 df2 <- data.table(obs1, pre1) rmse(df2[, 2, with = FALSE][[1]], df2[, 1, with = FALSE][[1]]) # df2[, 1, with = FALSE][[1]] # observed values from column 1 of df2 # df2[, 2, with = FALSE][[1]] # predicted values from column 2 of df2
sat_enthalpy and sat_enthalpy2 solve for the saturated enthalpy (H) given an elevation in feet and a range of temperature values (degrees F).
sat_enthalpy( file = tk_choose.files(default = "", caption = "Select file to open", multi = FALSE, filters = matrix(c("Comma-separated value file", ".csv", "MS Excel spreadsheet", ".xlsx", "MS Excel 97-2003 spreadsheet", ".xls"), 6, 2, byrow = TRUE)), sheet = 1, overwrite = TRUE, output = c("console", "csv", "xlsx") ) sat_enthalpy2( file = NULL, sheet = 1, elevation = NULL, tbegin = NULL, tend = NULL, tincrement = NULL, overwrite = TRUE, output = c("console", "csv", "xlsx") )
sat_enthalpy( file = tk_choose.files(default = "", caption = "Select file to open", multi = FALSE, filters = matrix(c("Comma-separated value file", ".csv", "MS Excel spreadsheet", ".xlsx", "MS Excel 97-2003 spreadsheet", ".xls"), 6, 2, byrow = TRUE)), sheet = 1, overwrite = TRUE, output = c("console", "csv", "xlsx") ) sat_enthalpy2( file = NULL, sheet = 1, elevation = NULL, tbegin = NULL, tend = NULL, tincrement = NULL, overwrite = TRUE, output = c("console", "csv", "xlsx") )
file |
Input file (.xls, .xlsx, or .csv) |
sheet |
Sheet number or sheet name for the input file (default is sheet 1) |
overwrite |
If |
output |
The output format of the resulting data.table (console, .csv, or .xlsx) |
elevation |
numeric vector that contains the location elevation in feet |
tbegin |
numeric vector that contains the beginning temperature in degrees F |
tend |
numeric vector that contains the ending temperature in degrees F |
tincrement |
numeric vector that contains the temperature increment in degrees F |
The sat_enthalpy function provides a file dialog for the user to choose the selected file.
The sat_enthalpy2 function either allows the user to provide the elevation and temperature values without a file or allows the user to provide a file without using a file dialog.
H is expressed as
enthalpy (Btu/lb)
dry-bulb temperature (degrees F)
specific humidity (lbwater / lbdry air)
where W or SH (Specific Humidity) is expressed as
W or SH – Specific Humidity (kg/kg)
Vapor pressure in saturated air at this temperature (millibars)
Pressure (millibars)
data.table with the output parameters displayed on the console or as a spreadsheet
Irucka Embry [R version], Timothy H. Diehl [Microsoft Excel(R) spreadsheet]
r - Better error message for stopifnot? - Stack Overflow answered by Andrie on Dec 1 2011. See https://stackoverflow.com/questions/8343509/better-error-message-for-stopifnot.
How to check file extensions in R - Stack Overflow answered by lebatsnok on May 17 2014. See https://stackoverflow.com/questions/23713284/how-to-check-file-extensions-in-r.
RDocumentation: TclInterface tcltk. See https://www.rdocumentation.org/packages/tcltk/versions/3.3.1.
James Wettenhall & Philippe Grosjean, File Open/Save dialogs in R tcltk, December 01, 2015. See https://web.archive.org/web/20160521051207/http://www.sciviews.org/recipes/tcltk/TclTk-file-open-save-dialogs/. Retrieved thanks to the Internet Archive: Wayback Machine
multiple output filenames in R - Stack Overflow asked and edited by Gabelins on Feb 1 2013. See https://stackoverflow.com/questions/14651594/multiple-output-filenames-in-r.
r - Regex return file name, remove path and file extension - Stack Overflow answered and edited by Ananda Mahto on Feb 25 20134. See https://stackoverflow.com/questions/15073753/regex-return-file-name-remove-path-and-file-extension/15073919.
"Andrew Revering's List of Meteorological Formulas", https://web.archive.org/web/20131222174626/https://aprweather.com/pages/calc.htm. Retrieved thanks to the Internet Archive: Wayback Machine
Functional Testing and Design Guides, Functional Testing Guide: from the Fundamentals to the Field, "Sidebar 3: How to Calculate Enthalpy", https://web.archive.org/web/20150216015428/https://www.ftguide.org/ftg/IntegratedOperation/IOC-Sidebars-1-3/IOC-SB3-How-To-Calculate-Enthalpy.htm. Retrieved thanks to the Internet Archive: Wayback Machine
## Not run: library("ie2misc") # Example to check the input file format # Copy and paste the following code into the R console if you # wish to see the input file format. library("openxlsx") openXL(system.file("extdata", "Saturated_Enthalpy_Example_Input.xlsx", package = "ie2misc")) # opens the workbook using the default spreadsheet application # Examples to show different use cases sat_enthalpy(output = "csv") # The sheet number is 1 (default) and the output is csv sat_enthalpy2(system.file("extdata", "Saturated_Enthalpy_Example_Input.xlsx" , package = "ie2misc"), output = "xlsx") # The sheet number is 1 (default) and the output is xlsx sat_enthalpy2(system.file("extdata", "Saturated_Enthalpy_Example_Input.csv", package = "ie2misc"), output = "console") # The sheet number is 1 (default) and the output is the console sat_enthalpy2(elevation = 1200, tbegin = 32, tend = 180, tincrement = 0.01, output = "csv") # elevation = 1200 feet, tbegin = 32 degrees F, tend = 180 degrees F, # tincrement = 0.01 degrees F ## End(Not run)
## Not run: library("ie2misc") # Example to check the input file format # Copy and paste the following code into the R console if you # wish to see the input file format. library("openxlsx") openXL(system.file("extdata", "Saturated_Enthalpy_Example_Input.xlsx", package = "ie2misc")) # opens the workbook using the default spreadsheet application # Examples to show different use cases sat_enthalpy(output = "csv") # The sheet number is 1 (default) and the output is csv sat_enthalpy2(system.file("extdata", "Saturated_Enthalpy_Example_Input.xlsx" , package = "ie2misc"), output = "xlsx") # The sheet number is 1 (default) and the output is xlsx sat_enthalpy2(system.file("extdata", "Saturated_Enthalpy_Example_Input.csv", package = "ie2misc"), output = "console") # The sheet number is 1 (default) and the output is the console sat_enthalpy2(elevation = 1200, tbegin = 32, tend = 180, tincrement = 0.01, output = "csv") # elevation = 1200 feet, tbegin = 32 degrees F, tend = 180 degrees F, # tincrement = 0.01 degrees F ## End(Not run)
This function computes the Nash-Sutcliffe model efficiency (NSE) or "Nash and Sutcliffe’s coefficient of efficiency (E)".
vnse(predicted, observed, na.rm = FALSE)
vnse(predicted, observed, na.rm = FALSE)
predicted |
numeric vector that contains the model predicted data points (1st parameter) |
observed |
numeric vector that contains the observed data points (2nd parameter) |
na.rm |
logical vector that determines whether the missing values should be removed or not. |
NSE or E is expressed as
"Nash and Sutcliffe’s coefficient of efficiency (E)"
the number of observations
the "model estimates or predictions"
the "pairwise-matched observations that are judged to be reliable"
the "true" mean of the observations
Note: Both P and O should have the same units.
"Nash and Sutcliffe’s coefficient of efficiency (E)" and other "dimensionless measures of average error" are fully discussed in the Willmott reference.
Nash-Sutcliffe model efficiency (NSE) as a numeric vector. The
default choice is that any NA values will be kept (na.rm = FALSE
). This can
be changed by specifying na.rm = TRUE
, such as vnse(pre, obs, na.rm = TRUE)
.
Cort J. Willmott, Scott M. Robeson, and Kenji Matsuura, "A refined index of model performance", International Journal of Climatology, Volume 32, Issue 13, pages 2088-2094, 15 November 2012, article from ResearchGate: https://www.researchgate.net/publication/235961403_A_refined_index_of_model_performance.
mape
for mean absolute percent error (MAPE), mae
for
mean-absolute error (MAE), madstat
for mean-absolute deviation (MAD), dr
for "index of agreement (dr)", and rmse
for root mean square error (RMSE).
library("ie2misc") obs <- 1:10 # observed pre <- 2:11 # predicted vnse(pre, obs) library("rando") set_n(100) # makes the example reproducible obs1 <- r_norm(.seed = 609) # observed pre1 <- r_norm(.seed = 624) # predicted # using the vectors pre1 and obs1 vnse(pre1, obs1) # using a matrix of the numeric vectors pre1 and obs1 mat1 <- matrix(data = c(obs1, pre1), nrow = length(pre1), ncol = 2, byrow = FALSE, dimnames = list(c(rep("", length(pre1))), c("Predicted", "Observed"))) vnse(mat1[, 2], mat1[, 1]) # mat1[, 1] # observed values from column 1 of mat1 # mat1[, 2] # predicted values from column 2 of mat1 # using a data.frame of the numeric vectors pre1 and obs1 df1 <- data.frame(obs1, pre1) vnse(df1[, 2], df1[, 1]) # df1[, 1] # observed values from column 1 of df1 # df1[, 2] # predicted values from column 2 of df1 library("data.table") # using a data.table of the numeric vectors pre1 and obs1 df2 <- data.table(obs1, pre1) vnse(df2[, 2, with = FALSE][[1]], df2[, 1, with = FALSE][[1]]) # df2[, 1, with = FALSE][[1]] # observed values from column 1 of df2 # df2[, 2, with = FALSE][[1]] # predicted values from column 2 of df2
library("ie2misc") obs <- 1:10 # observed pre <- 2:11 # predicted vnse(pre, obs) library("rando") set_n(100) # makes the example reproducible obs1 <- r_norm(.seed = 609) # observed pre1 <- r_norm(.seed = 624) # predicted # using the vectors pre1 and obs1 vnse(pre1, obs1) # using a matrix of the numeric vectors pre1 and obs1 mat1 <- matrix(data = c(obs1, pre1), nrow = length(pre1), ncol = 2, byrow = FALSE, dimnames = list(c(rep("", length(pre1))), c("Predicted", "Observed"))) vnse(mat1[, 2], mat1[, 1]) # mat1[, 1] # observed values from column 1 of mat1 # mat1[, 2] # predicted values from column 2 of mat1 # using a data.frame of the numeric vectors pre1 and obs1 df1 <- data.frame(obs1, pre1) vnse(df1[, 2], df1[, 1]) # df1[, 1] # observed values from column 1 of df1 # df1[, 2] # predicted values from column 2 of df1 library("data.table") # using a data.table of the numeric vectors pre1 and obs1 df2 <- data.table(obs1, pre1) vnse(df2[, 2, with = FALSE][[1]], df2[, 1, with = FALSE][[1]]) # df2[, 1, with = FALSE][[1]] # observed values from column 1 of df2 # df2[, 2, with = FALSE][[1]] # predicted values from column 2 of df2