绘制拟合的分段线性模型将显示比预期更多的断裂点 [英] plotting a fitted segmented linear model shows more break points than what is estimated

查看:39
本文介绍了绘制拟合的分段线性模型将显示比预期更多的断裂点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天在用分段回归帮助一个朋友.我们正在尝试使用断点拟合分段回归,以查看它是否比标准线性模型更适合数据.

我偶然发现了一个我无法理解的问题.当使用提供的数据对具有单个断点的分段回归进行拟合时,确实确实适合单个断点.

但是,当您从模型进行预测时,它会给出看起来像2个断点的样子.使用 plot.segmented()绘制模型时,不会发生此问题.

任何人都知道发生了什么事以及如何获得正确的预测(以及标准错误等)?或者我在代码中做错了什么?

 #个加载程序包库(分段)# 制作数据d<-data.frame(x = c(0,3,13,18,19,19,26,26,33,40,49,51,53,67,70,88),y = c(0,3.56211608128595,10.5214485148819,3.66063708049802,6.11000808621074,5.51520423804034、7.73043895812661、7.90961392857039、6.55966527933846,10.4413913666936、8.71673928545967、9.93374157928462、1.214860139929,3.32428882257746、2.65223361387063、3.25440939462105))#适合普通线性回归和分段回归lm1<-lm(y〜x,d)seg_lm<-segmented(lm1,〜x)斜率(seg_lm)#>$ x#>美东时间.圣艾尔t值CI(95%).l CI(95%).u#>斜率1 0.17185 0.094053 1.8271 -0.033079 0.37677000#>斜率2 -0.15753 0.071933 -2.1899 -0.314260 -0.00079718# 作出预测preds<-data.frame(x = d $ x,preds =预测(seg_lm))#情节分段拟合情节(seg_lm,res = TRUE)#剧情预测行数(preds $ preds〜preds $ x,col ='red') 

I was helping a friend with segmented regressions today. We were trying to fit a piecewise regression with a breakpoints to see if it fits data better than a standard linear model.

I stumbled across a problem I cannot understand. When fitting a piecewise regression with a single breakpoint with the data provided, it does indeed fit a single breakpoint.

However, when you predict from the model it gives what looks like 2 breakpoints. When plotting the model using plot.segmented() this problem does not happen.

Anyone have any idea what is going on and how I can get the proper predictions (and standard errors etc)? Or what I am doing wrong in the code in general?

# load packages
library(segmented)

# make data
d <- data.frame(x = c(0, 3, 13, 18, 19, 19, 26, 26, 33, 40, 49, 51, 53, 67, 70, 88
),
                y = c(0, 3.56211608128595, 10.5214485148819, 3.66063708049802, 6.11000808621074, 
                      5.51520423804034, 7.73043895812661, 7.90691392857039, 6.59626527933846, 
                      10.4413913666936, 8.71673928545967, 9.93374157928462, 1.214860139929, 
                      3.32428882257746, 2.65223361387063, 3.25440939462105))

# fit normal linear regression and segmented regression
lm1 <- lm(y ~ x, d)
seg_lm <- segmented(lm1, ~ x)

slope(seg_lm)
#> $x
#>            Est.  St.Err. t value CI(95%).l   CI(95%).u
#> slope1  0.17185 0.094053  1.8271 -0.033079  0.37677000
#> slope2 -0.15753 0.071933 -2.1899 -0.314260 -0.00079718

# make predictions
preds <- data.frame(x = d$x, preds = predict(seg_lm))

# plot segmented fit
plot(seg_lm, res = TRUE)

# plot predictions
lines(preds$preds ~ preds$x, col = 'red')

Created on 2018-07-27 by the reprex package (v0.2.0).

解决方案

It is a pure plotting issue.

#Call: segmented.lm(obj = lm1, seg.Z = ~x)
#
#Meaningful coefficients of the linear terms:
#(Intercept)            x         U1.x  
#     2.7489       0.1712      -0.3291  
#
#Estimated Break-Point(s):
#psi1.x  
# 37.46  

The break point is estimated to be at x = 37.46, which is not any of the sampling locations:

d$x
# [1]  0  3 13 18 19 19 26 26 33 40 49 51 53 67 70 88

If you make your plot with fitted values at those sampling locations,

preds <- data.frame(x = d$x, preds = predict(seg_lm))
lines(preds$preds ~ preds$x, col = 'red')

You won't visually see those fitted two segments join up at the break points, as lines just line up fitted values one by one. plot.segmented instead would watch for the break points and make the correct plot.


Try the following:

## the fitted model is piecewise linear between boundary points and break points
xp <- c(min(d$x), seg_lm$psi[, "Est."], max(d$x))
yp <- predict(seg_lm, newdata = data.frame(x = xp))

plot(d, col = 8, pch = 19)  ## observations
lines(xp, yp)  ## fitted model
points(d$x, seg_lm$fitted, pch = 19)  ## fitted values
abline(v = d$x, col = 8, lty = 2)  ## highlight sampling locations

这篇关于绘制拟合的分段线性模型将显示比预期更多的断裂点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆