# Example 1: gaussian response, two numeric predictors, unpenalized
library(MASS)
data("Rubber", package = "MASS")
y <- Rubber[, 1]
xmat <- Rubber[, -1]
# the sample size = 30. For a small sample, it is better to use pnt = FALSE
ans <- shapeselect(xmat, y, types = c("num", "num"), pnt = FALSE)
# print out the chosen predictors and their shapes
print (ans)
# plot chosen components
plot(ans)
# make a prediction
xtest <- data.frame(hard = mean(Rubber$hard), tens = mean(Rubber$tens))
pfit <- predict(ans, xtest, type = "response")$fit
# Example 2: binomial response, one nominal predictor and six numeric predictors, penalized
# each numeric predictor has 4 shape options
data(Pima.tr, package = "MASS")
df <- Pima.tr |> dplyr::filter(age <= 70) |>
dplyr::mutate(npreg_grp = cut(npreg, breaks = c(-Inf, 0, 1, 5, 9, Inf),
labels = c("0", "1", "2", "3", "4"), ordered_result = FALSE)) |>
dplyr::select(-npreg) |>
dplyr::select(npreg_grp, dplyr::everything())
y <- as.integer(df$type == "Yes")
xmat <- df[, 1:7, drop = FALSE]
# Default nsim = 500
if (FALSE) {
ans <- shapeselect(xmat, y, types = c("nom", rep("num", 6)), family = "binomial",
nsim = 200, pnt = TRUE, set4 = TRUE)
print(ans)
plot(ans)
}
Run the code above in your browser using DataLab