凹进时在R中使用NLS的三段式分段语法

问题描述 投票:1回答:2

我的目标是使用三分(即两个断点)回归模型来拟合,并使用传播的predictNLS函数进行预测,确保将结点定义为参数,但我的模型公式似乎不正确。

我已经使用segmented包来估计断点位置(在NLS中用作起始值),但想将模型保持在NLS格式,特别是nlsLM {minipack.lm},因为我正在拟合其他类型的曲线使用NLS对我的数据进行处理,想要允许NLS优化结点值,有时使用可变权重,并且需要能够根据propagate轻松计算出蒙特卡洛置信区间。尽管我很想让公式具有正确的语法,但在断点附近并没有得到预期的/必需的行为。段应该在断点处直接相接(没有任何跳跃),但是至少在此数据上,我在断点处得到了一个奇怪的局部最小值(请参见下面的图)。以下是我的数据和一般流程的示例。我认为我的问题出在NLS公式中。

library(minpack.lm) library(segmented) y <- c(-3.99448113, -3.82447011, -3.65447803, -3.48447030, -3.31447855, -3.14448753, -2.97447972, -2.80448401, -2.63448380, -2.46448069, -2.29448796, -2.12448912, -1.95448783, -1.78448797, -1.61448563, -1.44448719, -1.27448469, -1.10448651, -0.93448525, -0.76448637, -0.59448626, -0.42448586, -0.25448588, -0.08448548, 0.08551417, 0.25551393, 0.42551411, 0.59551395, 0.76551389, 0.93551398) x <- c(61586.1711, 60330.5550, 54219.9925, 50927.5381, 48402.8700, 45661.9175, 37375.6023, 33249.1248, 30808.6131, 28378.6508, 22533.3782, 13901.0882, 11716.5669, 11004.7305, 10340.3429, 9587.7994, 8736.3200, 8372.1482, 8074.3709, 7788.1847, 7499.6721, 7204.3168, 6870.8192, 6413.0828, 5523.8097, 3961.6114, 3460.0913, 2907.8614, 2016.1158, 452.8841) df<- data.frame(x,y) #Use Segmented to get estimates for parameters with 2 breakpoints my.seg2 <- segmented(lm(y ~ x, data = df), seg.Z = ~ x, npsi = 2) #extract knot, intercept, and coefficient values to use as NLS start points my.knot1 <- my.seg2$psi[1,2] my.knot2 <- my.seg2$psi[2,2] my.m_2 <- slope(my.seg2)$x[1,1] my.b1 <- my.seg2$coefficients[[1]] my.b2 <- my.seg2$coefficients[[2]] my.b3 <- my.seg2$coefficients[[3]] #Fit a NLS model to ~replicate segmented model. Presumably my model formula is where the problem lies my.model <- nlsLM(y~m*x+b+(b2*(ifelse(x>=knot1&x<=knot2,1,0)*(x-knot1))+(b3*ifelse(x>knot2,1,0)*(x-knot2-knot1))),data=df, start = c(m = my.m_2, b = my.b1, b2 = my.b2, b3 = my.b3, knot1 = my.knot1, knot2 = my.knot2))

应该

外观plot(my.seg2)
enter image description here

它的外观[[外观

plot(x, y) lines(x=x, y=predict(my.model), col='black', lty = 1, lwd = 1)

enter image description here
我非常确定我的想法是“正确的”,但是当用直线绘制95%的置信区间并且预测分辨率(例如x点的密度)增加时,情况似乎

严重错误

enter image description here

谢谢大家的帮助。

我的目标是拟合三段式(即,两个断点)回归模型,以使用传播的predictNLS函数进行预测,确保将结定义为参数,但我的模型公式为……

r regression linear-regression nls piecewise
2个回答
1
投票

1
投票
© www.soinside.com 2019 - 2024. All rights reserved.