knitr::opts_chunk$set( cache=FALSE, collapse=TRUE )
Here are some examples of using lspline(), qlspline(), and elspline().
We will use the following artificial data with knots at x=5 and x=10
set.seed(666) n <- 200 d <- data.frame( x = scales::rescale(rchisq(n, 6), c(0, 20)) ) d$interval <- findInterval(d$x, c(5, 10), rightmost.closed = TRUE) + 1 d$slope <- c(2, -3, 0)[d$interval] d$intercept <- c(0, 25, -5)[d$interval] d$y <- with(d, intercept + slope * x + rnorm(n, 0, 1))
Plotting y against x:
library(ggplot2) fig <- ggplot(d, aes(x=x, y=y)) + geom_point(aes(color=as.character(slope))) + scale_color_discrete(name="Slope") + theme_bw() fig
The slopes of the consecutive segments are 2, -3, and 0.
We can parametrize the spline with slopes of individual segments (default marginal=FALSE):
library(lspline) m1 <- lm(y ~ lspline(x, c(5, 10)), data=d) knitr::kable(broom::tidy(m1))
Or parametrize with coeficients measuring change in slope (with marginal=TRUE):
m2 <- lm(y ~ lspline(x, c(5,10), marginal=TRUE), data=d) knitr::kable(broom::tidy(m2)) k <- coef(m2) nam <- names(k)
The coefficients are
r nam[2] - the slope of the first segmentr nam[3] - the change in slope at knot $x=5$; it is changing from 2 to -3, so by -5r nam[4] - tha change in slope at knot $x=10$; it is changing from -3 to 0, so by 3The two parametrisations (obviously) give identical predicted values:
all.equal( fitted(m1), fitted(m2) )
graphically
fig + geom_smooth(method="lm", formula=formula(m1), se=FALSE, color = "black") + geom_vline(xintercept = c(5, 10), linetype=2)
n equal-length intervalsFunction elspline() sets the knots at points dividing the range of x into n equal length intervals.
m3 <- lm(y ~ elspline(x, 3), data=d) knitr::kable(broom::tidy(m3))
Graphically
fig + geom_smooth(method="lm", formula=formula(m3), se=FALSE, n=200, color = "black")
quantiles of xFunction qlspline() sets the knots at points dividing the range of x into q equal-frequency intervals.
m4 <- lm(y ~ qlspline(x, 4), data=d) knitr::kable(broom::tidy(m4))
Graphically
fig + geom_smooth(method="lm", formula=formula(m4), se=FALSE, n=200, color = "black")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.