piecewise regression ~ segmented package

Hi,

I have a distribution over time in months but I wanted to divide and represent it with 3 slopes. I used the segmented package for piecewise regression but the graph obtained is not exactly what I was aiming for. The drop after slope 2 I wanted to see is somehow in linear continuity with slope 3. I need help adjusting my graph in picture 1 to look more like picture 2, as the whole purpose is to estimate the drop between slope 2 and 3.

Code used:
library(segmented)
#making a linear model (df=name of dataframe)
#x axis = timemonth, avg_in = y axis
df_lin <- lm(avg_in~timemonth, data=df)
summary(df_lin)

#making a segmented model (psi breakpoint) #interested breakpoints = 25, 115, 132
df_seg <- segmented(df_lin, seg.Z = ~ timemonth, psi = 25, 115, 132)
summary(df_seg)

plot(df_seg)

get the breakpoints

df_seg$psi
Result
Initial Est. St.Err
psi1.timemonth 25 23.72201 2.306182

Question: Why is it only showing results for the 1 breakpoint?

get the slopes

slope(df_seg)

Results Est. St.Err. t value
slope1 2.9987e-05 3.9974e-05 0.75015
slope2 -2.2570e-04 3.7542e-06 -60.11900
slope3 -7.9043e-05 6.4128e-05 -1.23260
CI(95%).l CI(95%).u
slope1 -4.9054e-05 1.0903e-04
slope2 -2.3313e-04 -2.1828e-04
slope3 -2.0584e-04 4.7757e-05

Question: However when I check for slopes I get results for all 3, then why only one breakpoint?

Any help will be appreciated to get my graph right? and if someone can also help with other secondary questions I shall be forever grateful.

what y value sohuld the model predict at 115 on the x ?
you seem to want an infinity of values.
Maybe you could solve by splitting the graph into 4 segments (i.e. the 4 lines you roughly want), but put a two break close to 115 like 115 and 115.1

Hi, i only have values from 1-143 on my x-axis so putting 115.1 doesn't work. Maybe looking at the normal trend of distribution and the segmented linear regression together will help, picture attached. If I use 25, 115, 116, 132 as my psi, I get the following error:
"Error: $ operator is invalid for atomic vectors"

I won't be able to attempt to reproduce your error without your data.
consider sharing it via suggested methods FAQ: ( reprex )

although best guess is that only the first breakpoint suggestion you make is being considered (for that purpose) and the other estimates are being interpreted as different parameters to segmented. you should pass the set of estimates within the c() construct like

df_seg <- segmented(df_lin, seg.Z = ~ timemonth, psi =c( 25, 115, 132))
1 Like

That worked, thanks. My estimated are different from what I wanted them to be (picture attached). To overcome this when I use fixed.psi, an additional slope appears with breakpoint at 48 (second picture), giving 5 slopes. Also, I have changed my breakpoints (25, 114, 116) to get the desired drop at 115. Do you have any idea how I can overcome this?

can you share your data ?
also, its unclear to me what you mean by that your estimated are different from what you want them to be ... how did you form the original opinion of what you wanted them to be ? what was it you wanted them to be ?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.