Function that builds an arid_linreg class model object that provides sklearn linear regression interface functionality and attributes. The arid_linreg function instantiates a linear regression model type based on the input specifications and provides methods to fit/predict/score the results and retrieve the calculated sklearn coefficients.

arid_linreg(regularization = NULL, lambda = NULL)

Arguments

regularization

(character): A string defining NULL,'L1','L2', or 'L1L2' linear coefficient regularization (default: NULL)

lambda

(double):the numeric regularization strength value

Value

arid_linreg: an arid_linreg class linear regression model object

Examples

model <- arid_linreg() model$fit(matrix(1:12, nrow = 3, ncol = 3), as.matrix(c(1,2,0)))
#> $thisEnv #> <environment: 0x0000000044d1d950> #> #> $fit #> function (X, y) #> { #> if (is.null(X) | length(X) < 1) { #> stop("ERROR: Invalid input X feature values to fit") #> } #> if (is.null(y) | length(y) < 1) { #> stop("ERROR: Invalid input y response value to fit") #> } #> if (is.list(X)) { #> if (length(dplyr::select_if(X, is.numeric) != length(X))) { #> warning("WARNING: Dropping non-numeric input features in X") #> X <- X %>% dplyr::select_if(is.numeric) #> } #> X <- data.matrix(X) #> } #> if (is.list(y)) { #> warning("WARNING: Input y to fit is a list, converting to matrix") #> y <- as.matrix(y) #> } #> if (!is.numeric(y)) { #> stop("ERROR: Response y is not numeric") #> } #> if (nrow(X) != nrow(y)) { #> stop("ERROR: Input samples X and responses y not the same length") #> } #> model <- NULL #> if (is.null(regularization_)) { #> lambda_ <- 0 #> model <- glmnet::glmnet(X, y, family = "gaussian", alpha = 1, #> lambda = lambda_) #> } #> else if (regularization == c("L1")) { #> model <- glmnet::glmnet(X, y, alpha = 1, family = "gaussian", #> lambda = lambda_) #> } #> else if (regularization == c("L2")) { #> model <- glmnet::glmnet(X, y, alpha = 0, family = "gaussian", #> lambda = lambda_) #> } #> else if (regularization == c("L1L2")) { #> model <- glmnet::glmnet(X, y, alpha = 0.5, family = "gaussian", #> lambda = lambda_) #> } #> coefs <- .get_coefs(X, y, model, lambda_) #> arid_linreg$intercept_ <- coefs[1] #> arid_linreg$coef_ <- coefs[c(-1)] #> assign("coef_", coefs[c(-1)], thisEnv) #> assign("model_", model, thisEnv) #> return(arid_linreg) #> } #> <bytecode: 0x0000000044d231a8> #> <environment: 0x0000000044d1d950> #> #> $predict #> function (newx) #> { #> if (is.null(newx) | length(newx) < 1) { #> stop("ERROR: Invalid input new X sample values to predict") #> } #> if (is.null(model_)) { #> stop("ERROR: Must fit model before predicting") #> } #> if (length(coef_) != ncol(newx)) { #> stop("ERROR: Incorrect number of features in newx samples") #> } #> return(glmnet::predict.glmnet(model_, s = lambda_, newx = newx)) #> } #> <bytecode: 0x0000000044d1c450> #> <environment: 0x0000000044d1d950> #> #> $score #> function () #> { #> if (is.null(model_)) { #> stop("ERROR: Must fit model before scoring") #> } #> max(model_$dev.ratio) #> } #> <bytecode: 0x0000000044d1d2f8> #> <environment: 0x0000000044d1d950> #> #> $intercept_ #> [1] 2 #> #> $coef_ #> [1] -5.000000e-01 -3.330669e-16 -1.232595e-31 #> #> attr(,"class") #> [1] "arid_linreg"
model$predict(t(as.matrix(c(1,2,2))))
#> 1 #> [1,] 1.5
model$score()
#> [1] 0.25