| Title: | Perform AnchorRegression |
|---|---|
| Description: | Performs AnchorRegression proposed by Rothenhäusler et al. 2020. The code is adapted from the original paper repository. (<https://github.com/rothenhaeusler/anchor-regression>) The code was developed independently from the authors of the paper. |
| Authors: | Simon Zimmermann |
| Maintainer: | Simon Zimmermann <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.3 |
| Built: | 2026-05-13 05:32:51 UTC |
| Source: | https://github.com/simzim96/anchorregression |
Perform a prediction for an Anchor Regression model as described in Rothenhäusler et al.2020
anchor_prediction(anchor_model, x, anchor, gamma, target_variable)anchor_prediction(anchor_model, x, anchor, gamma, target_variable)
anchor_model |
is the Anchor Regression model object |
x |
is a dataframe containing the matrix x containing the independent variables |
anchor |
is a dataframe containing the matrix anchor containing the anchor variable |
gamma |
is the regularization parameter for the Anchor Regression |
target_variable |
is the target variable name contained in the x dataframe |
A list of predictions.
x <- as.data.frame(matrix(data = rnorm(100),nrow = 100,ncol = 10)) anchor <- as.data.frame(matrix(data = rnorm(200),nrow = 100,ncol = 2)) colnames(anchor) <- c('X1','X2') gamma <- 2 target_variable <- 'V2' anchor_model <- anchor_regression(x, anchor, gamma, target_variable) anchor_prediction(anchor_model$model, x, anchor, gamma, target_variable)x <- as.data.frame(matrix(data = rnorm(100),nrow = 100,ncol = 10)) anchor <- as.data.frame(matrix(data = rnorm(200),nrow = 100,ncol = 2)) colnames(anchor) <- c('X1','X2') gamma <- 2 target_variable <- 'V2' anchor_model <- anchor_regression(x, anchor, gamma, target_variable) anchor_prediction(anchor_model$model, x, anchor, gamma, target_variable)
Perform a prediction for an Anchor Regression model as described in Rothenhäusler et al.2020
anchor_prediction_gam( anchor_model, x, anchor, gamma, target_variable, bin_factor )anchor_prediction_gam( anchor_model, x, anchor, gamma, target_variable, bin_factor )
anchor_model |
is the Anchor Regression model object |
x |
is a dataframe containing the matrix x containing the independent variables |
anchor |
is a dataframe containing the matrix anchor containing the anchor variable |
gamma |
is the regularization parameter for the Anchor Regression |
target_variable |
is the target variable name contained in the x dataframe |
bin_factor |
binary variable that can be transformed to a factor to partial out effects |
A list of predictions.
x <- as.data.frame(matrix(data = rnorm(10000),nrow = 1000,ncol = 10)) x$bin <- sample(nrow(x),x = c(1,0),prob = c(0.5,0.5),replace = TRUE) anchor <- as.data.frame(matrix(data = rnorm(2000),nrow = 1000,ncol = 2)) colnames(anchor) <- c('X1','X2') gamma <- 2 target_variable <- 'V2' anchor_model <- anchor_regression_gam(x, anchor, gamma, target_variable,"bin") anchor_prediction_gam(anchor_model$model, x, anchor, gamma, target_variable,"bin")x <- as.data.frame(matrix(data = rnorm(10000),nrow = 1000,ncol = 10)) x$bin <- sample(nrow(x),x = c(1,0),prob = c(0.5,0.5),replace = TRUE) anchor <- as.data.frame(matrix(data = rnorm(2000),nrow = 1000,ncol = 2)) colnames(anchor) <- c('X1','X2') gamma <- 2 target_variable <- 'V2' anchor_model <- anchor_regression_gam(x, anchor, gamma, target_variable,"bin") anchor_prediction_gam(anchor_model$model, x, anchor, gamma, target_variable,"bin")
Perform an Anchor Regression as described in Rothenhäusler et al.2020
anchor_regression(x, anchor, gamma, target_variable, lambda = "CV")anchor_regression(x, anchor, gamma, target_variable, lambda = "CV")
x |
is a dataframe containing the matrix x containing the independent variables |
anchor |
is a dataframe containing the matrix anchor containing the anchor variable |
gamma |
is the regularization parameter for the Anchor Regression |
target_variable |
is the target variable name contained in the x dataframe |
lambda |
indicates the lambda that is used in the Anchor Regression. 'CV' is used if it should be estimated by cross validation on the full subset. |
A list with coefficient values and a list with the respective names overview_print. Additionally the transformed data as x and y plus the fixed lambda coefficient.
x <- as.data.frame(matrix(data = rnorm(1000),nrow = 100,ncol = 10)) anchor <- as.data.frame(matrix(data = rnorm(200),nrow = 100,ncol = 2)) colnames(anchor) <- c('X1','X2') gamma <- 2 target_variable <- 'V2' anchor_regression(x, anchor, gamma, target_variable)x <- as.data.frame(matrix(data = rnorm(1000),nrow = 100,ncol = 10)) anchor <- as.data.frame(matrix(data = rnorm(200),nrow = 100,ncol = 2)) colnames(anchor) <- c('X1','X2') gamma <- 2 target_variable <- 'V2' anchor_regression(x, anchor, gamma, target_variable)
Perform an Generalized Additive Anchor Regression
anchor_regression_gam( x, anchor, gamma, target_variable, bin_factor = NULL, force_binary = TRUE )anchor_regression_gam( x, anchor, gamma, target_variable, bin_factor = NULL, force_binary = TRUE )
x |
is a dataframe containing the matrix x containing the independent variables |
anchor |
is a dataframe containing the matrix anchor containing the anchor variable |
gamma |
is the regularization parameter for the Anchor Regression |
target_variable |
is the target variable name contained in the x dataframe |
bin_factor |
factor variable that can be transformed to a factor to partial out effects |
force_binary |
if set to TRUE forces bin_factor to be binary |
A list with coefficient values and a list with the respective names overview_print. Additionally the transformed data as x and y plus the fixed lambda coefficient.
x <- as.data.frame(matrix(data = rnorm(10000),nrow = 1000,ncol = 10)) x$bin <- sample(nrow(x),x = c(1,0),prob = c(0.5,0.5),replace = TRUE) anchor <- as.data.frame(matrix(data = rnorm(2000),nrow = 1000,ncol = 2)) colnames(anchor) <- c('X1','X2') gamma <- 2 target_variable <- 'V2' anchor_regression_gam(x, anchor, gamma, target_variable,bin_factor = "bin")x <- as.data.frame(matrix(data = rnorm(10000),nrow = 1000,ncol = 10)) x$bin <- sample(nrow(x),x = c(1,0),prob = c(0.5,0.5),replace = TRUE) anchor <- as.data.frame(matrix(data = rnorm(2000),nrow = 1000,ncol = 2)) colnames(anchor) <- c('X1','X2') gamma <- 2 target_variable <- 'V2' anchor_regression_gam(x, anchor, gamma, target_variable,bin_factor = "bin")
Perform an Anchor Stability Analysis as described in Rothenhäusler et al.2020
anchor_stability( x, anchor, target_variable, lambda = 0, alpha = 0.05, p_procedure = "bootstrap" )anchor_stability( x, anchor, target_variable, lambda = 0, alpha = 0.05, p_procedure = "bootstrap" )
x |
is a dataframe containing the matrix x containing the independent variables |
anchor |
is a dataframe containing the matrix anchor containing the anchor variable |
target_variable |
is the target variable name contained in the x dataframe |
lambda |
indicates the lambda that is used in the Anchor Regression. 'CV' is used if it should be estimated by cross validation on the full subset. |
alpha |
significance level for test decision on coefficient significance |
p_procedure |
procedure to estimate stability. Option 1: naive - stable if effect is non-zero in all cases; Option 2: post-lasso - post selection inference using SelectiveInference package |
A dataframe containing the stability values for each coefficient
x <- as.data.frame(matrix(data = rnorm(1000),nrow = 100,ncol = 10)) anchor <- as.data.frame(matrix(data = rnorm(200),nrow = 100,ncol = 2)) colnames(anchor) <- c('X1','X2') gamma <- 2 target_variable <- 'V2' anchor_stability(x, anchor, target_variable, lambda, alpha=0.05, p_procedure = "naive")x <- as.data.frame(matrix(data = rnorm(1000),nrow = 100,ncol = 10)) anchor <- as.data.frame(matrix(data = rnorm(200),nrow = 100,ncol = 2)) colnames(anchor) <- c('X1','X2') gamma <- 2 target_variable <- 'V2' anchor_stability(x, anchor, target_variable, lambda, alpha=0.05, p_procedure = "naive")
Perform a prediction for a Weighted Anchor Regression model
weighted_anchor_prediction(names, coeff, x, anchor, gamma, target_variable)weighted_anchor_prediction(names, coeff, x, anchor, gamma, target_variable)
names |
list of variable names corresponding to the coefficients in coeff |
coeff |
list of coefficients corresponding to the coefficients in names |
x |
is a dataframe containing the matrix x containing the independent variables |
anchor |
is a dataframe containing the matrix anchor containing the anchor variable |
gamma |
is the regularization parameter for the Anchor Regression |
target_variable |
is the target variable name contained in the x dataframe |
A list of predictions.
# number of observed environments environments <- 10 # populate list with generated data of x and anchor data_x_list <- c() data_anchor_list <- c() for(e in 1:environments){ x <- as.data.frame(matrix(data = rnorm(100),nrow = 100,ncol = 10)) anchor <- as.data.frame(matrix(data = rnorm(200),nrow = 100,ncol = 2)) colnames(anchor) <- c('X1','X2') data_x_list[[e]] <- x data_anchor_list[[e]] <- anchor } # estimate model gamma <- 2 target_variable <- 'V2' weighted_anchor_model <- weighted_anchor_regression(data_x_list, data_anchor_list, gamma, target_variable, anchor_model_pre=NULL, test_split=0.4, lambda=0) weighted_anchor_prediction(weighted_anchor_model$names, weighted_anchor_model$coeff, x, anchor, gamma, target_variable)# number of observed environments environments <- 10 # populate list with generated data of x and anchor data_x_list <- c() data_anchor_list <- c() for(e in 1:environments){ x <- as.data.frame(matrix(data = rnorm(100),nrow = 100,ncol = 10)) anchor <- as.data.frame(matrix(data = rnorm(200),nrow = 100,ncol = 2)) colnames(anchor) <- c('X1','X2') data_x_list[[e]] <- x data_anchor_list[[e]] <- anchor } # estimate model gamma <- 2 target_variable <- 'V2' weighted_anchor_model <- weighted_anchor_regression(data_x_list, data_anchor_list, gamma, target_variable, anchor_model_pre=NULL, test_split=0.4, lambda=0) weighted_anchor_prediction(weighted_anchor_model$names, weighted_anchor_model$coeff, x, anchor, gamma, target_variable)
Estimates weighted Anchor Regression coefficients
weighted_anchor_regression( data_x_list, data_anchor_list, gamma, target_variable, anchor_model_pre = NULL, test_split = 0.4, lambda = 0 )weighted_anchor_regression( data_x_list, data_anchor_list, gamma, target_variable, anchor_model_pre = NULL, test_split = 0.4, lambda = 0 )
data_x_list |
list containing coefficient dataframes for different environments |
data_anchor_list |
list containing anchor dataframes for different environments |
gamma |
is the regularization parameter for the Anchor Regression |
target_variable |
is the target variable name contained in the x dataframe |
anchor_model_pre |
is the pre estimated model for the Anchor Regression. In case of NULL a new model is estimated. |
test_split |
is desired test/train split for the estimation |
lambda |
penalization coefficient for Anchor Shrinkage. Initially set to 0. |
A list estimated coefficients with names, weights and the raw coefficient matrix
environments <- 10 # number of observed environments # populate list with generated data of x and anchor data_x_list <- c() data_anchor_list <- c() for(e in 1:environments){ x <- as.data.frame(matrix(data = rnorm(100),nrow = 100,ncol = 10)) anchor <- as.data.frame(matrix(data = rnorm(200),nrow = 100,ncol = 2)) colnames(anchor) <- c('X1','X2') data_x_list[[e]] <- x data_anchor_list[[e]] <- anchor } # estimate model gamma <- 2 target_variable <- 'V2' weighted_anchor_regression(data_x_list, data_anchor_list, gamma, target_variable, anchor_model_pre=NULL, test_split=0.4, lambda=0)environments <- 10 # number of observed environments # populate list with generated data of x and anchor data_x_list <- c() data_anchor_list <- c() for(e in 1:environments){ x <- as.data.frame(matrix(data = rnorm(100),nrow = 100,ncol = 10)) anchor <- as.data.frame(matrix(data = rnorm(200),nrow = 100,ncol = 2)) colnames(anchor) <- c('X1','X2') data_x_list[[e]] <- x data_anchor_list[[e]] <- anchor } # estimate model gamma <- 2 target_variable <- 'V2' weighted_anchor_regression(data_x_list, data_anchor_list, gamma, target_variable, anchor_model_pre=NULL, test_split=0.4, lambda=0)