For y = B0 + B1x, I can write it as lm(y ~ x)
. However I am not sure how to write y = B0eB1x into a model function in R.
I have tried lm(log(y) ~ x)
, lm(y ~ exp(x))
, lm(y ~ log(x))
, and lm(log(y) ~ log(x))
, but I am not sure which is correct. I get different results for each model.
The two ways that you can do this that are actually faithful to the original statistical model (Gaussian errors with constant variance) are:
glm(y ~ x, family = gaussian(link = "log"), data = ...)
(but you'll have to exponentiate the intercept parameter) or
nls(y ~ b0*exp(b1*x), start = ..., data = ...)
(but you'll have to provide starting values in the form list(b0 = 1, b1 = 1)
(for some sensible values).
y = b0*exp(b1*x)
implies log(y) = log(b0) + b1*x
, but transforming the response variable in this way will change the statistical model ... so lm(log(y) ~ x, data = ...)
will give you similar but not identical answers to the preceding two recipes.