## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>",
                      fig.width = 6, fig.height = 4)

## ----setup--------------------------------------------------------------------
library(panglm)
data(copd)

## ----splines------------------------------------------------------------------
set.seed(2026)
N <- 150; TT <- 5
sim <- data.frame(id = rep(seq_len(N), each = TT), year = rep(seq_len(TT), N))
alpha <- rnorm(N)
sim$bmi <- 27 + 3 * alpha[sim$id] + rnorm(N * TT, sd = 2.5)
sim$logcost <- 6 + alpha[sim$id] + 0.004 * (sim$bmi - 23)^2 +
  rnorm(N * TT, sd = 0.4)
fit_spline <- panglm(logcost ~ splines::ns(bmi, 4), data = sim,
                     index = c("id", "year"), model = "within",
                     vcov = "cluster")
lineartest(fit_spline, "bmi")

## ----splines-plot, fig.alt = "Within effect of BMI on log spending relative to BMI 25 with a pointwise band; the true curve is dashed"----
plot(effectcurve(fit_spline, "bmi", ref = 25), xlab = "Body mass index",
     ylab = "Change in log spending vs BMI 25")
curve(0.004 * (x - 23)^2 - 0.004 * (25 - 23)^2, add = TRUE, lty = 2)

## ----vcov---------------------------------------------------------------------
fit_fe <- panglm(fev1 ~ crp, data = copd, index = c("id", "visit"),
                 model = "within")
sqrt(c(classical = vcov(fit_fe), HC1 = vcov(fit_fe, type = "HC1"),
       cluster = vcov(fit_fe, type = "cluster")))

