Skip to contents

Method which can fit() data in an Experiment.

Generally speaking, users won't directly interact with the Method R6 class, but instead indirectly through create_method() and the following Experiment helpers:

See also

Public fields

name

The name of the Method.

method_fun

The user-defined method function.

method_params

A (named) list of default parameters to input into the method function.

Methods


Method new()

Initialize a new Method object.

Usage

Method$new(.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().

Returns

A new instance of Method.


Method fit()

Fit a Method on data using the provided Method parameters.

Usage

Method$fit(data_list, ..., .simplify = TRUE)

Arguments

data_list

List of data to pass into Method$method_fun(). If named, should match arguments in Method$method_fun().

...

User-defined arguments to pass into Method$method_fun() that will overwrite the initialized Method parameters. If no additional arguments are provided, the Method will be fit using Method$method_fun() and the parameters that were set when Method$new() was called.

.simplify

If TRUE, remove list wrapping from any column that has scalar values.

method

A Method object.

Returns

Result of Method$method_fun(), coerced into a single tibble row.


Method print()

Print a Method in a nice format, showing the Method's name, function, and parameters.

Usage

Method$print()

Returns

The original Method object, invisibly.


Method clone()

The objects of this class are cloneable with this method.

Usage

Method$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

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(names(.), "p-value"))
  return(pvals)
}

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

print(lm_method)
#> Method Name: OLS 
#>    Function: function (X, y, cols)  
#>    Parameters: List of 1
#>      $ cols: num [1:2] 1 2

# fit the Method on data with non-default arguments
lm_method$fit(data_corr, cols = 2)
#> # A tibble: 1 × 1
#>    ` p-value`
#>         <dbl>
#> 1 0.000000318

# fit the Method on data with default arguments
lm_method$fit(data_corr)
#> # A tibble: 1 × 2
#>   `X1 p-value` `X2 p-value`
#>          <dbl>        <dbl>
#> 1      0.00135        0.107