Title: | Statistical Tests for the Production of Reference Materials |
---|---|
Description: | The production of certified reference materials (CRMs) requires various statistical tests depending on the task and recorded data to ensure that reported values of CRMs are appropriate. Often these tests are performed according to the procedures described in 'ISO GUIDE 35:2017'. The 'eCerto' package contains a 'Shiny' app which provides functionality to load, process, report and backup data recorded during CRM production and facilitates following the recommended procedures. It is described in Lisec et al (2023) <doi:10.1007/s00216-023-05099-3> and can also be accessed online <https://apps.bam.de/shn00/eCerto/> without package installation. |
Authors: | Jan Lisec [cre, aut] , Frederik Kreß [ctb] |
Maintainer: | Jan Lisec <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.5.16 |
Built: | 2024-11-16 06:06:49 UTC |
Source: | https://github.com/janlisec/ecerto |
assert_col
will check in a data.frame for name, position,
type of a specific column and ensure that the return value (data frame)
contains a respective column. If possible, the current values are converted
into the specified type.
assert_col( df, name, pos = NULL, type = c("character", "integer", "numeric", "factor", "logical", "Date"), fuzzy_name = TRUE, default_value = NULL )
assert_col( df, name, pos = NULL, type = c("character", "integer", "numeric", "factor", "logical", "Date"), fuzzy_name = TRUE, default_value = NULL )
df |
Input data frame. |
name |
Name of the column to ensure (and to search for). |
pos |
Position of this column. NULL to keep position where found in df. |
type |
Desired data type of this column. |
fuzzy_name |
Allow fuzzy matching (additional blanks and case insensitive search allowed). |
default_value |
Default value if column needs to be created or can not be converted to specified type. Keep NULL to use pre defined default values. |
tbd.
A data frame with a column of the specified name and type at the specified position. An error message is attached to the result as an attribute in case of unexpected events.
x <- data.frame( "analyte" = c("A", "B"), "tmp" = rep(0L, 2), "unit" = c("x", "y") ) str(x) ac <- eCerto::assert_col str(ac(df = x, name = "analyte", pos = 1, type = "factor")) str(ac(df = x, name = "Analyte", pos = 3, type = "character")) str(ac(df = x, name = " Analyte", pos = 2, type = "factor")) str(ac(df = x, name = "Analyte", pos = 2, type = "factor", fuzzy_name = FALSE)) str(ac(df = x, name = "test", type = "factor", default_value = "test")) # this will lead to NAs in column unit because the conversion does not lead to an error # hence the default value is not used str(ac(df = x, name = "unit", type = "numeric", default_value = 10)) # this will lead to the specified default data in column unit because the # conversion attempt does lead to an error str(ac(df = x, name = "unit", type = "Date")) str(ac(df = data.frame("test" = "2022-03-31"), name = "test", type = "Date")) # show type and class of internal default values x <- data.frame( "character" = "", "integer" = 0L, "numeric" = 0, "factor" = factor(NA), "logical" = NA, "date" = Sys.Date(), NA ) sapply(1:ncol(x), function(i) { typeof(x[, i]) }) sapply(1:ncol(x), function(i) { class(x[, i]) })
x <- data.frame( "analyte" = c("A", "B"), "tmp" = rep(0L, 2), "unit" = c("x", "y") ) str(x) ac <- eCerto::assert_col str(ac(df = x, name = "analyte", pos = 1, type = "factor")) str(ac(df = x, name = "Analyte", pos = 3, type = "character")) str(ac(df = x, name = " Analyte", pos = 2, type = "factor")) str(ac(df = x, name = "Analyte", pos = 2, type = "factor", fuzzy_name = FALSE)) str(ac(df = x, name = "test", type = "factor", default_value = "test")) # this will lead to NAs in column unit because the conversion does not lead to an error # hence the default value is not used str(ac(df = x, name = "unit", type = "numeric", default_value = 10)) # this will lead to the specified default data in column unit because the # conversion attempt does lead to an error str(ac(df = x, name = "unit", type = "Date")) str(ac(df = data.frame("test" = "2022-03-31"), name = "test", type = "Date")) # show type and class of internal default values x <- data.frame( "character" = "", "integer" = 0L, "numeric" = 0, "factor" = factor(NA), "logical" = NA, "date" = Sys.Date(), NA ) sapply(1:ncol(x), function(i) { typeof(x[, i]) }) sapply(1:ncol(x), function(i) { class(x[, i]) })
Calculation of a time difference between time points in a vector x
and
a specific start date d_start
in month (days or years).
calc_time_diff( x = NULL, d_start = NULL, type = c("mon", "day", "year"), origin = "1900-01-01", exact = FALSE )
calc_time_diff( x = NULL, d_start = NULL, type = c("mon", "day", "year"), origin = "1900-01-01", exact = FALSE )
x |
A vector of dates or character in format 'yyyy-mm-dd'. |
d_start |
A specific start date (if unspecified the minimum of x will be used to ensure positive values). |
type |
You may specify 'year' or 'day' instead of month here. |
origin |
The origin used. |
exact |
Function will return exact values instead of full month and year if this is set to TRUE. |
A numeric vector of length x
containing calculated time differences
in the unit specified by type
. Not a difftime object.
x <- c("2022-02-01", "2022-02-03", "2022-03-01", "2024-02-01") calc_time_diff(x = x) calc_time_diff(x = x, exact = TRUE) calc_time_diff(x = x, type = "day") calc_time_diff(x = x, type = "year") calc_time_diff(x = x, type = "year", d_start = "2021-12-31") calc_time_diff(x = 1:3, type = "day", origin = Sys.Date())
x <- c("2022-02-01", "2022-02-03", "2022-03-01", "2024-02-01") calc_time_diff(x = x) calc_time_diff(x = x, exact = TRUE) calc_time_diff(x = x, type = "day") calc_time_diff(x = x, type = "year") calc_time_diff(x = x, type = "year", d_start = "2021-12-31") calc_time_diff(x = 1:3, type = "day", origin = Sys.Date())
An example set of data collected for a CRM.
data(CRM001)
data(CRM001)
A list of length = 6 containing CRM test data.
Dixon critical values table.
data(cvals_Dixon)
data(cvals_Dixon)
A data frame containing Dixon critical values with n in rows and alpha in cols.
http://www.statistics4u.com/fundstat_eng/cc_outlier_tests_dixon.html
Grubbs2 critical values table.
data(cvals_Grubbs2)
data(cvals_Grubbs2)
A data frame containing critical values for Double Grubbs test with n in rows and alpha in cols.
outliers
package and https://link.springer.com/article/10.1007/s10182-011-0185-y.
Builds a class, which allows only restricted access to the
contained 'reactiveValues'. Elements should be accessed via getValue()
.
Possible advantages are that (1) structure of 'reactiveValues' is clear
from the beginning (no function like "addVariable" should exist!) and that
(2) functions to calculate the mean or plot current data can be implemented
here directly.
General access to data object (so data object can maybe get changed without that much code edit)
Returns element. If 'key' is used, reactivity not working correctly.
Preferable way for calling getValue(df, key)
, see example
setValue(df, key, value) getValue(df, key = NULL)
setValue(df, key, value) getValue(df, key = NULL)
df |
An object of class R6. |
key |
Key value within R6 object 'df'. |
value |
Value to set. |
Nothing. The R6 object is updated automatically.
Value of 'key' from 'df'.
cur_an
Set or return the current analyte (reactiveVal) via an active binding.
new()
Write the (reactive) value of element 'keys' from list 'l'.
eCerto$new(rv)
rv
'reactiveValues' object.
A new 'eCerto' object.
get()
Read the value of field element of R6 object.
eCerto$get(keys = NULL)
keys
Name of list element.
Current value of field.
set()
Set element of R6 object defined by 'keys' to new value.
eCerto$set(keys = NULL, value)
keys
Name(s) of list element.
value
New value.
New value of element (invisible).
c_plot()
Plot the certification data either provided by the user or from the private slot of self.
eCerto$c_plot(data, annotate_id = FALSE, filename_labels = FALSE)
data
data.frame containing columns 'value', 'Lab' and 'L_flt' for a specific analyte.
annotate_id
T/F to overlay the plot with ID as text if column 'ID' is present.
filename_labels
T/F to use imported file names as labels on x-axes.
A plot.
c_lab_means()
Compute the analyte means for a data set filtered for a specific analyte.
eCerto$c_lab_means(data)
data
data.frame containing columns 'analyte', 'value', 'Lab', 'S_flt' and 'L_flt'.
A data.frame of lab means.
c_analytes()
Return analyte names currently in apm.
eCerto$c_analytes()
A named character vector.
c_lab_codes()
Return lab codes currently in C data.
eCerto$c_lab_codes()
A named character vector.
a_p()
Return currently specified values of a type for all analytes.
eCerto$a_p( val = c("precision", "precision_export", "pooling", "confirmed", "unit", "name") )
val
A character value indicating the item of the apm list to be extracted
A named vector.
e_present()
Return modules with existing data.
eCerto$e_present()
A named logical vector.
c_fltData()
Filter the full data set for a specific analyte and remove all 'S_flt' but keep 'L_flt'.
eCerto$c_fltData(recalc = FALSE)
recalc
If TRUE triggers a recalculation and returns current object if FALSE..
A data.frame with filtered data of a single analyte.
clone()
The objects of this class are cloneable with this method.
eCerto$clone(deep = FALSE)
deep
Whether to make a deep clone.
if (interactive()) { # establish new Shiny session and new eCerto object ShinySession <- shiny::MockShinySession$new() test <- eCerto::eCerto$new() # view current value stored in specific eCerto slot and register observer shiny::isolate(eCerto::getValue(test, c("Certification", "data"))) shiny::observeEvent(eCerto::getValue(test, c("Certification", "data")), { message("Certification$data changed:", eCerto::getValue(test, "Certification")$data) }) # change value of specific eCerto slot and flush reactivity to trigger observer shiny::isolate(eCerto::setValue(test, c("Certification", "data"), 5)) ShinySession$flushReact() shiny::isolate(eCerto::getValue(test, c("Certification", "data"))) } tmp <- eCerto$new() shiny::isolate(tmp$c_plot()) shiny::isolate(tmp$c_lab_means()) tmp$c_analytes() tmp$c_lab_codes() tmp$a_p() tmp$a_p("pooling") ca <- shiny::isolate(tmp$cur_an) tmp$a_p("pooling")[ca] shiny::isolate(tmp$e_present()) tmp$c_fltData() shiny::isolate(tmp$cur_an <- "Fe") shiny::isolate(tmp$cur_an) tmp$c_fltData() x <- shiny::isolate(eCerto::getValue(tmp, c("General", "apm"))) x[[shiny::isolate(tmp$cur_an)]][["lab_filter"]] <- "L2" shiny::isolate(eCerto::setValue(tmp, c("General", "apm"), x)) tmp$c_fltData() tmp$c_fltData(recalc = TRUE) # Only run examples in interactive R sessions if (interactive()) { rv <- eCerto$new(init_rv()) setValue(rv, c("Certification", "data"), 5) getValue(rv, c("Certification", "data")) # is 5? setValue(rv, c("General", "user"), "Franz") getValue(rv, c("General", "user")) }
if (interactive()) { # establish new Shiny session and new eCerto object ShinySession <- shiny::MockShinySession$new() test <- eCerto::eCerto$new() # view current value stored in specific eCerto slot and register observer shiny::isolate(eCerto::getValue(test, c("Certification", "data"))) shiny::observeEvent(eCerto::getValue(test, c("Certification", "data")), { message("Certification$data changed:", eCerto::getValue(test, "Certification")$data) }) # change value of specific eCerto slot and flush reactivity to trigger observer shiny::isolate(eCerto::setValue(test, c("Certification", "data"), 5)) ShinySession$flushReact() shiny::isolate(eCerto::getValue(test, c("Certification", "data"))) } tmp <- eCerto$new() shiny::isolate(tmp$c_plot()) shiny::isolate(tmp$c_lab_means()) tmp$c_analytes() tmp$c_lab_codes() tmp$a_p() tmp$a_p("pooling") ca <- shiny::isolate(tmp$cur_an) tmp$a_p("pooling")[ca] shiny::isolate(tmp$e_present()) tmp$c_fltData() shiny::isolate(tmp$cur_an <- "Fe") shiny::isolate(tmp$cur_an) tmp$c_fltData() x <- shiny::isolate(eCerto::getValue(tmp, c("General", "apm"))) x[[shiny::isolate(tmp$cur_an)]][["lab_filter"]] <- "L2" shiny::isolate(eCerto::setValue(tmp, c("General", "apm"), x)) tmp$c_fltData() tmp$c_fltData(recalc = TRUE) # Only run examples in interactive R sessions if (interactive()) { rv <- eCerto$new(init_rv()) setValue(rv, c("Certification", "data"), 5) getValue(rv, c("Certification", "data")) # is 5? setValue(rv, c("General", "user"), "Franz") getValue(rv, c("General", "user")) }
An example set of data collected for a LTS monitoring.
data(LTS001)
data(LTS001)
A list of lists of length = 2 containing LTS test data.
Run the Shiny Application
run_app( onStart = NULL, options = list(), enableBookmarking = NULL, uiPattern = "/", ... )
run_app( onStart = NULL, options = list(), enableBookmarking = NULL, uiPattern = "/", ... )
onStart |
A function that will be called before the app is actually run.
This is only needed for |
options |
Named options that should be passed to the |
enableBookmarking |
Can be one of |
uiPattern |
A regular expression that will be applied to each |
... |
arguments to pass to golem_opts.
See |
Translation of STEYX
function from Excel to R. It is implemented
according to the formula described in
http://office.microsoft.com/en-au/excel-help/steyx-function-HP010062545.aspx.
At least 3 finite pairs of data points are required for the calculation.
steyx(x, y)
steyx(x, y)
x |
x values as numeric vector. |
y |
y values as numeric vector of similar length as x. |
The standard error of the predicted y-value for each x in the regression.
steyx(x = 1:3, y = 2:4) steyx(x = 1:3, y = c(2, 3.1, 3.9))
steyx(x = 1:3, y = 2:4) steyx(x = 1:3, y = c(2, 3.1, 3.9))