R/linear.R

Defines functions gen_longlinear

Documented in gen_longlinear

#' Generate Long Linear Data
#'
#' This function generates a dataset consisting of long linear data.
#'
#' @param n A numeric value (default: 500) representing the sample size.
#' @param p A numeric value (default: 4) representing the number of dimensions.
#' @return A data containing the long linear data.
#' @export
#'
#' @examples
#' set.seed(20240412)
#' longlinear <- gen_longlinear(n = 500, p = 4)
gen_longlinear <- function(n = 500, p = 4) {

  if (p < 2) {
    cli::cli_abort("p should be greater than 2.")
  }

  if (n <= 0){
    cli::cli_abort("n should be positive.")
  }

  df <- tibble::tibble()

  data_list <- lapply(1:p, function(i) rep(0, n))
  names(data_list) <- paste0("x", 1:p)

  df <- tibble::tibble(!!!data_list)

  scale_fac_vec <- stats::runif(p, -10, 10)
  shift_fac_vec <- stats::runif(p, -300, 300)

  for (i in 1:p) {

    df[, i] <- scale_fac_vec[i] * (0:(n - 1) + 0.03 * n * stats::rnorm(n) + shift_fac_vec[i])

  }

  cli::cli_alert_success("Data generation completed successfully!!!")
  return(df)
}

Try the cardinalR package in your browser

Any scripts or data that you put into this service are public.

cardinalR documentation built on Aug. 21, 2025, 5:27 p.m.