Sometimes polynomials do not model nonlinearities well enough
E.g., Too many turns in the line, very quick changes of direction, etc.
Splines allow you to specify a structural “break” (AKA a “knot”) in the regression line
Slope can be completely different before and after the knot (e.g., before and after age 60)
Estimate separate coefficient for age < 60 and age ≥ 60
Parametric technique; you get a coefficient (and associated SE/CI/p-value) for the slope between each knot
Works with almost any regression model
I give only a quick introduction here. For details on splines, Marsh and Cormier’s 2002 Sage QASS book is a good and accessible introduction to splines
(cda_gss.dta | GSS 1972-2021 CDA - Categorical Data Analysis | date created 202
> 3)
lpoly samesexop year, /// xlab(1974(4)2018) ylab(1(1)4) noscatter cilegend(off) ///ytitle("Support for same-gender relationships") ///subtitle("Local polynomial smoothing") ///title("[2.7.a] Change in supportive attitudes toward same-gender relationships from 1972 to 2020")
library(haven) # import Stata data and labelslibrary(dplyr) # data manipulationlibrary(ggplot2) # graphslibrary(modelsummary) # regression tableslibrary(marginaleffects) # predictions and marginal effectslibrary(lspline) # linear splineslibrary(cleanplots) # graph schemecleanplots_defaults()gss <-read_dta("https://tdmize.github.io/data/data/cda_gss.dta")# Keep only the variables used and drop any missing data on themgss <- gss[c("samesexop", "year")]gss <- gss[complete.cases(gss), ]gss <- gss |>mutate(samesexop =as.numeric(samesexop))ggplot(gss, aes(x = year, y = samesexop)) +geom_smooth() +scale_x_continuous(breaks =seq(1974, 2018, 4)) +scale_y_continuous(breaks =1:4, limits =c(1, 4)) +labs(title ="[2.7.a] Change in supportive attitudes toward same-gender relationships",subtitle ="Local polynomial smoothing",x ="Year of survey", y ="Support for same-gender relationships")
*Where would you place the “knots” to model this with splines?
Interpreting linear splines
Linear splines specify linear relationships between each “knot”
I chose knots at 1990, 1996, and 2006 based on graph above
marginsplot, recastci(rarea) ciopts(col(gs13)) plotopts(msymbol(none)) /// xlab(1974(4)2018) ylab(1(1)4) ///ytitle("Support for same-gender relationships") xtitle("Year of survey") ///title("[2.7.c] Support for same-gender relationships - GSS 1973 - 2018", size(medlarge)) ///subtitle("Regression splines with knots at 1990, 1996, and 2006")
Variables that uniquely identify margins: year
plot_predictions(splinemod, by ="year") +scale_x_continuous(breaks =seq(1974, 2018, 4)) +scale_y_continuous(breaks =1:4, limits =c(1, 4)) +labs(title ="[2.7.c] Support for same-gender relationships - GSS 1973 - 2018",subtitle ="Regression splines with knots at 1990, 1996, and 2006",x ="Year of survey", y ="Support for same-gender relationships")