r - 2条曲线同时非线性回归

标签 r regression curve-fitting data-fitting nls

我正在尝试使用 nlm 拟合两条曲线,但遇到了一些问题。首先,我尝试分别拟合每条曲线,一切正常,获得的参数与用于模拟曲线的参数相似。

每条曲线都由不同的方程定义,但它们共享一些参数。因此,我想知道是否可以使用 nlm 或其他优化方法同时拟合两条曲线。

x<-seq(0,120, by=5)
y<-100/50*exp(-0.02*x)*rnorm(25, mean=1, sd=0.05)
y2<-(1*100/50)*(0.1/(0.1-0.02))*(exp(-0.02*x)-exp(-0.1*x))*rnorm(25, mean=1, sd=0.05)
xy<-data.frame(x,y)
xy2<-data.frame(x,y2)
fit<-nls(y~100/a*exp(-b*x), data=xy, start=c(a=45, b=0.018), trace=T)

fit

# Nonlinear regression model
#  model: y ~ 100/a * exp(-b * x)
#   data: xy
#       a        b 
# 51.68688  0.01936 
# residual sum-of-squares: 0.01934
#
# Number of iterations to convergence: 4 
# Achieved convergence tolerance: 1.395e-07

fit2<-nls(y2~100/a*(c/(c-b))*(exp(-b*x)-exp(-c*x)), data=xy2, 
    start=c(a=45, b=0.018, c=0.15), trace=T)

fit2

# Nonlinear regression model
#  model: y2 ~ 100/a * (c/(c - b)) * (exp(-b * x) - exp(-c * x))
#   data: xy2
#        a        b        c 
# 49.92938  0.01997  0.09903 
# residual sum-of-squares: 0.03024
#
# Number of iterations to convergence: 4 
# Achieved convergence tolerance: 2.12e-06

最佳答案

这是一种方法。 (编辑时:这工作正常,我原始代码中的一个拼写错误使它看起来不起作用,感谢@MrFlick和@Gregor指出了这一点)。首先使用固定的随机种子复制您的代码:

set.seed(1)
x<-seq(0,120, by=5)
y<-100/50*exp(-0.02*x)*rnorm(25, mean=1, sd=0.05)
y2<-(1*100/50)*(0.1/(0.1-0.02))*(exp(-0.02*x)-exp(-0.1*x))*rnorm(25, mean=1, sd=0.05)
xy<-data.frame(x,y)
xy2<-data.frame(x,y2)

fit<-nls(y~100/a*exp(-b*x), data=xy, start=c(a=45, b=0.018))
fit
#     Nonlinear regression model
#   model: y ~ 100/a * exp(-b * x)
#    data: xy
#        a        b 
# 50.29461  0.01962 
#  residual sum-of-squares: 0.0362

# Number of iterations to convergence: 4 
# Achieved convergence tolerance: 1.719e-07


fit2<-nls(y2~100/a*(c/(c-b))*(exp(-b*x)-exp(-c*x)), data=xy2, start=c(a=45, b=0.018, c=0.15))
fit2
# Nonlinear regression model
#   model: y2 ~ 100/a * (c/(c - b)) * (exp(-b * x) - exp(-c * x))
#    data: xy2
#        a        b        c 
# 49.26033  0.02041  0.09455 
#  residual sum-of-squares: 0.02364
#
# Number of iterations to convergence: 5 
# Achieved convergence tolerance: 8.4e-06

现在将它们组合起来:

xy0<-data.frame(x=c(x,x),y=c(y,y2),isY1=c(rep(c(1,0),each=length(x))),
                isY2=c(rep(c(0,1),each=length(x))))
fit0<-nls(y~isY1*(100/a*exp(-b*x))+isY2*(100/a*(c/(c-b))*(exp(-b*x)-exp(-c*x))), data=xy0, start=c(a=45, b=0.018,c=0.15))


fit0
# Nonlinear regression model
#   model: y ~ isY1 * (100/a * exp(-b * x)) + isY2 * (100/a * (c/(c - b)) *     (exp(-b * x) - exp(-c * x)))
#    data: xy0
#        a        b        c 
# 50.19176  0.01978  0.09800 
#  residual sum-of-squares: 0.06114

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

关于r - 2条曲线同时非线性回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27405484/

相关文章:

r - 计算数据帧最后 3M 值的平均值并将它们添加到数据帧中,重复 18 次,无需在 R 中使用循环

r - 在 R 中,如何过滤数据框以仅包含具有 >=2 个非 NA 值的行?

python - 回归神经网络的评估

algorithm - 曲线拟合的聚类算法

r - 如何为 R 中传单中的数值变量设置不对称颜色渐变

r - 使用 R-lsa 包计算语义空间中文档之间的余弦相似度

c++ - SVM 支持 vector 机回归 openCv c++

r - biglm 预测无法分配大小为 xx.x MB 的向量

python - 使用 scipy curve_fit 拟合噪声指数的建议?

python-3.x - 类型错误 : Improper input: N=2 must not exceed M=1