【问题标题】:2 curves simultaneous nonlinear regression2条曲线同时非线性回归
【发布时间】:2014-12-10 16:01:42
【问题描述】:

我正在尝试使用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

【问题讨论】:

    标签: r regression curve-fitting data-fitting nls


    【解决方案1】:

    这是一种方法。 (编辑时:这很好用,我的原始代码中的一个错字使它看起来好像不起作用,感谢@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
    

    【讨论】:

    • 我认为您的方程式中有错字。这个组合模型应该是fit0&lt;-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), trace=T),它给出了更相似的估计:a=48.50367, b=0.02047, c=0.09546
    • 啊哈。这就解释了。现在修复它。
    • 实际上,仔细想想,fit 使用了(100/(a*exp(-b*x)))。毕竟我不确定这是一个错字,我认为是乘法错误。
    • 您的 fit 与 OP 的 fit 不同。我很确定这是一个错字。
    • 是的,你们都是对的。再次编辑。感谢校对。
    猜你喜欢
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 2012-09-17
    • 2013-10-25
    相关资源
    最近更新 更多