I want to fit a piecewise linear function to my data:
- unknown breakpoint
- first line has intercept 0
- second line has slope 0 (unknown what the 'cap' value is though)
Following this answer I can obtain the below fit where the second line has slope=0, but how do I also constrain the intercept of the first line to be 0?
- y = 4.1593 * x + -1.3469 for x < 9.215
- y = 36.98 for x > 9.25
set.seed(1)
x <- rnorm(100, mean=9, sd=1.5)
y <- pmin(x * 4, 37) + rnorm(100)
plot(x, y)
# refine to put in a breakpoint allowing the other part to have non-zero slope
# with this setup, segmented will constrain the slope of the first segment to be 0
# since I want the second segment to be 0 I mirror around the X axis
negx <- -x
mpw <- segmented(m0, seg.Z=~negx)
# Estimated Break-Point(s):
# psi1.negx
# -9.215
slope(mpw)
# Est. St.Err. t value CI(95%).l CI(95%).u
# slope1 0.0000 NA NA NA NA
# slope2 -4.1593 0.16289 -25.534 -4.4826 -3.836
intercept(mpw)
# intercept1 36.9800
# intercept2 -1.3469
I have tried:
- using
seg.Z ~ negx - 1
in thesegmented
call: this returns a model that makes no sense to me (slope of -17 when transformed back to the original coordinate system, ie wrong sign entirely) - fitting the no-intercept part first then attempting to constrain the second part to be constant:
m1 <- lm(steam ~ x - 1)
andsegmented(m1, seg.Z=~1, psi=1)
, but it complains that the names ofpsi
don't match the variables in the model (I do not know how to name the intercept "variable")