I am working on a regression model and hoping to incorporate spline transformation for my continuous predictor (pre-score). However, I am struggling with incorporating the spline transformation into the logistic regression (using disease as an outcome). Any advice on how I should fix my code below is greatly appreciated.
Here is how I attempted my spline transformed logistic regression:
knots <- quantile(data_B$Pre.score, p = c(0.25, 0.5, 0.75))
# Build the model
library(splines)
model <- glm (disease ~ bs(Pre.score, knots = knots), data = data_B, family=binomial)
# Make predictions
pred.val.2 <- predict(model, type ="response")
# Model performance
library(caret)
data.frame(
RMSE = RMSE(pred.val.2, data_B$disease),
R2 = R2(pred.val.2, data_B$disease)
)
Here is my data:
structure(list(Pre.score = c(15.87301587, 5.310939628,
5.707491082, 3.089700997, 41.27569847, 4.200567644, 14.30503889,
6.699928724, 4.148471616, 3.70212766, 19.41605839, 11.99368753,
5.991232343, 4.42804428, 20.77562327, 3.432367595, 1.1574886,
4.186655037, 29.27974948, 0.874453467, 13.66459627, 0.564652739,
13.06160259, 19.66829268, 0, 8.623969562, 13.18289786, 3.505734689,
4.769475358, 0.208768267, 1.23683005, 11.11934766, 0.567000567,
4.077429984, 1.179835538, 2.582781457, 18.62888102, 0.540151242,
9.014084507, 2.714285714, 15.17327505, 17.41935484, 11.01306036,
13.53987378, 7.38645816, 18.69688385, 14.6287403),
disease = structure(c(1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L,
1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("none", "disease"
), class = "factor")), row.names = c(NA, 47L), class = "data.frame")