stats | nls——求解非线性回归的待定参数

本篇介绍基础包stats中的一个函数nls(),它的作用是求解非线性回归的待定参数。

nls(formula, data, start,
    control, algorithm,
    trace, subset, weights,
    na.action, model,
    lower, upper, ...)

主要参数的要求如下:

  • formula:非线性回归的形式,格式为y ~ f(x1, x2,...)

  • data:数据源;一般为数据框或列表结构,不能为矩阵结构;

  • start:待定参数的起始值;列表结构。

比如,S型曲线可以很好地拟合传染病感染人数的增加趋势。本文示例使用的数据是2020年1月至3月我国新冠肺炎的确诊人数(示例数据可在后台发送关键词“示例数据”获取)。

library(readxl)
covid <- read_xlsx("107.1-china-covid19.xlsx",sheet = 1)

library(tidyverse)
library(magrittr)
data <- covid %>%
  group_by(date) %>%
  summarise(total = sum(confirmed))
plot(data$total, type = "l")
829789be31af92cf5632073351cd0849.png
  • 从上图可以看出,累积确诊人数的增长趋势大致呈S型。

一个典型的S型曲线的函数形式如下:

Scurve = function(t, a, b, c) 
  return(c/(1+exp(a*t+b)))

v = apply(matrix(-10:10), 1, Scurve,
      a = -1, b = 0, c = 80000)
plot(-10:10, v, type = "l")
6710b1fb37a0e05d51246a52003f995c.png

下面使用nls()函数求解S型函数的相关参数,也就是abc

data$t = 1:dim(data)[1]
fit <- nls(formula = total ~ Scurve(t, a, b, c),
           data = data,
           start = list(a = -1, b = 10, c = 80000))
fit
## Nonlinear regression model
##   model: total ~ Scurve(t, a, b, c)
##    data: data
##          a          b          c 
##    -0.2225     8.7560 81216.6896 
##  residual sum-of-squares: 202669699
## 
## Number of iterations to convergence: 8 
## Achieved convergence tolerance: 9.394e-06
  • 需要注意的是,起始值的选择非常关键,要尽量靠近真实值,若相差太大可能会报错。

plot(data$t, data$total, type = "l", 
     xlab = "天数", ylab = "累积确诊数")
lines(fitted(fit), col = "red",
      lty = 2)
legend("topleft", legend = c("原始数据", "拟合值"),
       lty = c(1,2), col = c("black", "red"))
c210689e76b15116db57e86b2fe1a455.png

下面仅使用前50天的数据来拟合S型曲线。

data2 = data[1:50,]
fit2 <- nls(formula = total ~Scurve(t, a, b, c),
            data = data2, 
            start = list(a = -1, b = 10, c = 80000))
fit2
## Nonlinear regression model
##   model: total ~ Scurve(t, a, b, c)
##    data: data2
##          a          b          c 
##    -0.2105     8.4264 86016.4864 
##  residual sum-of-squares: 159722376
## 
## Number of iterations to convergence: 16 
## Achieved convergence tolerance: 8.084e-06

plot(data2$t, data2$total, type = "l", 
     xlab = "天数", ylab = "累积确诊数",
     xlim = c(1,91), ylim = c(0, 85000))
lines(predict(fit2, data), col = "red",
      lty = 2)
legend("topleft", legend = c("原始数据", "拟合值"),
       lty = c(1,2), col = c("black", "red"))
bab3b1c692fc2eff8787c4d719c06d05.png
  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值