如何估算R中散点图的最佳拟合函数? [英] How to estimate the best fitting function to a scatter plot in R?
问题描述
我有两个变量的散点图,例如:
I have scatterplot of two variables, for instance this:
x<-c(0.108,0.111,0.113,0.116,0.118,0.121,0.123,0.126,0.128,0.131,0.133,0.136)
y<-c(-6.908,-6.620,-5.681,-5.165,-4.690,-4.646,-3.979,-3.755,-3.564,-3.558,-3.272,-3.073)
我想找到一个更适合这两个变量之间关系的函数.
and I would like to find the function that better fits the relation between these two variables.
准确地说,我想比较三种模型的拟合度:linear
,exponential
和logarithmic
.
to be precise I would like to compare the fitting of three models: linear
, exponential
and logarithmic
.
我正在考虑使每个函数适合我的值,计算每种情况下的可能性并比较AIC值.
I was thinking about fitting each function to my values, calculate the likelihoods in each case and compare the AIC values.
但是我真的不知道如何或从哪里开始.任何对此的可能帮助将不胜感激.
But I don't really know how or where to start. Any possible help about this would be extremely appreciated.
非常感谢您.
蒂娜.
推荐答案
这里是比较五个模型的示例.由于前两个模型的形式,我们能够使用lm
获得良好的初始值. (请注意,不应比较使用y
的不同转换的模型,因此我们不应将lm1
和lm2
用作比较模型,而只能用于起始值.)现在,对前两个分别运行nls
.在这两个模型之后,我们尝试在x
中使用不同程度的多项式.幸运的是,lm
和nls
使用一致的AIC
定义(尽管不一定如此,其他R模型拟合函数具有一致的AIC
定义),因此我们可以将lm
用于多项式.最后,我们绘制了前两个模型的数据和拟合度.
Here is an example of comparing five models. Due to the form of the first two models we are able to use lm
to get good starting values. (Note that models using different transforms of y
should not be compared so we should not use lm1
and lm2
as comparison models but only for starting values.) Now run an nls
for each of the first two. After these two models we try polynomials of various degrees in x
. Fortunately lm
and nls
use consistent AIC
definitions (although its not necessarily true that other R model fitting functions have consistent AIC
definitions) so we can just use lm
for the polynomials. Finally we plot the data and fits of the first two models.
AIC越低越好,因此nls1
最好是lm3.2
,然后是nls2
.
The lower the AIC the better so nls1
is best followed by lm3.2
following by nls2
.
lm1 <- lm(1/y ~ x)
nls1 <- nls(y ~ 1/(a + b*x), start = setNames(coef(lm1), c("a", "b")))
AIC(nls1) # -2.390924
lm2 <- lm(1/y ~ log(x))
nls2 <- nls(y ~ 1/(a + b*log(x)), start = setNames(coef(lm2), c("a", "b")))
AIC(nls2) # -1.29101
lm3.1 <- lm(y ~ x)
AIC(lm3.1) # 13.43161
lm3.2 <- lm(y ~ poly(x, 2))
AIC(lm3.2) # -1.525982
lm3.3 <- lm(y ~ poly(x, 3))
AIC(lm3.3) # 0.1498972
plot(y ~ x)
lines(fitted(nls1) ~ x, lty = 1) # solid line
lines(fitted(nls2) ~ x, lty = 2) # dashed line
添加了更多模型,随后对其进行了修复并更改了表示法.另外,为了跟上Ben Bolker的评论,我们可以使用AICcmodavg软件包中的AICc
替换上面的所有AIC
.
ADDED a few more models and subsequently fixed them up and changed notation. Also to follow up on Ben Bolker's comment we can replace AIC
everywhere above with AICc
from the AICcmodavg package.
这篇关于如何估算R中散点图的最佳拟合函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!