R Under development (unstable) (2024-02-14 r85901 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(cpr) > > # Test if the CPR algorithm can recover a known spline > > recover_spline <- function(k = 4L, start_with = 100L, seed, theta_dist_sd = 100, ...) { + + if (missing(seed)) { + seed <- round(stats::runif(1) * 1e9) + } + + set.seed(seed) + + n_iknots <- sample(seq(1, floor(start_with / 4), by = 1L), 1L) + true_iknots <- sort(stats::runif(n_iknots)) + true_theta <- stats::rnorm(n_iknots + k, sd = theta_dist_sd) + + xvec <- runif(10000) + + true_bmat <- bsplines(xvec, iknots = true_iknots, bknots = c(0, 1), order = k) + + true_cp <- cp(true_bmat, true_theta) + + s_data <- data.frame(x = xvec, + y = as.numeric(true_bmat %*% matrix(true_theta, ncol = 1))) + + initial_iknots <- sort(c(stats::runif(start_with - n_iknots), true_iknots)) + + f <- substitute(y ~ bsplines(x, iknots = IKNOTS, bknots = c(0, 1), order = K), list(IKNOTS = initial_iknots, K = k)) + f <- stats::as.formula(f) + environment(f) <- environment() + + initial_cp <- suppressWarnings(do.call(cp, list(formula = f, data = s_data))) + + cpr_run <- cpr(initial_cp) + + found_cp <- cpr_run[[n_iknots + 1L]] + + out <- + list(recovered = isTRUE(all.equal(as.matrix(true_cp$cp), as.matrix(found_cp$cp))), + call = match.call(), + n_iknots = n_iknots, + start_with = start_with, + true_cp = true_cp, + initial_cp = initial_cp, + found_cp = found_cp, + cpr_run = cpr_run, + seed = seed + ) + + out + } > > stopifnot( + recover_spline(start_with = 40L, seed = 42)$recovered + ) | | | 0% | |== | 2% | |=== | 5% | |===== | 7% | |======= | 10% | |========= | 12% | |========== | 15% | |============ | 17% | |============== | 20% | |=============== | 22% | |================= | 24% | |=================== | 27% | |==================== | 29% | |====================== | 32% | |======================== | 34% | |========================== | 37% | |=========================== | 39% | |============================= | 41% | |=============================== | 44% | |================================ | 46% | |================================== | 49% | |==================================== | 51% | |====================================== | 54% | |======================================= | 56% | |========================================= | 59% | |=========================================== | 61% | |============================================ | 63% | |============================================== | 66% | |================================================ | 68% | |================================================== | 71% | |=================================================== | 73% | |===================================================== | 76% | |======================================================= | 78% | |======================================================== | 80% | |========================================================== | 83% | |============================================================ | 85% | |============================================================= | 88% | |=============================================================== | 90% | |================================================================= | 93% | |=================================================================== | 95% | |==================================================================== | 98% | |======================================================================| 100% > ################################################################################ > ## End of File ## > ################################################################################ > > proc.time() user system elapsed 5.62 0.39 6.00