In R package “segmented”, How could I set the slope of one of lines in the model to 0?

为君一笑 提交于 2020-01-23 01:18:11

问题


I am using the R package segmented to calculate parameters for a model, in which the response variable is linearly correlated with the explanatory variable until a breakpoint, then the response variable becomes independent from the explanatory variable. In other words, a segmented linear model with the second part having a slope = 0. What I already did is:

linear1 <- lm(Y ~ X)
linear2  <- segmented (linear1, seg.Z = ~ X, psi = 2)

This gives a model that have a very good first line, but the second line is not horizontal (but not significant). I want to make the second line horizontal. (psi = 2 is the place where I observed a breakpoint.)

Also, when I use "abline" to show the broken line on the plotting, it only show the first part of the model, giving a warning: "only using the first two of 4 regression coefficients". How could I display both parts of the model?

To input my data into R:

X <- c(0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0)
Y <- c(1.31, 1.60, 1.86, 2.16, 2.44, 2.71, 3.00, 3.24, 3.57, 3.81, 3.80, 3.83, 3.78, 3.94, 3.75, 3.89)

回答1:


This is as easy as using the plot method for segmented class objects provided by the package segmented and linked in the help for segmented

Assuming your data is in the data.frame d

linear2  <- segmented (linear1, seg.Z = ~ X, psi = 2, data = d)
plot(linear2)
points(Y~X, data = d)

An easy way to fudge a horizontal line would be to replace the coefficient with value required for that line to be horizontal

fudgedmodel <- linear2
fudgedmodel$coefficients[3] <- - fudgedmodel$coefficients[2]
plot(fudgedmodel)
points(Y~X, data = d)




回答2:


Searching for the same thing and found a neat answer on this post from the R help mailing list:

https://stat.ethz.ch/pipermail/r-help/2007-July/137625.html

Here's an edited version of that answer that cuts straight to the solution:

library(segmented)

# simulate data - linear slope down until some point, at which slope=0
n<-50
x<-1:n/n
y<- 0-pmin(x-.5,0)+rnorm(50)*.03
plot(x,y) #This should be your scatterplot..
abline(0,0,lty=2)


# a parsimonious modelling: constrain right slope=0
# NB. This is probably what you want...
o<-lm(y~1)
xx<- -x
o2<-segmented(o,seg.Z=~xx,psi=list(xx=-.3))
slope(o2)
points(x,fitted(o2),col=2)


# now constrain \hat{\mu}(x)=0 for x>psi (you can do this if you know what the value of y is when x becomes independent)
o<-lm(y~0)
xx<- -x
o3<-segmented(o,seg.Z=~xx,psi=list(xx=-.3))
slope(o3)
points(x,fitted(o3),col=3)

You should get something like this. Red points are the first method, which sounds like the one for you. Green points are the second method, which only applies if you already know the value of y at which x becomes independent:



来源:https://stackoverflow.com/questions/13810607/in-r-package-segmented-how-could-i-set-the-slope-of-one-of-lines-in-the-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!