【问题标题】:R: Error in function "nls" for my datsetR:我的数据集的函数“nls”错误
【发布时间】:2013-12-05 20:05:39
【问题描述】:

我有气候压力值和压力测量站高度的数据。我想为他们拟合一个指数模型。数据如下所示:

x
 [1]  539  575 1320  438 1840  496  316  552  325  386 1599 1073  556 1029 1661
[16] 2472 1594 1197  910 1035  596  646  420  516 1980 1045 2287  440  419 1611
[31]  577 3580  484 1018 1669  745 1974  366  273  454  203  588 1427  405 1403
[46]  485  490 2106  990 3305 1078  455  300 1638 1708  438 1303  482  775 2502
[61]  457 2690  422 1638  555  426

y
[1] 954.1 951.4 867.2 964.0 813.3 958.8 979.7 950.8 978.4 971.3 835.1 894.1
[13] 952.0 904.4 833.3 751.5 839.0 882.5 912.0 899.4 947.1 942.3 968.5 961.9
[25] 801.3 893.6 769.8 965.6 965.1 836.9 949.2 653.6 959.8 900.2 830.6 928.6
[37] 800.3 971.1 983.5 963.4 992.6 947.5 848.3 969.4 858.2 959.9 959.3 787.2
[49] 900.4 677.6 893.2 962.7 981.5 834.9 827.0 966.0 870.1 961.1 925.2 749.3
[61] 962.8 734.0 968.0 836.3 950.4 966.5

我首先尝试对数据取对数并拟合lm他们:

log.p=log(y)
log.height=log(x)
lmlog=lm(log.p~log.height)

但是由于这提供了一个根本不适合的模型,我决定使用 nls 函数,并从其他帖子中获取了各种提示(例如“开始”):

f <- function(x,a,b) {a* exp(b * x)}
dat <- as.list(x, y)
start <- coef(nls(log(y) ~ log(f(x, a, b)), dat, start = c(a = 1, b = -1)))
nls=nls(y~ f(x,a,b), data=dat, start=start)

不幸的是,即使是“开始”,也会出现以下错误,我真的不知道该怎么办了......

Error in numericDeriv(form[[3L]], names(ind), env) : 
  Missing value or an infinity produced when evaluating the model
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf

有人可以帮忙吗? 提前谢谢!!!

【问题讨论】:

    标签: r exponential nls model-fitting


    【解决方案1】:

    转换两个变量不会使关系线性化:

    plot(log(y)~log(x))
    

    相反,您应该只转换 y:

    plot(log(y)~x)
    modlm <- lm(log(y)~x)
    abline(modlm)
    

    summary(modlm)
    
    Call:
    lm(formula = log(y) ~ x)
    
    Residuals:
           Min         1Q     Median         3Q        Max 
    -0.0081825 -0.0009194  0.0000952  0.0008455  0.0070058 
    
    Coefficients:
                  Estimate Std. Error t value Pr(>|t|)    
    (Intercept)  6.927e+00  4.567e-04 15166.4   <2e-16 ***
    x           -1.227e-04  3.516e-07  -349.1   <2e-16 ***
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    
    Residual standard error: 0.002185 on 64 degrees of freedom
    Multiple R-squared:  0.9995,    Adjusted R-squared:  0.9995 
    F-statistic: 1.219e+05 on 1 and 64 DF,  p-value: < 2.2e-16
    

    当然你也可以使用nls

    modnls <- nls(y~exp(a+b*x), start=list(a=coef(modlm)[[1]], b=coef(modlm)[[2]]))
    plot(y~x)
    xnew <- seq(min(x), max(x), by=0.5)
    lines(xnew, exp(coef(modnls)[[1]]+xnew*coef(modnls)[[2]]))
    

    summary(modnls)
    
    Formula: y ~ exp(a + b * x)
    
    Parameters:
        Estimate Std. Error t value Pr(>|t|)    
    a  6.926e+00  4.384e-04 15797.5   <2e-16 ***
    b -1.225e-04  3.831e-07  -319.7   <2e-16 ***
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    
    Residual standard error: 1.904 on 64 degrees of freedom
    
    Number of iterations to convergence: 2 
    Achieved convergence tolerance: 6.25e-08
    

    请注意线性拟合和非线性拟合的参数估计值非常相似。通常,您应该根据误差分布选择是使用lm 处理转换后的数据还是使用nls 处理未转换的数据。不过,由于yx 之间的关系不是很非线性,所以这里没那么重要。

    【讨论】:

    • 非常感谢您的帮助,甚至彻底纠正了我的“愚蠢”错误! ;-) 由于数据来自这里大气的较低部分,线性和非线性拟合的接近程度是可以预料的,所以这正是我想要观察的!
    猜你喜欢
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2017-12-25
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    相关资源
    最近更新 更多