#-------------------------------------- # Shared Test Fixtures for pffr Tests #-------------------------------------- # # This file provides lazily-computed shared fixtures to reduce redundant # data generation and model fitting across tests. Each fixture is computed # once and cached for reuse within a test session. # # Usage: Call the getter function (e.g., get_basic_pffr_data()) to retrieve # the fixture. The first call computes it; subsequent calls return the cache. # Environment for caching fixtures pffr_test_env <- new.env(parent = emptyenv()) #-------------------------------------- # Basic Data Fixtures #-------------------------------------- #' Get basic pffr data with ff(X1) + xlin terms #' #' Generates a small dataset suitable for testing core pffr functionality. #' Uses a fixed seed for reproducibility. #' #' @returns A pffr_simulate result with n=30, nxgrid=25, nygrid=30 get_basic_pffr_data <- function() { if (is.null(pffr_test_env$basic_data)) { set.seed(12345) pffr_test_env$basic_data <- pffr_simulate( Y ~ ff(X1, xind = s) + xlin, n = 30, nxgrid = 25, nygrid = 30, effects = list(X1 = "cosine", xlin = "dnorm"), SNR = 50 ) } pffr_test_env$basic_data } #' Get simple xlin-only data #' #' Generates a minimal dataset with only a linear varying coefficient term. #' Useful for testing algorithm variants and basic functionality. #' #' @param n Number of observations (default: 30) #' @param nygrid Number of y-grid points (default: 25) #' @param SNR Signal-to-noise ratio (default: 50) #' @returns A pffr_simulate result get_xlin_data <- function(n = 30, nygrid = 25, SNR = 50) { cache_key <- paste0("xlin_data_", n, "_", nygrid, "_", SNR) if (is.null(pffr_test_env[[cache_key]])) { set.seed(23456) pffr_test_env[[cache_key]] <- pffr_simulate( Y ~ xlin, n = n, nygrid = nygrid, effects = list(xlin = "dnorm"), SNR = SNR ) } pffr_test_env[[cache_key]] } #' Get multi-term data for comprehensive tests #' #' Generates data with multiple term types: ff, xlin, s(xsmoo), c(xconst). #' Useful for testing term extraction, predict(type="terms"), etc. #' #' @returns A pffr_simulate result with n=35, nxgrid=25, nygrid=30 get_multiterm_data <- function() { if (is.null(pffr_test_env$multiterm_data)) { set.seed(34567) pffr_test_env$multiterm_data <- pffr_simulate( Y ~ ff(X1, xind = s) + xlin + s(xsmoo) + c(xconst), n = 35, nxgrid = 25, nygrid = 30, effects = list( X1 = "cosine", xlin = "dnorm", xsmoo = "sine", xconst = 2 ), SNR = 50 ) } pffr_test_env$multiterm_data } #' Get historical ff (limits) data #' #' Generates data with ff term using limits="s