Skip to contents

Extract predictions and confidence intervals from fitted models

Usage

extract_spline_data(
  fit,
  data,
  xvar,
  refx,
  model_info,
  term_index = 1,
  log_scale = FALSE,
  ci_level = 0.95
)

Arguments

fit

Fitted model object

data

Data frame

xvar

Variable name

refx

Reference value

model_info

Model information list

term_index

Which smooth term to use (for multiple s() terms)

log_scale

Whether to use log scale

ci_level

Confidence level

Value

Data frame with predictions

Examples

# Create sample data
set.seed(123)
n <- 100
x <- rnorm(n, mean = 50, sd = 10)
y <- rbinom(n, 1, plogis(-0.05*(x - 50)))
dat <- data.frame(x = x, y = y)

# Fit GLM with splines
library(splines)
fit <- glm(y ~ ns(x, df = 4), family = binomial(), data = dat)

# Extract spline data
model_info <- list(type = "glm", family = "binomial", ylabel = "Odds Ratio")
df <- extract_spline_data(fit, dat, "x", refx = 50, model_info,
                          log_scale = FALSE, ci_level = 0.95)
head(df)
#>          x        y        lcl       ucl     ylabel
#> 1 26.90831 4.820616 -0.9214519 10.562684 Odds Ratio
#> 2 27.13427 4.738736 -0.8847702 10.362242 Odds Ratio
#> 3 27.36022 4.656878 -0.8483307 10.162086 Odds Ratio
#> 4 27.58618 4.575062 -0.8121642  9.962289 Odds Ratio
#> 5 27.81213 4.493311 -0.7763023  9.762925 Odds Ratio
#> 6 28.03809 4.411646 -0.7407771  9.564069 Odds Ratio