## ============================================================
## Example 1: Binary outcome (binomial LASSO with CV)
## ============================================================
if (requireNamespace("glmnet", quietly = TRUE) &&
requireNamespace("pROC", quietly = TRUE)) {
set.seed(101)
n <- 180
x1 <- rnorm(n)
x2 <- rnorm(n)
f <- factor(sample(c("A", "B", "C"), n, replace = TRUE))
## Construct a binary outcome with signal in x2 and x1:x2
lin <- -0.3 + 1.0 * x2 + 0.6 * (x1 * x2) - 0.7 * (f == "C")
p <- 1 / (1 + exp(-lin))
yfac <- factor(rbinom(n, 1, p), labels = c("No", "Yes")) # 2-level factor
df <- data.frame(y = yfac, x1 = x1, x2 = x2, f = f)
## Model matrix with main effects + one interaction, no intercept
X <- model.matrix(~ x1 + x2 + f + x1:x2 - 1, data = df)
## Penalty factors must match X's columns (names + length).
penalty_factors <- rep(1, ncol(X))
names(penalty_factors) <- colnames(X)
## (Optional) keep x1 unpenalized:
if ("x1" %in% names(penalty_factors)) penalty_factors["x1"] <- 0
fit_bin <- fit_outcome(
yvar = "y",
df = df,
X = X,
penalty_factors = penalty_factors,
nfolds = 3,
standardize = TRUE,
parallel = FALSE,
return_models = FALSE,
verbose = FALSE
)
## Peek at the results
fit_bin$selected
fit_bin$lambda_min
fit_bin$goodness$full_data$auc
fit_bin$goodness$full_data$accuracy
}
## ============================================================
## Example 2: Continuous outcome (gaussian LASSO with CV)
## ============================================================
if (requireNamespace("glmnet", quietly = TRUE)) {
set.seed(202)
n <- 160
x1 <- rnorm(n)
x2 <- rnorm(n)
f <- factor(sample(c("L", "H"), n, replace = TRUE))
y <- 1.5 * x1 + 0.8 * x2 - 1.0 * (f == "H") + 0.6 * (x1 * x2) + rnorm(n, sd = 0.7)
df <- data.frame(y = y, x1 = x1, x2 = x2, f = f)
## Main effects only, no intercept
X <- model.matrix(~ x1 + x2 + f - 1, data = df)
penalty_factors <- rep(1, ncol(X))
names(penalty_factors) <- colnames(X)
fit_cont <- fit_outcome(
yvar = "y",
df = df,
X = X,
penalty_factors = penalty_factors,
nfolds = 3,
standardize = TRUE,
parallel = FALSE,
return_models = FALSE,
verbose = FALSE
)
## Key metrics
fit_cont$selected
fit_cont$lambda_min
fit_cont$goodness$full_data$mse
fit_cont$goodness$full_data$r_squared
}
Run the code above in your browser using DataLab