-
Notifications
You must be signed in to change notification settings - Fork 0
/
relerror.R
168 lines (151 loc) · 4.07 KB
/
relerror.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#' Relative error
#'
#' This function computes the relative error.
#'
#' Relative error is expressed as
#'
#' \deqn{\varepsilon_{t} = \frac{{true \: value} - {approximation}}{true \: value} \cdot 100}
#'
#' \describe{
#' \item{\emph{\eqn{\varepsilon_t}}}{the "true percent relative error"}
#' \item{\emph{true value}}{the true value}
#' \item{\emph{approximation}}{the approximate value}
#' }
#'
#'
#' @param xt numeric vector that contains the true value(s)
#' @param xa numeric vector that contains the approximate value(s)
#'
#' @return relative error, as a percent (\%), as a numeric \code{\link[base]{vector}}.
#'
#'
#' @references
#' Steven C. Chapra, \emph{Applied Numerical Methods with MATLAB for Engineers and Scientists}, Second Edition, Boston, Massachusetts: McGraw-Hill, 2008, page 82-83.
#'
#'
#'
#'
#'
#' @author Irucka Embry
#'
#'
#'
#' @encoding UTF-8
#'
#'
#'
#'
#'
#'
#' @seealso \code{\link{sgm}} for geometric mean, \code{\link{shm}} for harmonic mean, \code{\link{cv}} for
#' coefficient of variation (CV), \code{\link{rms}} for root-mean-square (RMS), \code{\link{approxerror}}
#' for approximate error, and \code{\link{ranges}} for sample range.
#'
#'
#'
#'
#'
#'
#'
#'
#'
#' @examples
#'
#' library(iemisc)
#'
#' # Example 4.1 from the Reference text (page 83)
#'
#' relerror(1.648721, 1.5) # answer as a percent (\%)
#'
#'
#'
#' @importFrom checkmate qtest
#' @importFrom assertthat assert_that
#'
#'
#' @export
relerror <- function (xt, xa) {
checks <- c(xt, xa)
# check for xt and xa
assert_that(!any(qtest(checks, "N+(,)") == FALSE), msg = "Either xt or xa is NA, NaN, Inf, -Inf, empty, or a string. Please try again.")
# only process with finite values and provide an error message if the check fails
assert_that(length(checks) == 2, msg = "There are not 2 known variables. Please try again with 2 known variables (xt and xa).")
# only process with enough known variables and provide an error message if the check fails
abs(((xt - xa) / xt)) * 100
}
#' Approximate error
#'
#' This function computes the "approximate estimate of the error" ("percent relative error").
#'
#' Approximate error is expressed as
#'
#' \deqn{\varepsilon_{a} = \frac{{present \: approximation} - {previous \: approximation}}{present \: approximation} \cdot 100}
#'
#' \describe{
#' \item{\emph{\eqn{\varepsilon_a}}}{the "approximate estimate of the error"}
#' \item{\emph{present approximation}}{the "present approximation"}
#' \item{\emph{previous approximation}}{the "previous approximation"}
#' }
#'
#'
#' @param pres numeric vector that contains the "present approximation"
#' value(s)
#' @param prev numeric vector that contains the "previous approximation"
#' value(s)
#'
#' @return approximate error, as a percent (\%), as a numeric \code{\link[base]{vector}}.
#'
#'
#' @references
#' Steven C. Chapra, \emph{Applied Numerical Methods with MATLAB for Engineers and Scientists}, Second Edition, Boston, Massachusetts: McGraw-Hill, 2008, page 82-84.
#'
#'
#'
#'
#'
#' @author Irucka Embry
#'
#'
#'
#' @encoding UTF-8
#'
#'
#'
#'
#'
#'
#' @seealso \code{\link{sgm}} for geometric mean, \code{\link{shm}} for harmonic mean, \code{\link{cv}} for
#' coefficient of variation (CV), \code{\link{rms}} for root-mean-square (RMS), \code{\link{relerror}}
#' for relative error, and \code{\link{ranges}} for sample range.
#'
#'
#'
#'
#'
#'
#'
#'
#'
#' @examples
#'
#' library(iemisc)
#'
#' # Example 4.1 from the Reference text (page 84)
#'
#' approxerror(1.5, 1) # answer as a percent (\%)
#'
#'
#'
#' @importFrom checkmate qtest
#' @importFrom assertthat assert_that
#'
#'
#' @export
approxerror <- function (pres, prev) {
checks <- c(pres, prev)
assert_that(!any(qtest(checks, "N+(,)") == FALSE), msg = "Either pres or prev is NA, NaN, Inf, -Inf, empty, or a string. Please try again.")
# only process with finite values and provide an error message if the check fails
assert_that(length(checks) == 2, msg = "There are not 2 known variables. Please try again with 2 known variables (pres and prev).")
# only process with enough known variables and provide an error message if the check fails
abs(((pres - prev) / pres)) * 100
}