Murat Hamutcuoglu
Murat Hamutcuoglu

Reputation: 31

Getting line equation from Segmented Package Function in R Language

 X=c(-    0.000000446,0.000006498,0.00001344,0.00002039,0.00002733,0.00003427,0.00004122,0.00004816,0.00005511,0.00006205,0.000069,0.0000836,0.00009821,0.0001128,0.0001274,0.000142,0.0001566,0.0001713,0.0001859,0.0002005,0.0002151,0.0002297,0.0002443,0.0002589,0.0002735,0.0002881,0.0003027,0.0003173,0.000332,0.0003466,0.0003612,0.0003758,0.0003904,0.000405,0.0004196,0.0004342,0.0004488,0.0004634,0.000478,0.0004926,0.0005073,0.0005219,0.0005365,0.0005511,0.0005657,0.0005803,0.0005949,0.0006095,0.0006241,0.0006387,0.0006533,0.000668,0.0006826,0.0006972,0.0007118,0.0007264,0.000741,0.0007556,0.0007664)
 Y=c(0,61820,73630,83830,93720,103600,109000,112700,116100,119500,120900,122100,123300,124300,125300,126600,127800,129000,130100,131200,132300,133300,134200,135200,136100,136900,137800,138600,139400,140100,140900,141500,142200,142800,143500,144000,144600,145100,145600,146100,146600,147000,147400,147800,148200,148500,148800,149100,149300,149600,149800,149800,149900,149800,149700,149300,148500,148000,147600)
 plot(X,Y)
 linear1=lm(Y~X)
 linear2=segmented(linear1,seg.Z=~X, psi = 0.0005,data=d)
 plot(linear2)
 points(X,Y)
 slope(linear2)

What i am trying to do is to get a bilinear representation of the curve given here. First of all i wanna ask if there is any better way than my code? Second and important thing is that i dont know what function or command displays the intersection point of the lines and the equations of each lines. So the question is how to get the line equations out of this segmented command.

Upvotes: 3

Views: 1386

Answers (2)

Fleetboat
Fleetboat

Reputation: 309

This is an old question, but I was poking around myself for information on using segmented, and since I think I understand what you're asking, can offer a quick answer. The 'x' value at which the two curves meet is the breakpoint. Using your example:

linear2

Call: segmented.lm(obj = linear1, seg.Z = ~X, psi = 5e-04, data = d)

Meaningful coefficients of the linear terms:

(Intercept) X U1.X
2.997e+04 2.212e+09 -2.167e+09

Estimated Break-Point(s) psi1.X : 4.215e-05

The linear equation set for your model is:

y = slope1*x + intercept1 when x <= breakpoint

y = slope2*x + intercept2 when x > breakpoint

Upvotes: 2

agstudy
agstudy

Reputation: 121568

is is not clear what do you mean by bilinear representation of the curve. To get lines equations, you can should use intercept and slope like this:

intercept(linear2)
$X
             Est.
intercept1  22730
intercept2 119200

> slope(linear2)
$X
            Est.   St.Err. t value CI(95%).l CI(95%).u
slope1 2.938e+09 288100000   10.20 2.360e+09 3.515e+09
slope2 4.872e+07   3840000   12.69 4.103e+07 5.642e+07

Upvotes: 3

Related Questions