flash_backfit: Refines a fit of the flash model to data by "backfitting".

View source: R/backfit.R

flash_backfitR Documentation

Refines a fit of the flash model to data by "backfitting".

Description

Iterates through the factors of a flash fit object, updating each until convergence.

Usage

flash_backfit(data, f_init, kset = NULL, var_type = c("by_column",
  "by_row", "constant", "zero", "kroneker"), tol = 0.01,
  ebnm_fn = "ebnm_pn", ebnm_param = NULL, verbose = TRUE,
  nullcheck = TRUE, maxiter = 1000)

Arguments

data

An n by p matrix or a flash data object created using flash_set_data.

f_init

A flash object or flash fit object to be refined.

kset

The indices of factors to be optimized (NULL indicates all factors).

var_type

The type of variance structure to assume for residuals. Options include:

"by_column"

Residuals in any given column are assumed to have the same variance.

"by_row"

Residuals in any given row have the same variance.

"constant"

All residuals are assumed to have the same variance.

"zero"

The variance of the residuals is fixed. To use this variance type, the standard errors must be specified via parameter S when using flash_set_data to set the flash data object.

"kroneker"

This variance type has not yet been implemented.

tol

Specifies how much the objective can change in a single iteration to be considered not converged.

ebnm_fn

The function used to solve the Empirical Bayes Normal Means problem. Either a single character string (giving the name of of the function) or a list with fields l and f (specifying different functions to be used for loadings and factors) are acceptable arguments. Options include:

"ebnm_ash"

A wrapper to the function ash.

"ebnm_pn"

A wrapper to function ebnm_point_normal in package ebnm.

"ebnm_pl"

A wrapper to function ebnm_point_laplace in ebnm.

ebnm_param

A named list containing parameters to be passed to ebnm_fn when optimizing. A list with fields l and f (each of which is a named list) will separately supply parameters for the loadings and factors. An unnamed list of length(kset) named lists will separately supply parameters for each factor/loading in kset. Finally, a list with fields l and f, each of which contains an unnamed list of length(kset) named lists, will separately supply parameters for each distinct loading and each distinct factor. Set to NULL to use defaults.

verbose

If TRUE, various progress updates will be printed.

nullcheck

If TRUE, then after running hill-climbing updates flash will check whether the achieved optimum is better than setting the factor to zero. If the check is performed and fails then the factor will be set to zero in the returned fit.

maxiter

A maximum number of iterations to perform (not including repeated fittings if nullcheck fails). To perform just one iteration we suggest setting maxiter = 1 and nullcheck = FALSE.

Value

A flash object.

Examples


LL = matrix(rnorm(200), ncol=2) # simulate some rank 2 data
FF = matrix(rnorm(20), nrow=2)
Y = LL %*% FF + matrix(rnorm(1000), nrow=100)
fg = flash_add_greedy(Y, 10)
fb = flash_backfit(Y, fg) # refines fit from greedy by backfitting
fb$ldf$d

# Example to illustrate different types of arguments to ebnm_param.
# 1. Fix a N(0, 1) prior on the loadings.
ebnm_param_l = list(g=ashr::normalmix(1,0,1), fixg=TRUE)
fg2 = flash_add_greedy(Y, 10, ebnm_fn="ebnm_ash",
                       ebnm_param=list(l=ebnm_param_l, f=list()))
# 2. Now refit factors, forcing loadings to use prior from greedy fit.
ebnm_param_f = lapply(fg2$gf, function(g) {list(g=g, fixg=TRUE)})
fb2 = flash_backfit(Y, fg2, kset=1:2, ebnm_fn="ebnm_ash",
                    ebnm_param=list(l=list(), f=ebnm_param_f))


stephenslab/flashr2 documentation built on Feb. 6, 2024, 5:21 a.m.