美文网首页
R语言 非线性最小二乘

R语言 非线性最小二乘

作者: yuanyb | 来源:发表于2017-11-13 19:02 被阅读0次

当模拟真实世界数据用于回归分析时,我们观察到,很少情况下,模型的方程是给出线性图的线性方程。大多数时候,真实世界数据模型的方程涉及更高程度的数学函数,如3的指数或sin函数。在这种情况下,模型的图给出了曲线而不是线。线性和非线性回归的目的是调整模型参数的值,以找到最接近您的数据的线或曲线。在找到这些值时,我们将能够以良好的精确度估计响应变量。

在最小二乘回归中,我们建立了一个回归模型,其中来自回归曲线的不同点的垂直距离的平方和被最小化。我们通常从定义的模型开始,并假设系数的一些值。然后我们应用R语言的nls()函数获得更准确的值以及置信区间。

语法

在R语言中创建非线性最小二乘测试的基本语法是 -

<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">nls(formula, data, start)
</pre>

以下是所使用的参数的描述 -

  • formula是包括变量和参数的非线性模型公式。

  • data是用于计算公式中变量的数据框。

  • start是起始估计的命名列表或命名数字向量。

我们将考虑一个假设其系数的初始值的非线性模型。 接下来,我们将看到这些假设值的置信区间是什么,以便我们可以判断这些值在模型中有多好。

所以让我们考虑下面的方程为这个目的 -

<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">a = b1*x^2+b2
</pre>

让我们假设初始系数为1和3,并将这些值拟合到nls()函数中。

<pre class="prettyprint notranslate tryit" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">xvalues <- c(1.6,2.1,2,2.23,3.71,3.25,3.4,3.86,1.19,2.21)
yvalues <- c(5.19,7.43,6.94,8.11,18.75,14.88,16.06,19.12,3.21,7.58)

Give the chart file a name.

png(file = "nls.png")

Plot these values.

plot(xvalues,yvalues)

Take the assumed values and fit into the model.

model <- nls(yvalues ~ b1*xvalues^2+b2,start = list(b1 = 1,b2 = 3))

Plot the chart with new data by fitting it to a prediction from 100 data points.

new.data <- data.frame(xvalues = seq(min(xvalues),max(xvalues),len = 100))
lines(new.data$xvalues,predict(model,newdata = new.data))

Save the file.

dev.off()

Get the sum of the squared residuals.

print(sum(resid(model)^2))

Get the confidence intervals on the chosen values of the coefficients.

print(confint(model))
</pre>

当我们执行上面的代码,它产生以下结果 -

<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[1] 1.081935
Waiting for profiling to be done...
2.5% 97.5%
b1 1.137708 1.253135
b2 1.497364 2.496484
</pre>

非线性至少方块R

我们可以得出结论,b1的值更接近1,而b2的值更接近2而不是3。

相关文章

  • R语言 非线性最小二乘

    当模拟真实世界数据用于回归分析时,我们观察到,很少情况下,模型的方程是给出线性图的线性方程。大多数时候,真实世界数...

  • 矩阵: QR分解 && 最小二乘问题求解

    最小二乘问题分为线性最小二乘问题和非线性最小二乘问题;非线性最小二乘问题求解方法有高斯牛顿法,Levenberg-...

  • ceres solver 01 入门小例子

    1. 基本介绍 Ceres Solver是用来解决非线性最小二乘问题的库。 非线性最小二乘问题用公式表达如下:其中...

  • 非线性最小二乘法

    很多问题最终归结为一个最小二乘问题,求解最小二乘的方法也很多。 内容来自Gauss-Newton非线性最小二乘算法...

  • 线性代数与数值方法

    主要分以下几个方面进行说明: 一、矩阵分解 二、线性最小二乘 三、非线性最小二乘 四、直接稀疏矩阵方法 五、迭代方...

  • 线性最小二乘和非线性最小二乘

    本文基于下面的博客,结合自己第一次看的时候的一些问题,重新梳理总结一下https://blog.csdn.net/...

  • 2018.06.04-2018.06.10工作总结

    上周已完成的工作 学习相机模型与图像的表示学习状态估计的几个基本方法:最大后验、最大似然与最小二乘研究非线性最小二...

  • 激光SLAM概述

    激光SLAM的pipeline 激光雷达去畸变 激光帧间(核心算法)|前端匹配 激光回环检测 非线性最小二乘优化(...

  • 深入理解卡尔曼滤波

    1. 最小二乘(LS)、加权最小二乘估计(WLS)、递推最小二乘(RLS) 观测方程![](http://late...

  • R语言统计:偏最小二乘路径模型(plspm)

    贴一下官方镜像的网址,3.5的环境安装失败,提示amap包错误,查看镜像amap包更新到了R3.6,下载Windo...

网友评论

      本文标题:R语言 非线性最小二乘

      本文链接:https://www.haomeiwen.com/subject/kbxtvxtx.html