Curve fitting in R using nls

百般思念 提交于 2019-12-19 02:09:53

问题


I'm trying to fit a curve over (the tail of) the following data:

 [1]   1   1   1   1   1   1   2   1   2   2   3   2   1   1   4   3   2  11   6   2  16   7  17  36
[25]  27  39  41  33  42  66  92 138 189 249 665 224 309 247 641 777 671 532 749 506 315 292 281 130
[49] 137  91  40  27  34  19   1

I'm using the following function in R to accomplish this:

nls(y~axexp(-b*x^2),start=list(a=1,b=1),trace=TRUE)

However, I'm getting the following error:

3650202 : 1 1

Error in numericDeriv(form[[3L]], names(ind), env) : Missing value or an infinity produced when evaluating the model

When using the following, artificial values for x and y, everything works just fine:

y=x*exp(-.5*x^2)+rnorm(length(x),0,0.1)

x
  [1] 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90
 [20] 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.80 1.85
 [39] 1.90 1.95 2.00 2.05 2.10 2.15 2.20 2.25 2.30 2.35 2.40 2.45 2.50 2.55 2.60 2.65 2.70 2.75 2.80
 [58] 2.85 2.90 2.95 3.00 3.05 3.10 3.15 3.20 3.25 3.30 3.35 3.40 3.45 3.50 3.55 3.60 3.65 3.70 3.75
 [77] 3.80 3.85 3.90 3.95 4.00 4.05 4.10 4.15 4.20 4.25 4.30 4.35 4.40 4.45 4.50 4.55 4.60 4.65 4.70
 [96] 4.75 4.80 4.85 4.90 4.95 5.00
y
  [1] -0.080214106  0.075247488  0.076355116 -0.020087646  0.181314038  0.075832658  0.248303254
  [8]  0.364244010  0.453655908  0.347854869  0.514373164  0.384051249  0.618584696  0.515684390
 [15]  0.534737770  0.609279111  0.618936091  0.534443863  0.739118585  0.677679546  0.526011452
 [22]  0.645645150  0.578274968  0.589619834  0.476186241  0.621638333  0.601663144  0.535981735
 [29]  0.518434367  0.581735107  0.423872948  0.445335110  0.340884242  0.317121065  0.342683141
 [36]  0.278351104  0.402947372  0.429483276  0.276655872  0.108164828  0.389994138  0.372300257
 [43] -0.057320612  0.131271986  0.226212869  0.131171973  0.245970674  0.009926555  0.173465207
 [50]  0.141220590  0.280616078  0.108515613  0.117697407  0.130700771  0.058540888  0.251613512
 [57]  0.168094899 -0.058382571  0.123306762 -0.048605186 -0.010131767  0.076701962 -0.051982924
 [64]  0.058427540  0.144665070  0.063998841 -0.010495697  0.119868854  0.114447318  0.006759691
 [71]  0.025041761 -0.178145771  0.041547126  0.122084819  0.034283141  0.209140060  0.197024853
 [78] -0.005491966 -0.033260219 -0.028123314 -0.005775553 -0.040781462  0.090024896  0.116390743
 [85] -0.017811031  0.094039200 -0.147064060 -0.057249278  0.211587898 -0.066153592  0.032100332
 [92] -0.092756136 -0.125906598  0.136937364  0.046453010  0.002000336 -0.134047101  0.089748847
 [99] -0.019355567 -0.042158950  0.149594368

Can anyone point out what I'm doing wrong? Thanks for your help.


回答1:


Well I found the answer to my problem. The starting values for the real data are completely different from the dummy values: a=500 and b=.1 result in a nice fit. Just thought it might be useful to mention that here.



来源:https://stackoverflow.com/questions/2243046/curve-fitting-in-r-using-nls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!