r - 为什么 nls(非线性最小二乘法)在 R 中不起作用

标签 r regression nonlinear-functions

我正在尝试运行 nls 并使用以下代码获取已知参数:

我创建一个数据框:

xx = 1:100
yy = 0.5*xx^2
dd = data.frame(xx,yy)
str(dd)
'data.frame':   100 obs. of  2 variables:
 $ xx: int  1 2 3 4 5 6 7 8 9 10 ...
 $ yy: num  0.5 2 4.5 8 12.5 18 24.5 32 40.5 50 ...

head(dd)
  xx   yy
1  1  0.5
2  2  2.0
3  3  4.5
4  4  8.0
5  5 12.5
6  6 18.0

我对数据运行 nls:

> nls(yy ~ A*(xx^B), data=dd)
Error in nls(yy ~ A * xx^B, data = dd) : 
  number of iterations exceeded maximum of 50
In addition: Warning message:
In nls(yy ~ A * xx^B, data = dd) :
  No starting values specified for some parameters.
Initializing ‘A’, ‘B’ to '1.'.
Consider specifying 'start' or using a selfStart model

我尝试添加“开始”参数:

> nls(yy ~ A*(xx^B), data=dd, start=c(A=0.1, B=0.1))
Error in numericDeriv(form[[3L]], names(ind), env) : 
  Missing value or an infinity produced when evaluating the model

错误在哪里?为什么我没有得到参数 A=0.5 和 B=2?

最佳答案

阅读?nls帮助页面。特别是关于在人工“零残留”数据上使用 nls 的警告。尝试一下

yy = 0.5*xx^2 + rnorm(length(xx))

添加一些噪音

关于r - 为什么 nls(非线性最小二乘法)在 R 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26953637/

相关文章:

r - Crosstable 类似于 R 中的 Stata

r - 非线性回归 'abline'

python - 多元回归 Python

matlab - 具有可变限制的积分

algorithm - 非线性计数器

r - dplyr中的字符串操作/聚合

r - 在 data.frame 中找到最高比例

r - 在 RCommander 中使用 ggplot2

r - 如何制作R中回归估计量线性组合的置信区间?

c# - 如何枚举 x^2 + y^2 = z^2 - 1(有附加约束)