非线性拟合在R中

huangapple go评论46阅读模式
英文:

Non-linear fit in R

问题

我有一组数据:

x <- c(0.00100000, 0.08977778, 0.17855556, 0.26733333, 0.35611111, 0.44488889, 0.53366667, 0.62244444, 0.71122222)
y <- c(0.094982692, 0.074529874, 0.051782003, 0.026081288, -0.003576486, -0.038845028, -0.082764061, -0.141998524, -0.141998524)

我想将对数据进行对数曲线拟合(y = c * ln(x + k) + d)。我尝试在R中使用nls(结果不太理想)。我还尝试拟合指数曲线,但没有成功(拟合效果非常差)。我的问题是,您能否在数据中看到对数或指数相关性?我的最终目标是找到相关系数,以便我可以转换y并使相关性变为线性。感谢任何帮助。

英文:

I have a set of data:

x&lt;- c(0.00100000, 0.08977778, 0.17855556, 0.26733333, 0.35611111, 0.44488889, 0.53366667, 0.62244444, 0.71122222)
y &lt;- c(0.094982692, 0.074529874, 0.051782003, 0.026081288, -0.003576486, -0.038845028, -0.082764061, -0.141998524, -0.141998524)

and I wanted to fit a logarithmic curve to the data (y = c*naturalLog(x + k) + d). I tried nls in R ( which was as useful as tits on a fish). I also tried to fit exponential, with no sucess (fit was very bad). My question is, can you see a logarithmic or exponential correlation in the data? My end goal is to find the correlation coefficients, so I can transform y and make the correlation linear.

Any help would be appreciated.

答案1

得分: 2

这个威布尔样曲线在仅5次迭代中拟合成功:

fm <- nls(y ~ a + (1-a) * (1 - exp(b * x^c)), 
          data.frame(x, y), 
          start = list(a = 0.5, b = 1, c = 1))
fm

输出:

非线性回归模型
  模型: y ~ a + (1 - a) * (1 - exp(b * x^c))
   数据: data.frame(x, y)
      a       b       c 
0.09653 0.37163 1.17432 
 残差平方和: 0.0009496

收敛迭代次数: 5 
达到的收敛容差: 3.901e-06

拟合效果看起来不错:

plot(y ~ x)
lines(fitted(fm) ~ x, col = "red")

非线性拟合在R中

英文:

This Weibull-like curve fits in only 5 iterations:

fm &lt;- nls(y ~ a + (1-a) * (1 - exp(b * x^c)), 
          data.frame(x, y), 
          start = list(a = 0.5, b = 1, c = 1))
fm

giving:

Nonlinear regression model
  model: y ~ a + (1 - a) * (1 - exp(b * x^c))
   data: data.frame(x, y)
      a       b       c 
0.09653 0.37163 1.17432 
 residual sum-of-squares: 0.0009496

Number of iterations to convergence: 5 
Achieved convergence tolerance: 3.901e-06

The fit looks good:

plot(y ~ x)
lines(fitted(fm) ~ x, col = &quot;red&quot;)

非线性拟合在R中

答案2

得分: 1

以下是翻译好的代码部分:

library(minpack.lm)

x <- c(0.00100000, 0.08977778, 0.17855556, 0.26733333, 0.35611111, 0.44488889, 0.53366667, 0.62244444, 0.71122222)
y <- c(0.094982692, 0.074529874, 0.051782003, 0.026081288, -0.003576486, -0.038845028, -0.082764061, -0.141998524, -0.141998524)

# 创建一个数据框
df <- data.frame(x, y)

# 写出方程式
nls.eq <- as.formula(y ~ (c*log(x + k) + d))

# 使用 minpack.lm 包拟合方程
nls.out <- nlsLM(nls.eq, 
                 data = df,
                 start=list(c=1, k=1, d=1),
                 control  = nls.lm.control(maxiter = 500),
                 algorithm = "LM")
英文:

You can use following code

library(minpack.lm)

x &lt;- c(0.00100000, 0.08977778, 0.17855556, 0.26733333, 0.35611111, 0.44488889, 0.53366667, 0.62244444, 0.71122222)
y &lt;- c(0.094982692, 0.074529874, 0.051782003, 0.026081288, -0.003576486, -0.038845028, -0.082764061, -0.141998524, -0.141998524)

#Create a data frame
df &lt;- data.frame(x, y)

#Write the equation
nls.eq &lt;- as.formula(y ~ (c*log(x + k) + d))

#Fitting equation using minpack.lm package
nls.out &lt;- nlsLM(nls.eq, 
                 data = df,
                 start=list(c=1, k=1, d=1),
                 control  = nls.lm.control(maxiter = 500),
                 algorithm = &quot;LM&quot;)

答案3

得分: 0

你说你使用了这段代码:

m &lt;- nls(1-x ~ a * log(y + b) , data = df, start = list(a = 1, b = 1), nls.control(maxiter = 500))

而不是 x(死亡率),我使用了 1-x(出生率)。

通常情况下,我们使用 y 作为响应变量,而不是 x。同时要小心,birth_rate = 1 - death_rate 不一定成立,更准确的说法是生存率等于 1 减去死亡率。

英文:

You've said that you used this code:

m &lt;- nls(1-x ~ a * log(y + b) , data = df, start = list(a = 1, b = 1), nls.control(maxiter = 500)) 

> Instead of x (death rate), I used 1-x (birth rate).

Normally we use y as the response, not x. Also be careful that birth_rate = 1 - death_rate is not generally true, rather survival rate is 1 minus death rate.

答案4

得分: 0

一种拟合对数函数的替代方法:

非线性拟合在R中

非线性拟合在R中

所使用的方法的一般原理在这里解释:https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales 拟合不是针对方程模型进行的,而是针对线性积分方程,方程模型是其解的情况。在当前情况下,线性积分方程为:非线性拟合在R中。这避免了迭代计算,无需“猜测”参数的初始值。

英文:

An alternative method to fit the logarithmic function :

非线性拟合在R中

非线性拟合在R中

The general principle of the method used is explained in : https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales The fitting isn't wrt the equation model but wrt a linear integral equation to which the equation model is solution. In the present case the linear integral equation is : 非线性拟合在R中. This avoid iterative calculus and no need for "guessed" initial values of the parameters.

huangapple
  • 本文由 发表于 2023年6月12日 20:44:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456797.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定