I am using the R package segmented to detrend my data and to obtain the residuals. I want to fit 2 linear lines with one breakpoint, for each of the groups of cyl, but I don't want the second line to have a negative slope (it should be positive or 0). How can I do that?
This is my code to create 2 segmented lines for each cyl:
library(segmented)
mtcars%>%
nest_by(cyl) %>%
mutate(mod = list(segmented(lm(hp~disp, data = data)))) %>%
summarize(augment(mod))