Nothing
tm_data <- function() {
data(forageLIBS, package = "specProc", envir = environment())
x <- as.matrix(forageLIBS[1:120, -(1:14)][, seq(1, 7000, by = 100)])
list(x = x, y = forageLIBS$K[1:120], ca = forageLIBS$Ca[1:120])
}
test_that("tidy and glance summarize a robust PCA", {
d <- tm_data()
set.seed(1)
pca <- robpca(d$x, ncomp = 3)
ev <- tidy(pca)
expect_named(ev, c("PC", "eigenvalue", "std.dev"))
expect_equal(ev$eigenvalue, pca$eigenvalues)
ld <- tidy(pca, matrix = "loadings")
expect_named(ld, c("column", "PC", "value"))
expect_equal(nrow(ld), 3 * ncol(d$x))
expect_equal(ld$value, as.vector(pca$loadings))
expect_equal(unique(ld$column), colnames(d$x))
expect_equal(tidy(pca, matrix = "rotation"), ld) # the names of broom
sc <- tidy(pca, matrix = "scores")
expect_equal(sc$value[sc$PC == 2], unname(pca$scores[, 2]))
expect_error(tidy(pca, matrix = "coefficients"), "matrix")
gl <- glance(pca)
expect_equal(gl$nobs, 120)
expect_equal(gl$nvar, ncol(d$x))
expect_equal(gl$ncomp, 3)
types <- table(pca$outlier_type)
expect_equal(gl$n.bad.leverage, as.integer(types[["bad leverage"]]))
expect_equal(gl$n.orthogonal.outlier, as.integer(types[["orthogonal outlier"]]))
expect_true(is.na(gl$flagged.cells))
expect_equal(gl$method, "ROBPCA")
set.seed(1)
macro <- glance(macropca(d$x, ncomp = 2))
expect_equal(macro$method, "MacroPCA")
expect_false(is.na(macro$flagged.cells))
})
test_that("tidy and glance summarize RSIMPLS and RPCR", {
d <- tm_data()
set.seed(2)
fit <- rsimpls(d$x, d$y, ncomp = 2, ncomp_max = 4)
co <- tidy(fit)
expect_named(co, c("response", "term", "estimate"))
expect_equal(co$term[1], "(Intercept)")
expect_equal(nrow(co), ncol(d$x) + 1)
# the coefficients reproduce the fitted values
expect_equal(drop(d$x %*% co$estimate[-1]) + co$estimate[1], drop(fit$fitted),
ignore_attr = TRUE)
expect_equal(tidy(fit, matrix = "components")$r.squared, fit$components$R2)
expect_equal(nrow(tidy(fit, matrix = "loadings")), 2 * ncol(d$x))
gl <- glance(fit)
expect_equal(gl$ncomp, 2)
expect_equal(gl$ncomp_max, 4)
expect_equal(gl$r.squared, fit$R2)
expect_equal(gl$sigma, sqrt(fit$sigma[1, 1]))
expect_equal(gl$n.vertical.outlier, sum(fit$outlier_type == "vertical outlier"))
expect_equal(gl$method, "RSIMPLS")
set.seed(2)
two <- rsimpls(d$x, cbind(K = d$y, Ca = d$ca), ncomp = 2, ncomp_max = 3)
expect_equal(unique(tidy(two)$response), c("K", "Ca"))
expect_true(is.na(glance(two)$sigma))
set.seed(3)
pcr <- rpcr(d$x, d$y, ncomp = 2, ncomp_max = 3)
expect_equal(glance(pcr)$method, "RPCR")
expect_equal(glance(pcr)$regression, pcr$regression)
expect_equal(nrow(tidy(pcr)), ncol(d$x) + 1)
})
test_that("tidy and glance summarize an LTS regression", {
d <- tm_data()
set.seed(4)
fit <- lts_fit(d$x[, 30:32], d$y)
td <- tidy(fit, conf.int = TRUE)
expect_named(td, c("term", "estimate", "std.error", "statistic", "p.value", "conf.low", "conf.high"))
expect_equal(td$term, c("(Intercept)", colnames(d$x)[30:32]))
expect_equal(td$statistic, td$estimate / td$std.error)
expect_true(all(td$std.error > 0))
expect_equal(td$conf.high - td$estimate, stats::qt(0.975, fit$df_residual) * td$std.error)
expect_equal(fit$df_residual, sum(fit$weights) - 4)
gl <- glance(fit)
expect_equal(gl$n.outliers, sum(fit$weights == 0))
expect_equal(gl$sigma, fit$scale)
expect_true(gl$r.squared > 0 && gl$r.squared < 1)
# a fit made before the standard errors were kept
old <- fit
old$std_error <- NULL
old$df_residual <- NULL
expect_true(all(is.na(tidy(old)$std.error)))
})
test_that("tidy and glance summarize RSIMCA and robust discriminant analysis", {
set.seed(5)
s <- rsimca(iris[1:4], iris$Species)
ts <- tidy(s)
expect_equal(ts$class, levels(iris$Species))
expect_equal(ts$prior, unname(s$prior))
expect_equal(ts$ncomp, unname(s$ncomp))
expect_equal(ts$cutoff.od[2], s$models$versicolor$cutoff_od)
expect_equal(ts$error, s$misclassification$error[1:3])
gs <- glance(s)
expect_equal(gs$n.classes, 3)
expect_equal(gs$error, s$misclassification$error[4])
expect_equal(gs$nobs, 150)
expect_equal(gs$method, "RSIMCA")
set.seed(6)
da <- robust_da(iris[1:4], iris$Species, method = "quadratic")
expect_named(tidy(da), c("class", "prior", "n", "error"))
gd <- glance(da)
expect_equal(gd$method, "quadratic")
expect_equal(gd$n.outliers, sum(da$weights == 0))
expect_equal(gd$nvar, 4)
})
test_that("tidy and glance summarize OPLS, SOM, wavelength selection, NAS and the domain", {
d <- tm_data()
set.seed(7)
o <- opls(d$x, d$y, ncomp = 1, crossval = 3, permutation = 3)
to <- tidy(o)
expect_equal(nrow(to), ncol(d$x))
expect_equal(to$vip, unname(o$vip))
expect_equal(tidy(o, matrix = "components")$component, o$components$component)
go <- glance(o)
expect_equal(go$ncomp.ortho, as.integer(o$summary$ort))
expect_equal(go$q2.cum, o$summary$`Q2(cum)`)
expect_false(is.na(go$p.q2))
set.seed(8)
m <- som(d$x, grid = c(3, 3))
tm <- tidy(m)
expect_equal(nrow(tm), 9)
expect_equal(sum(tm$n), 120)
used <- tm$n > 0
expect_equal(tm$qe[used], as.vector(tapply(m$qe, m$unit, mean)))
expect_true(all(is.na(tm$qe[!used])))
gm <- glance(m)
expect_equal(gm$n.units, 9)
expect_equal(gm$n.novel, sum(m$qe > m$cutoff))
w <- select_wavelengths(d$x, d$y, ncomp = 2)
tw <- tidy(w)
expect_equal(sum(tw$selected), length(w$selected))
expect_equal(glance(w)$n.selected, length(w$selected))
expect_true(is.na(glance(w)$rmsecv))
set.seed(9)
ip <- select_wavelengths(d$x, d$y, method = "ipls", ncomp = 2, intervals = 5, folds = 3)
ti <- tidy(ip)
expect_equal(as.vector(table(ti$interval)), as.vector(ip$intervals$size))
expect_equal(glance(ip)$rmsecv, utils::tail(ip$path$RMSECV, 1))
n <- nas(d$x, d$y, ncomp = 2, noise = 1)
expect_equal(tidy(n)$estimate, unname(n$coefficients))
gn <- glance(n)
expect_equal(gn$sensitivity, unname(n$figures_of_merit[["sensitivity"]]))
expect_equal(gn$lod, unname(n$figures_of_merit[["lod"]]))
expect_true(is.na(glance(nas(d$x, d$y, ncomp = 2))$lod))
ad <- applicability_domain(d$x, ncomp = 3)
td <- tidy(ad)
expect_equal(td$cumulative[3], ad$explained)
gd <- glance(ad)
expect_equal(gd$t2.limit, unname(ad$limits[["t2"]]))
expect_equal(gd$ncomp, 3)
})
test_that("tidy and glance reach the engines of parsnip fits", {
skip_if_not_installed("parsnip")
d <- tm_data()
df <- data.frame(K = d$y, d$x[, 30:32])
set.seed(10)
fit <- parsnip::fit(parsnip::set_engine(parsnip::linear_reg(), "lts"), K ~ ., data = df)
expect_equal(tidy(fit), tidy(parsnip::extract_fit_engine(fit)))
expect_equal(glance(fit), glance(parsnip::extract_fit_engine(fit)))
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.