【问题标题】:Equal and opposite slopes in segmented package分段包装中的等斜率和相反斜率
【发布时间】:2016-02-13 18:16:11
【问题描述】:

您好,我正在尝试使用 R 中的分段包来拟合分段线性回归模型来估计我的数据中的断点。我已使用以下代码来获取此图表。

library(segmented)
set.seed(5)
x <- c(1:10, 13:22)
y <- numeric(20)
## Create first segment
y[1:10] <- 20:11 + rnorm(10, 0, 1.5)
## Create second segment
y[11:20] <- seq(11, 15, len=10) + rnorm(10, 0, 1.5)

## fitting a linear model
lin.mod <- lm(y~x)
segmented.mod <- segmented(lin.mod, seg.Z = ~x, psi=15)
summary(segmented.mod)
plot(x,y, pch=".",cex=4,xlab="x",ylab="y")
plot(segmented.mod, add=T, lwd = 3,col = "red") 

我的理论计算表明,两条线关于断点的斜率应该在大小上相等但符号相反。我是 lm 和 glms 的初学者。我希望是否有一种方法可以估计斜率受关系约束的断点,slope1=-slope2

enter image description here

【问题讨论】:

    标签: r package breakpoints


    【解决方案1】:

    分段包不支持此功能。

    可以使用nls2 和"plinear-brute" 算法。在输出中.lin1 和.lin2 分别是常数项和斜率。这会尝试将x 范围内的每个值作为可能的bp 对每个值进行线性回归。

    library(nls2)
    
    st <- data.frame(bp = seq(min(x), max(x)))
    nls2(y ~ cbind(1, abs(x - bp)), start = st, alg = "plinear-brute")
    

    给予:

    Nonlinear regression model
      model: y ~ cbind(1, abs(x - bp))
       data: parent.frame()
           bp     .lin1     .lin2 
    14.000000  9.500457  0.709624 
     residual sum-of-squares: 45.84213
    
    Number of iterations to convergence: 22 
    Achieved convergence tolerance: NA
    

    这是另一个可以澄清这一点的示例,因为它从与 fit 相同的模型生成数据:

    library(nls2)
    
    set.seed(123)
    n <- 100
    bp <- 25
    x <- 1:n
    y <- rnorm(n, 10 + 2 * abs(x - bp))
    
    st <- data.frame(bp = seq(min(x), max(x)))
    fm <- nls2(y ~ cbind(1, abs(x - bp)), start = st, alg = "plinear-brute")
    

    给予:

    > fm
    Nonlinear regression model
      model: y ~ cbind(1, abs(x - bp))
       data: parent.frame()
        bp  .lin1  .lin2 
    25.000  9.935  2.005 
     residual sum-of-squares: 81.29
    
    Number of iterations to convergence: 100 
    Achieved convergence tolerance: NA
    

    注意: 在上面我们假设bp 是x 范围内的一个整数,但是如果不希望这样的条件,我们可以通过使用nls2 的结果来放宽这一点作为nls优化的起始值,即nls(y ~ cbind(1, abs(x - bp)), start = coef(fm)[1], alg = "plinear")。

    【讨论】:

      猜你喜欢
      • 2016-03-03
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 2020-10-16
      • 1970-01-01
      • 2018-01-19
      相关资源
      最近更新 更多