更好地适应 R 中的自定义函数
Better Fit of Custom Function in R
我在我的数据中拟合了一条非线性曲线。我使用的代码是:
ggplot(data, aes(x, y)) +
geom_point() +
stat_smooth(method = "nls",
formula = "y ~ (a/x)",
method.args = list(algorithm = "port", start = c(a = 1)),
se = FALSE) +
labs(x = "x", y = "y") +
scale_y_continuous(labels = comma)
我使用公式 y ~ (a/x)
作为开始 a=1
,因为我假设我的数据遵循此规则。但是看到图表,我可以看出这不是最合适的。我可以做些什么来拟合更好的 a/x
曲线吗?就像第一次观察的曲线更陡峭,其余的曲线更平坦?
这是我的数据集:
y x
318088 2.818808
336211 2.874786
401643 2.732688
435816 2.580749
378424 2.862525
379680 2.762701
354782 2.839747
409505 2.712271
382991 2.783217
484853 2.865189
481788 2.613035
387994 3.013499
375327 2.902625
399366 2.822512
457983 2.778006
530702 2.853720
554820 2.710258
547952 2.676447
525960 2.734852
509157 2.709694
482776 2.582611
478888 2.894800
343332 3.128868
318626 3.310392
279998 2.820940
232878 3.219122
342657 2.880345
454904 2.756803
597070 2.740670
629832 2.739997
645339 2.456461
601518 2.533754
593473 3.063990
596382 2.701835
562774 2.805480
404588 2.915558
347486 2.934605
434991 2.621134
426929 2.532104
348084 2.899483
348785 2.789879
334617 2.939476
322318 2.843211
310848 2.817476
270500 2.683987
224446 2.554492
213684 2.809247
282404 2.640408
336469 2.941160
366556 2.869811
358514 3.052722
404425 2.798377
411394 2.584497
370665 2.763246
375990 2.769727
363207 2.584378
325418 2.717353
307332 2.880379
403078 2.567024
386521 2.573119
329488 2.829060
297818 2.879921
313291 2.922753
309145 2.966452
302217 2.845400
336894 2.578288
335552 2.653786
303367 2.622335
342057 2.745391
350693 3.423275
326369 2.652801
322293 2.699542
395416 2.539014
393757 2.452486
340809 2.552500
333703 2.677471
323882 2.802757
291526 2.994942
289024 2.868676
361007 2.613192
337997 2.465057
256454 2.533094
262345 2.616848
195605 2.657868
177227 2.817398
172935 2.483164
179142 2.343136
178935 2.720142
177627 2.543538
183297 2.710079
277263 2.569121
314612 2.921395
394498 2.701341
566969 2.715369
573897 2.695608
533915 2.871800
431106 2.804339
487278 2.889213
522717 5.382958
503169 2.889163
587002 2.592890
652177 2.667569
547244 2.927379
527966 2.855652
542794 2.767391
500434 2.810982
480563 3.211367
559648 2.734722
568892 2.644250
485245 2.663728
522124 2.662655
509209 2.703339
513912 2.678759
451048 2.931140
545108 2.724209
622589 2.934804
507540 2.910144
496703 2.897143
488153 2.541633
494480 2.608369
450904 2.730225
545874 2.555485
574458 2.572568
497318 2.629635
484248 2.752913
439854 2.789159
435549 2.791774
410635 2.804586
502417 2.683846
536582 2.649824
462139 2.661041
477259 2.717991
498415 2.949599
464460 2.583202
402989 2.708579
482276 2.676654
543463 2.721056
483854 2.659228
472970 3.016031
467194 3.423359
458830 3.151692
388446 3.047256
443329 3.061273
475608 3.160686
436197 3.142662
422146 3.183787
421591 3.367449
417524 3.130931
397089 3.147788
469774 3.054959
537462 3.378351
457240 3.067574
421987 3.289652
452076 3.282907
463292 2.952140
411454 3.062896
469632 3.125029
499961 3.192651
470231 3.052396
426435 3.084369
413277 3.044155
332061 3.074529
295336 3.030897
354612 3.132999
373374 3.009976
336148 3.038227
328659 3.135106
320652 3.150984
325762 2.926446
320949 3.045080
350623 2.943999
288277 3.162889
227425 3.054258
185629 2.906675
246709 2.984109
276159 4.300290
262537 3.116466
293402 3.283875
297795 3.007910
249537 2.996318
267314 3.354558
278516 3.100201
270348 3.023718
261186 3.068398
275437 3.085977
282662 3.049299
268530 3.161894
250753 3.210491
238059 3.197553
231861 3.172572
236661 3.091866
274325 3.284630
281563 3.092375
251875 3.120738
233500 3.048110
234641 3.256575
235626 3.102921
240376 3.226411
278165 2.905918
294890 3.043176
262143 3.163785
243308 3.109935
248225 3.221056
254422 3.616995
255737 3.291750
262492 3.190569
242269 3.262991
218760 3.276030
206165 3.376757
199568 3.192706
199901 3.228047
203278 3.361286
221573 3.135710
236671 3.336578
224909 3.285750
212335 3.409516
209672 3.100753
206667 3.477523
212272 3.341284
244915 3.271667
254639 3.224289
209344 3.275723
191725 3.168092
197447 3.178668
192552 3.260977
205142 3.163146
246057 4.152538
256093 3.317521
222355 3.396396
220011 3.475071
209645 3.352367
210690 3.642851
211753 3.475454
245137 3.458855
225789 3.352159
224473 3.349224
219929 3.329815
272416 3.251371
299300 3.383322
296407 3.465826
350088 3.413542
363884 3.777645
317916 3.635040
307377 3.730956
303743 3.838431
299388 3.606044
299031 3.862217
352912 4.120376
363647 3.423283
322933 3.572628
337366 3.925978
308285 3.980198
304655 4.005487
283874 3.582592
336132 4.145034
336230 3.954005
275528 3.889334
271113 3.935482
276283 3.887819
274942 3.900225
266933 3.762177
278204 3.590627
291083 3.475412
250593 3.571164
182623 3.696895
177937 3.702510
179893 3.506154
180820 3.483347
189747 3.411142
177318 3.448681
183941 3.581280
177965 3.831371
171082 3.918870
173392 3.655046
213628 3.558670
265686 3.441305
274562 3.308130
243729 3.527024
269196 3.570593
282492 3.460696
268099 3.621155
270395 3.500871
354696 3.342175
323164 3.579816
219246 3.430618
222832 3.512364
215442 3.692077
217057 3.523960
204375 3.587310
227147 3.513631
233728 3.396191
230588 3.417746
241174 3.742648
233384 3.512578
230285 3.605550
233691 3.524610
261651 3.424540
259537 3.422988
249027 3.547568
236783 3.455910
236808 3.545529
240779 3.794796
232181 3.352256
260543 3.409897
255879 3.561510
233716 3.355982
217540 3.366562
236359 3.746781
201513 3.798635
227904 3.893903
281891 3.640823
279886 3.937271
266120 3.708926
248266 3.809452
239477 3.990469
247679 3.872256
250121 4.027336
283313 3.621488
282773 3.089742
245995 3.027371
237379 3.031593
231324 3.222918
230889 2.932897
238313 3.001761
277913 2.828859
281245 3.162213
258537 3.186444
255604 2.923421
259266 2.835952
261345 2.876915
260054 2.986111
292471 2.895799
324378 3.008379
270807 3.064305
276748 3.107425
274135 3.074070
258643 2.993347
249392 3.036252
275747 3.113728
272187 2.948600
249579 3.348998
243490 3.175328
244576 3.049288
244391 3.183174
234976 3.228730
262768 3.051605
266822 3.066944
256679 3.192835
252421 3.247266
246730 3.175772
243815 3.101510
229785 3.324920
252492 3.317948
272911 3.173554
247775 3.202868
255683 3.066175
230019 3.140274
229951 3.137172
230863 3.183656
254420 2.985195
263055 2.923506
243070 3.154108
246389 3.103570
246786 3.098457
241969 3.066267
239544 3.208468
273433 2.993599
265125 3.071725
250987 3.108260
259616 3.005487
238194 3.169553
240198 3.029928
238518 3.104733
263671 3.347683
264454 3.201232
255704 3.241008
241042 3.222571
232599 3.487346
232822 3.635051
235612 3.725143
258297 3.471994
259056 3.488392
237961 3.432317
229838 4.296460
226430 4.835155
226270 4.862763
238614 4.511147
268627 4.859204
264323 5.455362
238190 5.473459
248697 4.874474
250119 4.672262
245319 4.909624
239323 4.623515
265408 5.296186
270663 4.318392
249145 4.801727
240939 5.209521
239549 5.151567
231575 4.808046
238310 4.212207
254595 3.804590
250701 3.735263
244254 4.168639
234574 5.147149
242680 4.777019
230793 5.157367
239942 4.592258
258330 4.476864
270651 4.724341
243033 4.927935
236412 4.882507
237512 4.766240
237161 5.025718
236633 5.197085
257124 4.862109
253096 4.850248
242846 5.257920
245173 5.158687
244075 4.749932
239519 4.639774
242199 4.919499
261240 4.591937
256211 4.584748
243291 4.891834
239057 4.571363
241656 4.579442
240977 4.644715
241008 4.739053
255526 4.617224
257931 4.438777
257012 4.682752
251138 4.703861
251565 4.620473
256875 4.820580
239659 4.516136
242466 5.022058
244166 4.603836
241964 5.028560
228295 4.777028
224098 5.067747
229479 4.855140
234249 5.080227
244578 5.648285
256816 5.034281
244804 5.030434
240678 4.921041
239854 4.879955
239743 4.840965
240284 4.820238
253388 4.677457
254573 4.853772
250842 5.030775
245028 4.901774
233488 4.838135
236696 4.810584
236297 4.723644
252273 5.010890
252920 5.466656
251201 5.670652
237628 4.849653
232925 5.167281
230655 4.925711
240136 4.619335
252716 5.139529
254602 4.641356
248077 5.131379
244802 4.889343
239421 4.741268
235864 4.464777
237300 4.583542
248424 4.527479
249990 4.395524
232084 4.695336
218641 4.490047
222754 4.652566
235154 5.075482
244438 4.462584
262524 4.506913
264968 4.387850
242366 5.019483
240146 4.724235
236292 4.526909
229201 4.575609
242761 4.904999
257763 4.729100
259118 4.505885
248841 4.831024
240614 4.529327
230445 4.771021
232315 4.536143
230687 4.478429
247701 4.480123
245365 4.186084
244704 4.406776
227411 4.096910
228497 4.137604
229427 4.203292
228555 4.247168
241367 4.244598
我认为你的公式需要更多的自由度来拟合。
我做了这个小调整,得到了更好的结果。
ggplot(data, aes(x, y)) +
geom_point() +
stat_smooth(method = "nls",
formula = "y ~ (a/x) + b*x + c",
method.args = list(algorithm = "port", start = c(a = 1, b=1, c=1)),
se = FALSE) +
labs(x = "x", y = "y") +
scale_y_continuous(labels = scales::comma)
Obs:这不仅仅是微调。我注意到 y 随着 x 的增加而不断增加,这在纯反比例下是不可能的,因此它表明关系中存在与 x 成比例的其他一些因素。此外,通过添加一个自由常数 c,我为 y 提供了一个基于偏移量移动的自由度。看起来它仍然不完全适合,所以这个关系还有别的东西。您可以对公式做更多的最小调整以查看最合适的或使用其他一些包来尝试找出关系是什么,然后设计相应的公式。
我在我的数据中拟合了一条非线性曲线。我使用的代码是:
ggplot(data, aes(x, y)) +
geom_point() +
stat_smooth(method = "nls",
formula = "y ~ (a/x)",
method.args = list(algorithm = "port", start = c(a = 1)),
se = FALSE) +
labs(x = "x", y = "y") +
scale_y_continuous(labels = comma)
我使用公式 y ~ (a/x)
作为开始 a=1
,因为我假设我的数据遵循此规则。但是看到图表,我可以看出这不是最合适的。我可以做些什么来拟合更好的 a/x
曲线吗?就像第一次观察的曲线更陡峭,其余的曲线更平坦?
这是我的数据集:
y x
318088 2.818808
336211 2.874786
401643 2.732688
435816 2.580749
378424 2.862525
379680 2.762701
354782 2.839747
409505 2.712271
382991 2.783217
484853 2.865189
481788 2.613035
387994 3.013499
375327 2.902625
399366 2.822512
457983 2.778006
530702 2.853720
554820 2.710258
547952 2.676447
525960 2.734852
509157 2.709694
482776 2.582611
478888 2.894800
343332 3.128868
318626 3.310392
279998 2.820940
232878 3.219122
342657 2.880345
454904 2.756803
597070 2.740670
629832 2.739997
645339 2.456461
601518 2.533754
593473 3.063990
596382 2.701835
562774 2.805480
404588 2.915558
347486 2.934605
434991 2.621134
426929 2.532104
348084 2.899483
348785 2.789879
334617 2.939476
322318 2.843211
310848 2.817476
270500 2.683987
224446 2.554492
213684 2.809247
282404 2.640408
336469 2.941160
366556 2.869811
358514 3.052722
404425 2.798377
411394 2.584497
370665 2.763246
375990 2.769727
363207 2.584378
325418 2.717353
307332 2.880379
403078 2.567024
386521 2.573119
329488 2.829060
297818 2.879921
313291 2.922753
309145 2.966452
302217 2.845400
336894 2.578288
335552 2.653786
303367 2.622335
342057 2.745391
350693 3.423275
326369 2.652801
322293 2.699542
395416 2.539014
393757 2.452486
340809 2.552500
333703 2.677471
323882 2.802757
291526 2.994942
289024 2.868676
361007 2.613192
337997 2.465057
256454 2.533094
262345 2.616848
195605 2.657868
177227 2.817398
172935 2.483164
179142 2.343136
178935 2.720142
177627 2.543538
183297 2.710079
277263 2.569121
314612 2.921395
394498 2.701341
566969 2.715369
573897 2.695608
533915 2.871800
431106 2.804339
487278 2.889213
522717 5.382958
503169 2.889163
587002 2.592890
652177 2.667569
547244 2.927379
527966 2.855652
542794 2.767391
500434 2.810982
480563 3.211367
559648 2.734722
568892 2.644250
485245 2.663728
522124 2.662655
509209 2.703339
513912 2.678759
451048 2.931140
545108 2.724209
622589 2.934804
507540 2.910144
496703 2.897143
488153 2.541633
494480 2.608369
450904 2.730225
545874 2.555485
574458 2.572568
497318 2.629635
484248 2.752913
439854 2.789159
435549 2.791774
410635 2.804586
502417 2.683846
536582 2.649824
462139 2.661041
477259 2.717991
498415 2.949599
464460 2.583202
402989 2.708579
482276 2.676654
543463 2.721056
483854 2.659228
472970 3.016031
467194 3.423359
458830 3.151692
388446 3.047256
443329 3.061273
475608 3.160686
436197 3.142662
422146 3.183787
421591 3.367449
417524 3.130931
397089 3.147788
469774 3.054959
537462 3.378351
457240 3.067574
421987 3.289652
452076 3.282907
463292 2.952140
411454 3.062896
469632 3.125029
499961 3.192651
470231 3.052396
426435 3.084369
413277 3.044155
332061 3.074529
295336 3.030897
354612 3.132999
373374 3.009976
336148 3.038227
328659 3.135106
320652 3.150984
325762 2.926446
320949 3.045080
350623 2.943999
288277 3.162889
227425 3.054258
185629 2.906675
246709 2.984109
276159 4.300290
262537 3.116466
293402 3.283875
297795 3.007910
249537 2.996318
267314 3.354558
278516 3.100201
270348 3.023718
261186 3.068398
275437 3.085977
282662 3.049299
268530 3.161894
250753 3.210491
238059 3.197553
231861 3.172572
236661 3.091866
274325 3.284630
281563 3.092375
251875 3.120738
233500 3.048110
234641 3.256575
235626 3.102921
240376 3.226411
278165 2.905918
294890 3.043176
262143 3.163785
243308 3.109935
248225 3.221056
254422 3.616995
255737 3.291750
262492 3.190569
242269 3.262991
218760 3.276030
206165 3.376757
199568 3.192706
199901 3.228047
203278 3.361286
221573 3.135710
236671 3.336578
224909 3.285750
212335 3.409516
209672 3.100753
206667 3.477523
212272 3.341284
244915 3.271667
254639 3.224289
209344 3.275723
191725 3.168092
197447 3.178668
192552 3.260977
205142 3.163146
246057 4.152538
256093 3.317521
222355 3.396396
220011 3.475071
209645 3.352367
210690 3.642851
211753 3.475454
245137 3.458855
225789 3.352159
224473 3.349224
219929 3.329815
272416 3.251371
299300 3.383322
296407 3.465826
350088 3.413542
363884 3.777645
317916 3.635040
307377 3.730956
303743 3.838431
299388 3.606044
299031 3.862217
352912 4.120376
363647 3.423283
322933 3.572628
337366 3.925978
308285 3.980198
304655 4.005487
283874 3.582592
336132 4.145034
336230 3.954005
275528 3.889334
271113 3.935482
276283 3.887819
274942 3.900225
266933 3.762177
278204 3.590627
291083 3.475412
250593 3.571164
182623 3.696895
177937 3.702510
179893 3.506154
180820 3.483347
189747 3.411142
177318 3.448681
183941 3.581280
177965 3.831371
171082 3.918870
173392 3.655046
213628 3.558670
265686 3.441305
274562 3.308130
243729 3.527024
269196 3.570593
282492 3.460696
268099 3.621155
270395 3.500871
354696 3.342175
323164 3.579816
219246 3.430618
222832 3.512364
215442 3.692077
217057 3.523960
204375 3.587310
227147 3.513631
233728 3.396191
230588 3.417746
241174 3.742648
233384 3.512578
230285 3.605550
233691 3.524610
261651 3.424540
259537 3.422988
249027 3.547568
236783 3.455910
236808 3.545529
240779 3.794796
232181 3.352256
260543 3.409897
255879 3.561510
233716 3.355982
217540 3.366562
236359 3.746781
201513 3.798635
227904 3.893903
281891 3.640823
279886 3.937271
266120 3.708926
248266 3.809452
239477 3.990469
247679 3.872256
250121 4.027336
283313 3.621488
282773 3.089742
245995 3.027371
237379 3.031593
231324 3.222918
230889 2.932897
238313 3.001761
277913 2.828859
281245 3.162213
258537 3.186444
255604 2.923421
259266 2.835952
261345 2.876915
260054 2.986111
292471 2.895799
324378 3.008379
270807 3.064305
276748 3.107425
274135 3.074070
258643 2.993347
249392 3.036252
275747 3.113728
272187 2.948600
249579 3.348998
243490 3.175328
244576 3.049288
244391 3.183174
234976 3.228730
262768 3.051605
266822 3.066944
256679 3.192835
252421 3.247266
246730 3.175772
243815 3.101510
229785 3.324920
252492 3.317948
272911 3.173554
247775 3.202868
255683 3.066175
230019 3.140274
229951 3.137172
230863 3.183656
254420 2.985195
263055 2.923506
243070 3.154108
246389 3.103570
246786 3.098457
241969 3.066267
239544 3.208468
273433 2.993599
265125 3.071725
250987 3.108260
259616 3.005487
238194 3.169553
240198 3.029928
238518 3.104733
263671 3.347683
264454 3.201232
255704 3.241008
241042 3.222571
232599 3.487346
232822 3.635051
235612 3.725143
258297 3.471994
259056 3.488392
237961 3.432317
229838 4.296460
226430 4.835155
226270 4.862763
238614 4.511147
268627 4.859204
264323 5.455362
238190 5.473459
248697 4.874474
250119 4.672262
245319 4.909624
239323 4.623515
265408 5.296186
270663 4.318392
249145 4.801727
240939 5.209521
239549 5.151567
231575 4.808046
238310 4.212207
254595 3.804590
250701 3.735263
244254 4.168639
234574 5.147149
242680 4.777019
230793 5.157367
239942 4.592258
258330 4.476864
270651 4.724341
243033 4.927935
236412 4.882507
237512 4.766240
237161 5.025718
236633 5.197085
257124 4.862109
253096 4.850248
242846 5.257920
245173 5.158687
244075 4.749932
239519 4.639774
242199 4.919499
261240 4.591937
256211 4.584748
243291 4.891834
239057 4.571363
241656 4.579442
240977 4.644715
241008 4.739053
255526 4.617224
257931 4.438777
257012 4.682752
251138 4.703861
251565 4.620473
256875 4.820580
239659 4.516136
242466 5.022058
244166 4.603836
241964 5.028560
228295 4.777028
224098 5.067747
229479 4.855140
234249 5.080227
244578 5.648285
256816 5.034281
244804 5.030434
240678 4.921041
239854 4.879955
239743 4.840965
240284 4.820238
253388 4.677457
254573 4.853772
250842 5.030775
245028 4.901774
233488 4.838135
236696 4.810584
236297 4.723644
252273 5.010890
252920 5.466656
251201 5.670652
237628 4.849653
232925 5.167281
230655 4.925711
240136 4.619335
252716 5.139529
254602 4.641356
248077 5.131379
244802 4.889343
239421 4.741268
235864 4.464777
237300 4.583542
248424 4.527479
249990 4.395524
232084 4.695336
218641 4.490047
222754 4.652566
235154 5.075482
244438 4.462584
262524 4.506913
264968 4.387850
242366 5.019483
240146 4.724235
236292 4.526909
229201 4.575609
242761 4.904999
257763 4.729100
259118 4.505885
248841 4.831024
240614 4.529327
230445 4.771021
232315 4.536143
230687 4.478429
247701 4.480123
245365 4.186084
244704 4.406776
227411 4.096910
228497 4.137604
229427 4.203292
228555 4.247168
241367 4.244598
我认为你的公式需要更多的自由度来拟合。
我做了这个小调整,得到了更好的结果。
ggplot(data, aes(x, y)) +
geom_point() +
stat_smooth(method = "nls",
formula = "y ~ (a/x) + b*x + c",
method.args = list(algorithm = "port", start = c(a = 1, b=1, c=1)),
se = FALSE) +
labs(x = "x", y = "y") +
scale_y_continuous(labels = scales::comma)
Obs:这不仅仅是微调。我注意到 y 随着 x 的增加而不断增加,这在纯反比例下是不可能的,因此它表明关系中存在与 x 成比例的其他一些因素。此外,通过添加一个自由常数 c,我为 y 提供了一个基于偏移量移动的自由度。看起来它仍然不完全适合,所以这个关系还有别的东西。您可以对公式做更多的最小调整以查看最合适的或使用其他一些包来尝试找出关系是什么,然后设计相应的公式。