This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
library(segmented)
homework<-read.table("clipboard",header = TRUE)
通过画出所有变量的散点关系图,找出需要进行分段回归的关系
plot(homework)
通过上图可以看出,x3和y1之间存在分段关系
plot(homework$x3~homework$y1)
obj<-glm(x3~y1,data = homework)
obj.seg<-segmented(obj,seg.Z = ~y1,psi = 380)
obj
##
## Call: glm(formula = x3 ~ y1, data = homework)
##
## Coefficients:
## (Intercept) y1
## 343.8339 -0.2574
##
## Degrees of Freedom: 178 Total (i.e. Null); 177 Residual
## Null Deviance: 1689000
## Residual Deviance: 1111000 AIC: 2077
obj.seg
## Call: segmented.glm(obj = obj, seg.Z = ~y1, psi = 380)
##
## Meaningful coefficients of the linear terms:
## (Intercept) y1 U1.y1
## 530.0537 -0.8793 0.8136
##
## Estimated Break-Point(s):
## psi1.y1
## 388.4
##
## Degrees of Freedom: 178 Total (i.e. Null); 175 Residual
## Null Deviance: 1689000
## Residual Deviance: 808500 AIC: 2024
par(mfrow=c(2,1))
plot(obj.seg,conf.level = 0.95,shade = TRUE)
## Warning in class(x) == c("segmented", "Arima"): 长的对象长度不是短的对象长
## 度的整倍数
## Warning in class(x) == c("segmented", "Arima"): 长的对象长度不是短的对象长
## 度的整倍数
plot(homework$x3~homework$y1)
plot(obj.seg,add=TRUE,link=FALSE,lwd=2,col=2:3, lty=c(1,3))
## Warning in class(x) == c("segmented", "Arima"): 长的对象长度不是短的对象长
## 度的整倍数
## Warning in class(x) == c("segmented", "Arima"): 长的对象长度不是短的对象长
## 度的整倍数
lines(obj.seg,col=2,pch=19,bottom=FALSE,lwd=2)