Skip to content

Commit

Permalink
Added alpha to mnr object; updated print and glance methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kloppen committed Jun 24, 2020
1 parent 6e88058 commit c46e5e6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
25 changes: 15 additions & 10 deletions R/mnr.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#' This object has the following fields:
#' \item{\code{call}}{the expression used to call this function}
#' \item{\code{data}}{the original data used to compute the MNR}
#' \item{\code{alpha}}{the value of alpha given by the user}
#' \item{\code{mnr}}{the computed MNR test statistic}
#' \item{\code{crit}}{the critical value given the sample size and the
#' significance level}
Expand All @@ -55,9 +56,9 @@
#' ## Call:
#' ## maximum_normed_residual(data = ., x = strength)
#' ##
#' ## MNR = 1.958797 ( critical value = 1.887145 )
#' ## MNR = 1.958797 ( critical value = 1.887145 )
#' ##
#' ## Outliers:
#' ## Outliers ( alpha = 0.05 ):
#' ## Index Value
#' ## 6 44.26
#'
Expand All @@ -68,9 +69,9 @@
#' ## Call:
#' ## maximum_normed_residual(data = ., x = strength)
#' ##
#' ## MNR = 1.469517 ( critical value = 1.887145 )
#' ## MNR = 1.469517 ( critical value = 1.887145 )
#' ##
#' ## No outliers detected
#' ## No outliers detected ( alpha = 0.05 )
#'
#' @references
#' “Composite Materials Handbook, Volume 1. Polymer Matrix Composites
Expand All @@ -94,6 +95,8 @@ maximum_normed_residual <- function(data = NULL, x, alpha = 0.05) {
cur_data <- eval_tidy(enquo(x), data)
res$data <- cur_data

res$alpha <- alpha

indicies_cur <- seq_along(res$data)

cur_mnr <- max(abs(res$data - mean(res$data)) / sd(res$data))
Expand Down Expand Up @@ -155,6 +158,7 @@ maximum_normed_residual_crit <- function(n, alpha) {
#' columns:
#'
#' \item{\code{mnr}}{the computed MNR test statistic}
#' \item{\code{alpha}}{the value of alpha used for the test}
#' \item{\code{crit}}{the critical value given the sample size and the
#' significance level}
#' \item{\code{n_outliers}}{the number of outliers found}
Expand All @@ -168,10 +172,10 @@ maximum_normed_residual_crit <- function(n, alpha) {
#' m <- maximum_normed_residual(x = x)
#' glance(m)
#'
#' ## # A tibble: 1 x 3
#' ## mnr crit n_outliers
#' ## <dbl> <dbl> <dbl>
#' ## 1 4.25 2.73 1
#' ## # A tibble: 1 x 4
#' ## mnr alpha crit n_outliers
#' ## <dbl> <dbl> <dbl> <dbl>
#' ## 1 4.23 0.05 2.73 1
#'
#' @method glance mnr
#' @importFrom tibble tibble
Expand All @@ -182,6 +186,7 @@ glance.mnr <- function(x, ...) { # nolint
x,
tibble::tibble(
mnr = mnr,
alpha = alpha,
crit = crit,
n_outliers = n_outliers
)
Expand Down Expand Up @@ -275,9 +280,9 @@ print.mnr <- function(x, ...) {
paste(deparse(x$call), sep = "\n", collapse = "\n"), "\n\n", sep = "")
cat("MNR =", x$mnr, " ( critical value =", x$crit, ")\n\n")
if (nrow(x$outliers) == 0) {
cat("No outliers detected\n\n")
cat("No outliers detected ( alpha =", x$alpha, ")\n\n")
} else {
cat("Outliers:\n")
cat("Outliers ( alpha =", x$alpha, "):\n")

justify <- c("right", "left", "left")
width <- c(8L, 2L, 16L)
Expand Down
9 changes: 5 additions & 4 deletions man/glance.mnr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions man/maximum_normed_residual.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/testthat/test-mnr.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ test_that(
expect_output(print(res), "no outliers", ignore.case = TRUE)
expect_output(print(res), "MNR.*2.008", ignore.case = TRUE)
expect_output(print(res), ".*crit.*2\\.12", ignore.case = TRUE)
expect_output(print(res), ".*alpha.*0\\.05", ignore.case = TRUE)

# check for typographical errors in the data above
df %>%
Expand All @@ -276,6 +277,7 @@ test_that(
expect_output(print(res), "outliers", ignore.case = TRUE)
expect_output(print(res), "MNR.*2.119", ignore.case = TRUE)
expect_output(print(res), ".*crit.*2\\.01", ignore.case = TRUE)
expect_output(print(res), ".*alpha.*0\\.05", ignore.case = TRUE)
# check that the outlier was shown in the print statement
expect_output(print(res), "4.*80\\.23348", ignore.case = TRUE)

Expand Down Expand Up @@ -365,6 +367,7 @@ test_that("glance method returns expected results", {

expect_equal(glance_res$mnr, 2.119, tolerance = 0.001)
expect_equal(glance_res$crit, 2.02, tolerance = 0.001)
expect_equal(glance_res$alpha, 0.05, tolerance = 0.00001)
expect_equal(glance_res$n_outliers, 1)
})

Expand Down

0 comments on commit c46e5e6

Please sign in to comment.