R Under development (unstable) (2024-01-12 r85803 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library("robsurvey", quietly = TRUE) > > # test on floating point equality > all_equal <- function(target, current, label, + tolerance = sqrt(.Machine$double.eps), scale = NULL, + check.attributes = FALSE) + { + if (missing(label)) + stop("Argument 'label' is missing\n") + res <- all.equal(target, current, tolerance, scale, + check.attributes = check.attributes) + if (is.character(res)) + cat(paste0(label, ": ", res, "\n")) + } > > # test against MASS::hubers > if (requireNamespace("MASS", quietly = TRUE)) { + library(MASS) + + # convergence tolerance + TOLERANCE <- 1e-8 + + # make a copy of the function MASS::hubers + hubers_mod <- hubers + # replace the mad by the (scaled) IQR as initial scale estimator + body(hubers_mod)[[7]][[3]][[2]] <- substitute(s0 <- IQR(y, type = 2) * + 0.7413) + + # payroll data + attach(workplace) + all_equal(huber2(payroll, rep(1, length(payroll)), tol = TOLERANCE), + hubers_mod(payroll, tol = 1e-8)$mu, + label = "huber2: payroll data: test against MASS::hubers failed") + + # x11 data + x11 <- c(1:10, 1e99); w11 <- rep(1, 11) + all_equal(huber2(x11, w11, tol = TOLERANCE), + hubers_mod(x11, tol = 1e-8)$mu, + label = "huber2: x11 data: test against MASS::hubers failed") + + # x11 data (scaled) + x11 <- c(1:10, 1e99) / 1e20; w11 <- rep(1, 11) + all_equal(huber2(x11, w11, tol = TOLERANCE), + hubers_mod(x11, tol = 1e-8)$mu, + label = "huber2: x11 data (scale): test against MASS::hubers failed") + } > > proc.time() user system elapsed 0.17 0.07 0.23