在R中拟合对数曲线 [英] Fitting logarithmic curve in R

查看:152
本文介绍了在R中拟合对数曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在R中有一组线性的点,可以执行以下操作以绘制这些点,在它们上拟合一条线,然后显示该线:

If I have a set of points in R that are linear I can do the following to plot the points, fit a line to them, then display the line:

x=c(61,610,1037,2074,3050,4087,5002,6100,7015)
y=c(0.401244, 0.844381, 1.18922, 1.93864, 2.76673, 3.52449, 4.21855, 5.04368, 5.80071)

plot(x,y)    
Estimate = lm(y ~ x)    
abline(Estimate)

现在,如果我有一组看起来像对数曲线拟合的点更合适,例如:

Now, if I have a set of points that looks like a logarithmic curve fit is more appropriate such as the following:

x=c(61,610,1037,2074,3050,4087,5002,6100,7015)        
y=c(0.974206,1.16716,1.19879,1.28192,1.30739,1.32019,1.35494,1.36941,1.37505)

我知道我可以使用以下方法对x值的对数进行标准回归拟合:

I know I can get the standard regression fit against the log of the x values with the following:

logEstimate = lm(y ~ log(x))

但是,如何将logEstimate转换回正常比例,并根据之前的线性曲线绘制曲线呢?

But then how do I transform that logEstimate back to normal scaling and plot the curve against my linear curve from earlier?

推荐答案

嗯,我不太确定您是指将曲线与之前的线性曲线相对应".

Hmmm, I'm not quite sure what you mean by "plot the curve against my linear curve from earlier".

d <- data.frame(x,y)  ## need to use data in a data.frame for predict()
logEstimate <- lm(y~log(x),data=d)

这里有三种获取预测值的方法:

Here are three ways to get predicted values:

(1)使用预测:

plot(x,y)
xvec <- seq(0,7000,length=101)
logpred <- predict(logEstimate,newdata=data.frame(x=xvec))
lines(xvec,logpred)

(2)提取数字系数值:

(2) Extract the numeric coefficient values:

coef(logEstimate)
## (Intercept)      log(x) 
##  0.6183839   0.0856404 
curve(0.61838+0.08564*log(x),add=TRUE,col=2)

(3)使用 with()魔术(您需要在参数估计名称周围加上反引号,因为它们包含括号)

(3) Use with() magic (you need back-quotes around the parameter estimate names because they contain parentheses)

with(as.list(coef(logEstimate)),
      curve(`(Intercept)`+`log(x)`*log(x),add=TRUE,col=4))

也许您想要的是

est1 <- predict(lm(y~x,data=d),newdata=data.frame(x=xvec))
plot(est1,logpred)

...尽管我不确定为什么...

... although I'm not sure why ...

这篇关于在R中拟合对数曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆