[Fast, recommended when the number of variables is small] Adds the best variable or drops the worst variable one at a time in the latent variables. You can select the desired search criterion (AIC, BIC, cross-validation error, cross-validation AUC) to determine which variable is the best/worst and should be added/dropped. Note that when the number of variables in G and E is large, this does not generally converge to the optimal subset, this function is only recommended when you have a small number of variables (e.g. 2 environments, 6 genetic variants). If using cross-validation (search_criterion="cv"
or search_criterion="cv_AUC"
), to prevent cross-validating with each variable (extremely slow), we recommend setting a p-value threshold (p_threshold
) and forcing the algorithm not to look at models with bigger AIC (exclude_worse_AIC=TRUE
).
stepwise_search_IM(
data,
formula,
interactive_mode = FALSE,
latent_var_original = NULL,
latent_var_extra = NULL,
search_type = "bidirectional-forward",
search = 0,
search_criterion = "AIC",
forward_exclude_p_bigger = 0.2,
backward_exclude_p_smaller = 0.01,
exclude_worse_AIC = TRUE,
max_steps = 100,
cv_iter = 5,
cv_folds = 10,
folds = NULL,
Huber_p = 1.345,
classification = FALSE,
start_latent_var = NULL,
eps = 0.01,
maxiter = 100,
family = gaussian,
ylim = NULL,
seed = NULL,
print = TRUE,
remove_miss = FALSE,
test_only = FALSE
)
Returns an object of the class "IMLEGIT" which is list containing, in the following order: a glm fit of the main model, a list of the glm fits of the latent variables and a list of the true model parameters (AIC, BIC, rank, df.residual, null.deviance) for which the individual model parts (main, genetic, environmental) don't estimate properly.
data.frame of the dataset to be used.
Model formula. The names of latent_var
can be used in the formula to represent the latent variables. If names(latent_var
) is NULL, then L1, L2, ... can be used in the formula to represent the latent variables. Do not manually code interactions, write them in the formula instead (ex: G*E1*E2 or G:E1:E2).
If TRUE, uses interactive mode. In interactive mode, at each iteration, the user is shown the AIC, BIC, p-value and also the cross-validation \(R^2\) if search_criterion="cv"
and the cross-validation AUC if search_criterion="cv_AUC"
for the best 5 variables. The user must then enter a number between 1 and 5 to select the variable to be added, entering anything else will stop the search.
list of data.frame. The elements of the list are the datasets used to construct each latent variable. For interpretability and proper convergence, not using the same variable in more than one latent variable is highly recommended. It is recommended to set names to the list elements to prevent confusion because otherwise, the latent variables will be named L1, L2, ...
list of data.frame (with the same structure as latent_var_original) containing the additionnal elements to try including inside the latent variables. Set to NULL if using a backward search.
If search_type="forward"
, uses a forward search. If search_type="backward"
, uses backward search. If search_type="bidirectional-forward"
, uses bidirectional search (that starts as a forward search). If search_type="bidirectional-backward"
, uses bidirectional search (that starts as a backward search).
If search=0
, uses a stepwise search for all latent variables. Otherwise, if search = i, uses a stepwise search on the i-th latent variable (Default = 0).
Criterion used to determine which variable is the best to add or worst to drop. If search_criterion="AIC"
, uses the AIC, if search_criterion="AICc"
, uses the AICc, if search_criterion="BIC"
, uses the BIC, if search_criterion="cv"
, uses the cross-validation error, if
search_criterion="cv_AUC"
, uses the cross-validated AUC, if search_criterion="cv_Huber"
, uses the Huber cross-validation error, if search_criterion="cv_L1"
, uses the L1-norm cross-validation error (Default = "AIC"). The Huber and L1-norm cross-validation errors are alternatives to the usual cross-validation L2-norm error (which the \(R^2\) is based on) that are more resistant to outliers, the lower the values the better.
If p-value > forward_exclude_p_bigger
, we do not consider the variable for inclusion in the forward steps (Default = .20). This is an exclusion option which purpose is skipping variables that are likely not worth looking to make the algorithm faster, especially with cross-validation. Set to 1 to prevent any exclusion here.
If p-value < backward_exclude_p_smaller
, we do not consider the variable for removal in the backward steps (Default = .01). This is an exclusion option which purpose is skipping variables that are likely not worth looking to make the algorithm faster, especially with cross-validation. Set to 0 to prevent any exclusion here.
If AIC with variable > AIC without variable, we ignore the variable (Default = TRUE). This is an exclusion option which purpose is skipping variables that are likely not worth looking to make the algorithm faster, especially with cross-validation. Set to FALSE to prevent any exclusion here.
Maximum number of steps taken (Default = 50).
Number of cross-validation iterations (Default = 5).
Number of cross-validation folds (Default = 10). Using cv_folds=NROW(data)
will lead to leave-one-out cross-validation.
Optional list of vectors containing the fold number for each observation. Bypass cv_iter and cv_folds. Setting your own folds could be important for certain data types like time series or longitudinal data.
Parameter controlling the Huber cross-validation error (Default = 1.345).
Set to TRUE if you are doing classification (binary outcome).
Optional list of starting points for each latent variable (The list must have the same length as the number of latent variables and each element of the list must have the same length as the number of variables of the corresponding latent variable).
Threshold for convergence (.01 for quick batch simulations, .0001 for accurate results).
Maximum number of iterations.
Outcome distribution and link function (Default = gaussian).
Optional vector containing the known min and max of the outcome variable. Even if your outcome is known to be in [a,b], if you assume a Gaussian distribution, predict() could return values outside this range. This parameter ensures that this never happens. This is not necessary with a distribution that already assumes the proper range (ex: [0,1] with binomial distribution).
Seed for cross-validation folds.
If TRUE, print all the steps and notes/warnings. Highly recommended unless you are batch running multiple stepwise searchs. (Default=TRUE).
If TRUE, remove missing data completely, otherwise missing data is only removed when adding or dropping a variable (Default = FALSE).
If TRUE, only uses the first fold for training and predict the others folds; do not train on the other folds. So instead of cross-validation, this gives you train/test and you get the test R-squared as output.
if (FALSE) {
## Example
train = example_3way_3latent(250, 1, seed=777)
# Forward search for genes based on BIC (in interactive mode)
forward_genes_BIC = stepwise_search_IM(train$data,
latent_var_original=list(G=NULL, E=train$latent_var$E, Z=train$latent_var$Z),
latent_var_extra=list(G=train$latent_var$G,E=NULL,Z=NULL),
formula=y ~ E*G*Z,search_type="forward", search=1, search_criterion="BIC",
interactive_mode=TRUE)
# Bidirectional-backward search for everything based on AIC
bidir_backward_AIC = stepwise_search_IM(train$data, latent_var_extra=NULL,
latent_var_original=train$latent_var,
formula=y ~ E*G*Z,search_type="bidirectional-backward", search=0, search_criterion="AIC")
}
Run the code above in your browser using DataLab