1

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:

  1. Am I wrong in assuming the segmented function in the segmented package fits a spline with a degree of 1 similar to using the bSpline function with a degree of 1 in the splines2 package?
  2. Is there a way to get the slopes and intercept for each variable (as opposed to the orthogonal spline in the summary output) from teh splines2 package? 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)
kjetil b halvorsen
  • 63,378
  • 26
  • 142
  • 467
Jordan
  • 215
  • 1
  • 8
  • 1
    `segmented` and `splines2` have 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 how `segmented` finds and reports breakpoints? – whuber Mar 09 '18 at 16:32
  • Thanks for the reply @whuber. I'm not sure I totally understand. How is segmented automatically finding the breakpoints if I'm specifying the psi? – Jordan Mar 09 '18 at 16:35
  • The [documentation](https://cran.r-project.org/web/packages/segmented/segmented.pdf) for `segmented` states "The algorithm used by segmented is not grid-search. It is an iterative procedure (Muggeo, 2003)." It also states that `psi` is 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:37
  • 1
    Thank you @whuber and I did not see your return question. The thrust was understanding `segmented` but now I feel like I want to understand how to re-engineer the `splines2` package. 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
  • The site is this one--CV! – whuber Mar 09 '18 at 17:21
  • Oh! You mean use cross validated. Got it. Didn't mean to upset you. Thanks for the help. – Jordan Mar 09 '18 at 17:23

0 Answers0