insight icon indicating copy to clipboard operation
insight copied to clipboard

Requesting insight support for segmented::

Open topherwhatever opened this issue 4 years ago • 4 comments
trafficstars

Hi there,

Quick request: I'm not sure how popular the package is, but as a user of the segmented package, support by insight would be helpful.

Cheers, and keep up the hard work!

topherwhatever avatar Oct 26 '21 15:10 topherwhatever

Could you give us a reproducible example using the segmented package?

bwiernik avatar Oct 26 '21 21:10 bwiernik

I already typed this up and submitted it once, so perhaps the delay is by design. Here's the reproducible code for the segmented package, taken explicitly from his documentation with some minor commentary by me:

It works well with the (g)lm and is supposed to work well with time series models, though I don't have any experience. I find the parsimony to be easier to convey than most non-linear methods, parametric or not.

set.seed(12)
xx<-1:100
zz<-runif(100)
yy<-2+1.5*pmax(xx-35,0)-1.5*pmax(xx-70,0)+15*pmax(zz-.5,0)+rnorm(100,0,2)
dati<-data.frame(x=xx,y=yy,z=zz)
out.lm<-lm(y~x,data=dati) ## fit a linear model to potentially nonlinear functions.


## I start with the knowledge of a single inflection/breakpoint (denoted psi) 
library(segmented)

o<-segmented(out.lm) #1 breakpoint for x, not specified because it's the only term in the model. 
plot.segmented(o)
summary(o)

o<-segmented(out.lm,seg.Z=~z) ## 1 breakpoint, but specifying a covariate not in the original lm() 
summary(o)
plot.segmented(o)

o<-segmented(out.lm,seg.Z=~z+x) ## finding breakpoints in two predictors 
summary(o)

plot.segmented(o, term = "x") 
plot.segmented(o, term = "z")

##two breakpoints, with user-provided starting values
o<-segmented(out.lm,seg.Z=~x,psi=c(30,60), control=seg.control(display=FALSE)) 
summary(o)
plot.segmented(o, term = "x")

## two breakpoints, estimated automatically
o<-segmented(out.lm,seg.Z=~x, npsi=2, control=seg.control(display=FALSE)) summary(o)
plot.segmented(o, term = "x")
slope(o)
summary(o)
## if the number of breakpoints is unknown (or zero), there are helpful inferential tests to estimate their presence or the number of them

z<-runif(100)
x<-rnorm(100,2)
y<-2+10*pmax(z-.5,0)+rnorm(100,0,3)
o<-lm(y~z+x)
davies.test(o,~z)
davies.test(o,~x)
o<-glm(y~z+x)
davies.test(o,~z)  #p-values in the classical Davies test suggest no breakpoints,
selgmented(o) ## more modern score-based methods to estimate the number of breakpoints suggest 0 as well. 


## Here's an example with 2 breakpoints:
xx<-1:100
zz<-runif(100)
yy<-2+1.5*pmax(xx-35,0)-1.5*pmax(xx-70,0)+15*pmax(zz-.5,0)+rnorm(100,0,2)
dati<-data.frame(x=xx,y=yy,z=zz)
out.lm<-lm(y~x,data=dati)

os<-selgmented(out.lm) ## selects number of breakpoints via the Score test
os <-selgmented(out.lm, Kmax=3, type="bic") #BIC-based selection


z<-runif(100)
y<-rpois(100,exp(2+1.8*pmax(z-.6,0)))
o<-glm(y~z,family=poisson)
o.seg<-segmented(o,seg.Z=~z,psi=.5)
plot(z,y)
## plotting routines are pretty rudimentary, but subsequent analyses can be performed on fitted values and residuals.
points(z,
                broken.line(o.seg,link=FALSE)$fit,col=2)## broken.line pulls those values
fit_val <- broken.line(o.seg,link=FALSE)
View(fit_val)
str(fit_val)

topherwhatever avatar Oct 27 '21 20:10 topherwhatever

Could you give us a reproducible example using the segmented package?

was the below what you were looking for?

topherwhatever avatar Nov 11 '21 00:11 topherwhatever

Yes

bwiernik avatar Nov 11 '21 00:11 bwiernik