if (FALSE) {
library(splines)
library(dplyr)
library(magrittr)
df <- 10
trust_experts <- trust_experts %>%
mutate(across(
where(is.factor),
~ set_attr(.x, "contrasts", contr.sum(nlevels(.x), FALSE, TRUE))
))
x <- Matrix::sparse.model.matrix(
~ 0 + region + age + gender + raceethnicity + period +
bs(cli, df = df) + bs(hh_cmnty_cli, df = df),
data = trust_experts, drop.unused.levels = TRUE
)
gr <- sapply(trust_experts, function(x) ifelse(is.factor(x), nlevels(x), NA))
gr <- rep(seq(ncol(trust_experts) - 1), times = c(gr[!is.na(gr)], df, df))
fit <- cv.sparsegl(x, trust_experts$trust_experts, gr)
}
Run the code above in your browser using DataLab