[英]Piecewise linear regression in R (segmented.lm)
I appreciate any help to make segmented.lm (or any other function) find the obvious breakpoints in this example:我感谢任何帮助使segmented.lm (或任何其他函数)在这个例子中找到明显的断点:
data = list(x=c(50,60,70,80,90) , y= c(703.786,705.857,708.153,711.056,709.257))
plot(data, type='b')
require(segmented)
model.lm = segmented(lm(y~x,data = data),seg.Z = ~x, psi = NA)
It returns with the following error:它返回以下错误:
Error in solve.default(crossprod(x1), crossprod(x1, y1)) : system is computationally singular: reciprocal condition number = 1.51417e-20 solve.default(crossprod(x1), crossprod(x1, y1)) 中的错误:系统在计算上是奇异的:倒数条件数 = 1.51417e-20
If I change K:如果我改变 K:
model.lm = segmented(lm(y~x,data = data),seg.Z = ~x, psi = NA, control = seg.control(K=1))
I get another error:我收到另一个错误:
Error in segmented.lm(lm(y ~ x, data = data), seg.Z = ~x, psi = NA, control = seg.control(K = 1)) : only 1 datum in an interval: breakpoint(s) at the boundary or too close each other segmented.lm(lm(y ~ x, data = data), seg.Z = ~x, psi = NA, control = seg.control(K = 1)) 中的错误:间隔中只有 1 个数据:断点(s) ) 在边界处或彼此太近
A nice objective method to determine the break point is described in Crawley (2007: 427). Crawley (2007: 427) 描述了一种很好的确定断点的客观方法。
First, define a vector breaks
for a range of potential break points:首先,为一系列潜在的断点定义一个向量breaks
:
breaks <- data$x[data$x >= 70 & data$x <= 90]
Then run a for
loop for piecewise regressions for all potential break points and yank out the minimal residual standard error ( mse
) for each model from the summary
output:然后对所有潜在断点运行for
循环进行分段回归,并从summary
输出中提取每个模型的最小残差标准误差 ( mse
):
mse <- numeric(length(breaks))
for(i in 1:length(breaks)){
piecewise <- lm(data$y ~ data$y*(data$x < breaks[i]) + data$y*(data$x >= breaks[i]))
mse[i] <- summary(piecewise)[6]
}
mse <- as.numeric(mse)
Finally, identify the break point with the least mse
:最后,确定mse
的断点:
breaks[which(mse==min(mse))]
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.