R/plot.functions.R
changed.
Other files ignored by Codecov
man/plot.Benford.Rd
has changed.
.Rbuildignore
has changed.
7 | 7 | ##' @param x a "Benford" object |
|
8 | 8 | ##' @param select it specifies the order and which plots are going to be plotted. If NULL, the parameter except is used. |
|
9 | 9 | ##' @param except it specifies which plots are not going to be plotted. If NULL, the parameter select is used. |
|
10 | - | ##' Currently, you can choose from 9 plots: "digits", "rootogram digits", "second order", "rootogram second order", "summation", |
|
10 | + | ##' Currently, you can choose from 10 plots: "digits", "rootogram digits", "second order", "rootogram second order", "last two digits", "summation", |
|
11 | 11 | ##' "mantissa", "chi square", "abs diff", "ex summation". If you want to plot all, just |
|
12 | 12 | ##' put except = "none". The default is not to plot the "mantissa" and "abs diff". If you want to plot all, just |
|
13 | 13 | ##' put except = "all" |
17 | 17 | ##' @param err.bounds logical; if TRUE, the upper and lower error bounds are draw. The error bounds indicate the binomial root mean square error. |
|
18 | 18 | ##' @param alpha it specifies level of confidence interval. The defaults to 95 percent confidence interval,i.e., the error bounds will represent 1.96 standard error from the expected count by Benford's Law. |
|
19 | 19 | ##' @param grid logical; if TRUE, adds an rectangular grid to plot. |
|
20 | + | ##' @param exp.benford logical; if TRUE, adds a line representing the expected frequencies by Berford's Law. |
|
20 | 21 | ##' @param freq logical; if TRUE, the plot is a representation of counts; if FALSE, relative frequencies are plotted (so that the plot has a total area of one). Defaults to `TRUE`. |
|
21 | 22 | ##' @param ... arguments to be passed to generic plot functions, |
|
22 | 23 | ##' @return Plots the Benford object. |
37 | 38 | alpha = 0.05, |
|
38 | 39 | grid = TRUE, |
|
39 | 40 | mfrow = NULL, |
|
41 | + | exp.benford = TRUE, |
|
40 | 42 | freq = TRUE, ...){ |
|
41 | 43 | ||
42 | 44 |
44 | 46 | ||
45 | 47 | if(!(alpha > 0 & alpha < 1)) stop(paste0(alpha, " is not a valid value for 'alpha' parameter")) |
|
46 | 48 | ||
47 | - | available.plots <- c("digits", "rootogram digits", "second order", "rootogram second order", "summation", "mantissa", "chi squared", "ex summation", "abs diff", "obs vs exp") |
|
49 | + | available.plots <- c("digits", "rootogram digits", "second order", "rootogram second order", "summation", "mantissa", "chi squared", "ex summation", "abs diff", "obs vs exp", "last two digits") |
|
48 | 50 | ||
49 | 51 | if (!is.null(select)) { |
|
50 | 52 | check.plot.names(select, c(available.plots, "all")) |
102 | 104 | lg_size <- ifelse(rows > 1, 1, ifelse(err.bounds, 0.4, 0.7))/rows |
|
103 | 105 | ||
104 | 106 | for (i in 1:length(plot_this)) { |
|
105 | - | plot.switch(plot_this[i], x, col.bar, grid, err.bounds, alpha, freq) |
|
107 | + | plot.switch(plot_this[i], x, col.bar, grid, err.bounds, alpha, exp.benford, freq) |
|
106 | 108 | if(!(plot_this[i] %in% c("chi squared", "abs diff", "ex summation", "obs vs exp"))){ |
|
107 | 109 | legend.Berford(x, err.bounds, lg_size) |
|
108 | 110 | } |
115 | 117 | lg_size <- ifelse(err.bounds, 0.4, 0.7) |
|
116 | 118 | ||
117 | 119 | for (i in 1:length(plot_this)) { |
|
118 | - | plot.switch(plot_this[i], x, col.bar, grid, err.bounds, alpha, freq) |
|
120 | + | plot.switch(plot_this[i], x, col.bar, grid, err.bounds, alpha, exp.benford, freq) |
|
119 | 121 | if(!(plot_this[i] %in% c("chi squared", "abs diff", "ex summation", "obs vs exp"))){ |
|
120 | 122 | legend.Berford(x, err.bounds, lg_size) |
|
121 | 123 | } |
153 | 155 | "summation" = { |
|
154 | 156 | obs_freq <- x[["bfd"]]$data.summation |
|
155 | 157 | exp_freq <- rep(mean(obs_freq), length(digits)) |
|
158 | + | }, |
|
159 | + | "last two digits" = { |
|
160 | + | digits <- 0:99 |
|
161 | + | obs_freq <- x$last.two.digits$data.dist.freq |
|
162 | + | exp_freq <- rep(mean(obs_freq), length(digits)) |
|
156 | 163 | } |
|
157 | 164 | ) |
|
158 | 165 | }else{ |
168 | 175 | "summation" = { |
|
169 | 176 | obs_freq <- x[["bfd"]]$data.summation |
|
170 | 177 | exp_freq <- rep(mean(obs_freq), length(digits)) |
|
178 | + | }, |
|
179 | + | "last two digits" = { |
|
180 | + | digits <- 0:99 |
|
181 | + | obs_freq <- x$last.two.digits$data.dist.freq/sum(x$last.two.digits$data.dist.freq) |
|
182 | + | exp_freq <- rep(mean(obs_freq), length(digits)) |
|
171 | 183 | } |
|
172 | 184 | ) |
|
173 | 185 | } |
208 | 220 | ||
209 | 221 | barplot(obs_freq, col = col.bar, yaxt = "n", add = T) |
|
210 | 222 | ||
211 | - | if(exp.benford) |
|
223 | + | if(exp.benford){ |
|
212 | 224 | lines(xmarks, exp_freq, col = "red", lwd = 2) |
|
225 | + | } |
|
213 | 226 | if((err.bounds & !(obs.freq %in% c("summation", "second order")))){ |
|
214 | 227 | lines(ub ~ xmarks, lty = 2, col = 'red') |
|
215 | 228 | lines(lb ~ xmarks, lty = 2, col = 'red') |
301 | 314 | ybottom = exp_freq, ytop = exp_freq - obs_freq, |
|
302 | 315 | col = col.bar) |
|
303 | 316 | abline(h = 0) |
|
304 | - | if(exp.benford) |
|
317 | + | if(exp.benford){ |
|
305 | 318 | lines(xmarks, exp_freq, col = "red", lwd = 2) |
|
319 | + | } |
|
306 | 320 | if((err.bounds & !(obs.freq %in% "second order"))){ |
|
307 | 321 | lines(ub ~ xmarks, lty = 2, col = 'red') |
|
308 | 322 | lines(lb ~ xmarks, lty = 2, col = 'red') |
484 | 498 | } |
|
485 | 499 | } |
|
486 | 500 | ||
487 | - | plot.switch <- function(plot_this, x, col.bar, grid, err.bounds, alpha, freq){ |
|
501 | + | plot.switch <- function(plot_this, x, col.bar, grid, err.bounds, alpha, exp.benford, freq){ |
|
488 | 502 | switch(plot_this, |
|
489 | - | "digits" = histogram.Benford(x, obs.freq = "digits", main = "Digits distribution", xlab = NULL, ylab = NULL, grid, col.bar, err.bounds, alpha, freq), |
|
490 | - | "rootogram digits" = rootogram.Benford(x, obs.freq = "digits", main = "Digits distribution - Rootogram", xlab = NULL, ylab = NULL, grid, col.bar, err.bounds, alpha, freq), |
|
491 | - | "second order" = histogram.Benford(x, obs.freq = "second order", main = "Digits distribution\nSecond Order Test", xlab = NULL, ylab = NULL, grid, col.bar, err.bounds, alpha, freq), |
|
492 | - | "rootogram second order" = rootogram.Benford(x, obs.freq = "second order", main = "Digits distribution\nSecond Order Test - Rootogram", xlab = NULL, ylab = NULL, grid, col.bar, err.bounds, alpha, freq), |
|
493 | - | "summation" = histogram.Benford(x, obs.freq = "summation", main = "Summation Distribution by digits", xlab = NULL, ylab = "Summation", grid, col.bar, err.bounds, alpha, freq), |
|
503 | + | "digits" = histogram.Benford(x, obs.freq = "digits", main = "Digits distribution", xlab = NULL, ylab = NULL, grid = grid, col.bar = col.bar, err.bounds = err.bounds, alpha = alpha, exp.berford = exp.benford, freq = freq), |
|
504 | + | "rootogram digits" = rootogram.Benford(x, obs.freq = "digits", main = "Digits distribution - Rootogram", xlab = NULL, ylab = NULL, grid = grid, col.bar = col.bar, err.bounds = err.bounds, alpha = alpha, exp.berford = exp.benford, freq = freq), |
|
505 | + | "second order" = histogram.Benford(x, obs.freq = "second order", main = "Digits distribution\nSecond Order Test", xlab = NULL, ylab = NULL, grid = grid, col.bar = col.bar, err.bounds = err.bounds, alpha = alpha, exp.berford = exp.benford, freq = freq), |
|
506 | + | "rootogram second order" = rootogram.Benford(x, obs.freq = "second order", main = "Digits distribution\nSecond Order Test - Rootogram", xlab = NULL, ylab = NULL, grid = grid, col.bar = col.bar, err.bounds = err.bounds, alpha = alpha, exp.berford = exp.benford, freq = freq), |
|
507 | + | "summation" = histogram.Benford(x, obs.freq = "summation", main = "Summation Distribution by digits", xlab = NULL, ylab = "Summation", grid = grid, col.bar = col.bar, err.bounds = err.bounds, alpha = alpha, exp.berford = exp.benford, freq = freq), |
|
494 | 508 | "mantissa" = plot.ordered.mantissa(x, grid), |
|
495 | 509 | "chi squared" = needle.Benford(x, discrepancy = "chi squared", main = "Chi-Squared Difference", xlab = NULL, ylab = "Chi-squared", grid), |
|
496 | 510 | "abs diff" = needle.Benford(x, discrepancy = "abs diff", main = "Absolute Difference", xlab = NULL, ylab = "Absolute Difference", grid), |
|
497 | 511 | "ex summation" = needle.Benford(x, discrepancy = "ex summation", main = "Summation Difference", xlab = NULL, ylab = "Absolute Excess Summation", grid), |
|
498 | - | "obs vs exp" = xyplot.Berford(x, obs.freq = "digits", main = "Expected vs observed frequencies", xlab = NULL, ylab = NULL, grid, freq) |
|
512 | + | "obs vs exp" = xyplot.Berford(x, obs.freq = "digits", main = "Expected vs observed frequencies", xlab = NULL, ylab = NULL, grid, freq), |
|
513 | + | "last two digits" = histogram.Benford(x, obs.freq = "last two digits", main = "Last-Two Digits distribution", xlab = "Last-Two Digits", ylab = NULL, grid = grid, col.bar = col.bar, err.bounds = err.bounds, alpha = alpha, exp.berford = exp.benford, freq = freq) |
|
499 | 514 | ||
500 | 515 | ) |
|
501 | 516 | } |
Files | Coverage |
---|---|
R | 100.00% |
Project Totals (4 files) | 100.00% |