r - 具有二次多项式和在断点处平滑连接的直线的分段回归

标签 r regression linear-regression lm piecewise

我想用一个断点拟合分段线性回归 xt ,对于 x < xt我们有一个二次多项式,对于 x >= xt我们有一条直线。两部分应该顺利连接,连续性达到 xt 处的一阶导数.这是它可能的样子的图片:

piecewise regression

我将分段回归函数参数化为:

regression function

哪里a , b , cxt是要估计的参数。

我想根据调整后的 R 平方将这个模型与整个范围内的二次多项式回归进行比较。

这是我的数据:

y <- c(1, 0.59, 0.15, 0.078, 0.02, 0.0047, 0.0019, 1, 0.56, 0.13, 
0.025, 0.0051, 0.0016, 0.00091, 1, 0.61, 0.12, 0.026, 0.0067, 
0.00085, 4e-04)

x <- c(0, 5.53, 12.92, 16.61, 20.3, 23.07, 24.92, 0, 5.53, 12.92, 
16.61, 20.3, 23.07, 24.92, 0, 5.53, 12.92, 16.61, 20.3, 23.07, 
24.92)

scatter plot

我的尝试如下,对于一个已知的 xt :
z <- pmax(0, x - xt)
x1 <- pmin(x, xt)
fit <- lm(y ~  x1 + I(x1 ^ 2) + z - 1)

但直线似乎与 xt 处的二次多项式不相切。 . 我哪里做错了?

类似问题:
  • Piecewise regression with a straight line and a horizontal line joining at a break point
  • Fitting a V-shape curve to my data (在交叉验证上)
  • 最佳答案

    在本节中,我将演示一个可重现的示例。请确保您已在其他答案中定义了源函数。

    ## we first generate a true model
    set.seed(0)
    x <- runif(100)  ## sample points on [0, 1]
    beta <- c(0.1, -0.2, 2)  ## true coefficients
    X <- getX(x, 0.6)  ## model matrix with true break point at 0.6
    y <- X %*% beta + rnorm(100, 0, 0.08)  ## observations with Gaussian noise
    plot(x, y)
    

    scatter plot

    现在,假设我们不知道 c ,我们想在均匀分布的网格上搜索:
    c.grid <- seq(0.1, 0.9, 0.05)
    fit <- choose.c(x, y, c.grid)
    fit$c
    

    choose c
    RSS 选择了 0.55。这与真实值 0.6 略有不同,但从图中我们看到 RSS 曲线在 [0.5, 0.6] 之间变化不大,所以我对此很满意。

    生成的模型 fit 包含丰富的信息:
    #List of 12
    # $ coefficients : num [1:3] 0.114 -0.246 2.366
    # $ residuals    : num [1:100] 0.03279 -0.01515 0.21188 -0.06542 0.00763 ...
    # $ fitted.values: num [1:100] 0.0292 0.3757 0.2329 0.1087 0.0263 ...
    # $ R            : num [1:3, 1:3] -10 0.1 0.1 0.292 2.688 ...
    # $ sig2         : num 0.00507
    # $ coef.table   : num [1:3, 1:4] 0.1143 -0.2456 2.3661 0.0096 0.0454 ...
    #  ..- attr(*, "dimnames")=List of 2
    #  .. ..$ : chr [1:3] "beta0" "beta1" "beta2"
    #  .. ..$ : chr [1:4] "Estimate" "Std. Error" "t value" "Pr(>|t|)"
    # $ aic          : num -240
    # $ bic          : num -243
    # $ c            : num 0.55
    # $ RSS          : num 0.492
    # $ r.squared    : num 0.913
    # $ adj.r.squared: num 0.911
    

    我们可以提取系数的汇总表:
    fit$coef.table
    #        Estimate  Std. Error   t value     Pr(>|t|)
    #beta0  0.1143132 0.009602697 11.904286 1.120059e-20
    #beta1 -0.2455986 0.045409356 -5.408546 4.568506e-07
    #beta2  2.3661097 0.169308226 13.975161 5.730682e-25
    

    最后,我们想看一些预测图。
    x.new <- seq(0, 1, 0.05)
    p <- pred(fit, x.new)
    
    head(p)
    #           fit     se.fit       lwr       upr
    #[1,] 0.9651406 0.02903484 0.9075145 1.0227668
    #[2,] 0.8286400 0.02263111 0.7837235 0.8735564
    #[3,] 0.7039698 0.01739193 0.6694516 0.7384880
    #[4,] 0.5911302 0.01350837 0.5643199 0.6179406
    #[5,] 0.4901212 0.01117924 0.4679335 0.5123089
    #[6,] 0.4009427 0.01034868 0.3804034 0.4214819
    

    我们可以做一个情节:
    plot(x, y, cex = 0.5)
    matlines(x.new, p[,-2], col = c(1,2,2), lty = c(1,2,2), lwd = 2)
    

    confidence band

    关于r - 具有二次多项式和在断点处平滑连接的直线的分段回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39418783/

    相关文章:

    r - 错误 : argument "x" is missing, 没有默认值

    machine-learning - MLJ : selecting rows and columns for training in evaluate

    r - glmnet:我怎么知道我的响应的哪个因子水平在逻辑回归中被编码为 1

    r - mgcv:如何设置样条线的结的数量和/或位置

    apache-spark - Apache Spark 中的线性回归给出错误的截距和权重

    math - 梯度下降算法中的delta到底是什么意思?

    r - 引用其他列的条件更新

    r - R中的部分动物字符串匹配

    python - 使用 statsmodels 线性回归拟合下降趋势(负斜率)

    R 函数使用 for 循环返回 data.frame