我想使分段线性函数适合我的数据:
在this answer之后,我可以在第二条直线的斜率= 0的情况下获得下面的拟合,但是如何将第一条直线的截距限制为0?
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
我尝试过:
seg.Z ~ negx - 1
调用中使用segmented
:这将返回一个对我没有意义的模型(转换回原始坐标系时的坡度为-17,即完全错误的符号)m1 <- lm(steam ~ x - 1)
和segmented(m1, seg.Z=~1, psi=1)
,但是它抱怨psi
的名称与模型中的变量(我不知道如何将截距命名为“变量”)