I was trying to teach myself the math behind what produces the slope and intercept of a segmented variable in the segmented package using the plant data set. My plan was to run a model through the segmented package, produce the slopes and intercept using the slope and intercept commands and compare to summary output to the summary output of splines2 package.
I notice then that both outputs from both packages summary functions were not the same.
My questions are thus:
- Am I wrong in assuming the segmented function in the
segmentedpackage fits a spline with a degree of 1 similar to using thebSplinefunction with a degree of 1 in thesplines2package? - Is there a way to get the slopes and intercept for each variable (as opposed to the orthogonal spline in the
summaryoutput) from tehsplines2package? Even hard coding the steps would nice to know.
Here is the code I used to try and do this task:
library(segmented)
library(splines2)
data <- data("plant")
#run glm
plant_glm <- glm(y ~ time, data = plant)
#run segmented model
plant_segmented <- segmented(plant_glm, Z = ~time, psi = c(366.5))
#get summary, slope, and intecept
summary(plant_segmented)
slope(plant_segmented)
intercept(plant_segmented)
#Run BSpline model
spline_model <- glm(y ~ bSpline(time, degree = 1, knots = 366.5),data =
plant)
#get bSpline summary
summary(spline_model)
segmentedandsplines2have different purposes. The former automatically find the breakpoints whereas the latter requires you to specify them. That's why you're getting different results. One general way to work out slopes and other spline parameters (but not the best in your case due to its simplicity) is to reverse-engineer the splines, as I explain at https://stats.stackexchange.com/a/101484/919. Anyway, what is the thrust of your question: understanding piecewise linear splines or understanding howsegmentedfinds and reports breakpoints? – whuber Mar 09 '18 at 16:32segmentedstates "The algorithm used by segmented is not grid-search. It is an iterative procedure (Muggeo, 2003)." It also states thatpsiis an argument that should contain "starting values for the breakpoints to be estimated" (emphasis added). If this doesn't make sense to you, then please explore material on our site (or elsewhere) about change-point estimation. – whuber Mar 09 '18 at 16:37segmentedbut now I feel like I want to understand how to re-engineer thesplines2package. I have a data set that I want to do a simple piece-wise linear fit to the 1st degree and extract the slopes and coefficients in order to convert to simple relativities (because I'm starting from a straight GLM). I have actually read your o...919 post but embarrassingly, I do not comprehend enough to take that information and convert to what I need. You mentioned your site...what site is that? – Jordan Mar 09 '18 at 17:01