简体   繁体   English

指数拟合在对数标度R上

[英]Exponential fit on logarithmic scale R

This is my code: 这是我的代码:

a<-c(0.83, 1.67, 2.5, 3.33,6.39)
b<-c(34252553.89, 34430947.5, 36494798.86, 66156794.56, 248698700.1)
plot(a,b)
plot(a,b, log='y')

Now I want to add exponential fit to my plot (should look linear with logarithmic scale) 现在我想为我的情节添加指数拟合(应该与对数刻度呈线性关系)

How can I do this? 我怎样才能做到这一点?

You can use lm : 你可以使用lm

dat <- as.data.frame(cbind(a,b))

plot the data: 绘制数据:

plot(log(b)~a, data=dat)

Fit linear model: 适合线性模型:

fit <- lm(log(b) ~ a, data=dat)
summary(fit)

Call:
lm(formula = log(b) ~ a, data = dat)

Residuals:
       1        2        3        4        5 
 0.27207 -0.04616 -0.30751 -0.03222  0.11383 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 16.75764    0.20360   82.31 3.95e-06 ***
a            0.38502    0.05798    6.64  0.00696 ** 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 0.2481 on 3 degrees of freedom
Multiple R-squared: 0.9363,     Adjusted R-squared: 0.9151 
F-statistic: 44.09 on 1 and 3 DF,  p-value: 0.006959 

Predict values and plot them: 预测值并绘制它们:

lines(predict(fit)~dat$a)

在此输入图像描述

Does this help you? 这对你有帮助吗?

With basic plot, You need to fit your data externally and add it. 使用基本绘图,您需要在外部调整数据并添加它。 Using ggplot2 it is easier to do this. 使用ggplot2更容易做到这一点。 For example: 例如:

dat <- data.frame(x=a,y=b)
library(ggplot2)
ggplot(dat,aes(x=x,y=y))+
  geom_point(size=5) +
  stat_smooth(method='glm')+
  scale_y_log10()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM