Segmented Return in R

Asked

Viewed 507 times

2

I was studying a little bit about regressão segmentada and some examples executed in R and I’m having doubts about the workings of segmented. I need to always define where the breaking points should exist?

The R does not provide any function that estimates the value of break points according to the distribution of my data?

  • I never used the package, but maybe you can make a script that tests different points and compares the results, if not too many points maybe will give a good result.

  • It is.. I had seen the use of piecewise.linear of the Sizer package and, from what I understand, it asks you to define the breaking point.. other solutions that extend the same concept allow more break points to be defined. but I should input the values. My doubt is there is the possibility of the function itself to define where these breakpoints would be without needing to indicate them.

1 answer

1


Consider the example of help(segmented) To adjust the segmented regression model, you first need to adjust the linear regression model.

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)

The function segmented always estimate breakpoints. If you provide initial values using the parameter psi, the number of breakpoints he estimates is the same as the size of the vector you passed.

> m.s <- segmented(m, seg.Z = ~x, psi = c(25, 80))
> m.s
Call: segmented.lm(obj = m, seg.Z = ~x, psi = c(25, 80))

Meaningful coefficients of the linear terms:
(Intercept)            x            z         U1.x         U2.x  
    1.12868     -0.06976      8.72782      1.55907     -1.54130  

Estimated Break-Point(s):
psi1.x  psi2.x  
 34.37   70.56  

See above for estimated values p/ breakpoints.

You may also not provide any initial value (psi = NA), and in this case provide only the number of breakpoints you want to use through the parameter K in function seg.control.

> m.s <- segmented(m, seg.Z = ~x, psi = NA, control = seg.control(K = 2))
> m.s
Call: segmented.lm(obj = m, seg.Z = ~x, psi = NA, control = seg.control(K = 2))

Meaningful coefficients of the linear terms:
(Intercept)            x            z         U1.x         U2.x  
    1.11308     -0.06832      8.72422      1.55716     -1.54335  

Estimated Break-Point(s):
psi1.x  psi2.x  
 34.39   70.61 

Note that the standard value of K is 10 and so, if your database does not have as many points of change, you will have pet problems if it does not provide K.

  • That’s right, Daniel. It worked with Segmented. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.