create_method: Create a new 'Method'

View source: R/method.R

create_methodR Documentation

Create a new Method

Description

Create a Method which can fit() data in an Experiment.

Usage

create_method(.method_fun, .name = NULL, ...)

Arguments

.method_fun

The user-defined method function.

.name

(Optional) The name of the Method, helpful for later identification.

...

User-defined default arguments to pass into .method_fun().

Value

A new Method object.

Examples

# generate some data
dgp_fun <- function(n, beta, rho, sigma) {
  cov_mat <- matrix(c(1, rho, rho, 1), byrow = TRUE, nrow = 2, ncol = 2)
  X <- MASS::mvrnorm(n = n, mu = rep(0, 2), Sigma = cov_mat)
  y <- X %*% beta + rnorm(n, sd = sigma)
  return(list(X = X, y = y))
}

dgp <- create_dgp(.dgp_fun = dgp_fun,
                  .name = "Linear Gaussian DGP",
                  n = 50, beta = c(1, 0), rho = 0, sigma = 1)
data_corr <- dgp$generate(rho = 0.7)

# create an example Method function
lm_fun <- function(X, y, cols) {
  X <- X[, cols]
  lm_fit <- lm(y ~ X)
  pvals <- summary(lm_fit)$coefficients[-1, "Pr(>|t|)"] %>%
    setNames(paste(paste0("X", cols), "p-value"))
  return(pvals)
}

# create Method with default argument `cols`
lm_method <- create_method(
  .method_fun = lm_fun,
  .name = "OLS",
  cols = c(1, 2)
)

print(lm_method)

# fit the Method on data with non-default arguments
lm_method$fit(data_corr, cols = 2)

# fit the Method on data with default arguments
lm_method$fit(data_corr)


Yu-Group/simChef documentation built on March 25, 2024, 3:22 a.m.