Transforming both variables doesn't linearize the relationship:
plot(log(y)~log(x))
Instead you should only transform y
:
plot(log(y)~x)
modlm <- lm(log(y)~x)
abline(modlm)
summary(modlm)
Call:
lm(formula = log(y) ~ x)
Residuals:
Min 1Q Median 3Q Max
-0.0081825 -0.0009194 0.0000952 0.0008455 0.0070058
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 6.927e+00 4.567e-04 15166.4 <2e-16 ***
x -1.227e-04 3.516e-07 -349.1 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.002185 on 64 degrees of freedom
Multiple R-squared: 0.9995, Adjusted R-squared: 0.9995
F-statistic: 1.219e+05 on 1 and 64 DF, p-value: < 2.2e-16
Of course you can use nls
too:
modnls <- nls(y~exp(a+b*x), start=list(a=coef(modlm)[[1]], b=coef(modlm)[[2]]))
plot(y~x)
xnew <- seq(min(x), max(x), by=0.5)
lines(xnew, exp(coef(modnls)[[1]]+xnew*coef(modnls)[[2]]))
summary(modnls)
Formula: y ~ exp(a + b * x)
Parameters:
Estimate Std. Error t value Pr(>|t|)
a 6.926e+00 4.384e-04 15797.5 <2e-16 ***
b -1.225e-04 3.831e-07 -319.7 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.904 on 64 degrees of freedom
Number of iterations to convergence: 2
Achieved convergence tolerance: 6.25e-08
Note how the parameter estimates of the linear and the nonlinear fit are very similar. Usually, you should choose whether to use lm
with transformed data or nls
with untransformed data depending on the error distribution. However, since the relationship between y
and x
is not very nonlinear, it doesn't matter so much here.