I'd like to perform an exponential regression with multiple independent variables (similar to the LOGEST function in Excel)

I'm trying to model the function $Y = b {m_1}^{x_1}{m_2}^{x_2}$ where $b$ is a constant, $x_1$ and $x_2$ are my independent variables, and $m_1$ and $m_2$ are the coefficients of the independent variables.

I think I can linearize the function by doing something like glm(log(Y) ~ x1 + x2) but I don't totally understand why that would work. Also, I'd like to run a true non-linear regression if there is such a thing.

My goal is to run both a linear and an exponential regression, and find the best fit line based on the higher $R^2$ value.

I would also really appreciate your help in understanding how to plot the predicted curve in a scatter plot of my data as well.

  • 1
    $\begingroup$ the question of choosing lm(log(Y) ~ x1 + x2) versus the original exponential model is down to how you think the error behaves. If, in your data, you see that the noise increases as x1 and x2 increase, then you have good reason to use the log model, but if you see that it's constant, then the latter is more appropriate - my argument depends upon the lm function assuming constant variance of noise - i would have a look at stats.stackexchange.com/questions/47063/… $\endgroup$
    – queenbee
    Apr 1, 2014 at 11:49
  • $\begingroup$ The correct glm formulation will work; it will perform well; and it will make other facilities available including hypothesis testing, prediction, and confidence intervals. Use glm(y ~ x1 + x2, gaussian(link="log") . If the software complains about being unable to finding a starting value for iteration, often just throwing a linear trend at it works, as in glm(y ~ x1 + x2, gaussian(link="log"), mustart=1:length(y)) . $\endgroup$
    – whuber
    Aug 16, 2018 at 12:56

3 Answers 3


As a start:

f <- function(x1,x2,a,b1,b2) {a * (b1^x1) * (b2^x2) }

# generate some data
x1 <- 1:10
x2 <- c(2,3,5,4,6,7,8,10,9,11)
y <- 2*exp(x1/4) + rnorm(10)*2
dat <- data.frame(x1,x2, y)

# fit a nonlinear model
fm <- nls(y ~ f(x1,x2,a,b1,b2), data = dat, start = c(a=1, b1=1,b2=1))

# get estimates of a, b
co <- coef(fm)
  • $\begingroup$ Thank you! I've also posted the technique I used to plot a curve for this model below: $\endgroup$
    – TheRyBerg
    Mar 5, 2013 at 16:02

Huub Hoofs' approach above worked! Thank you. Here is the technique I utilized to plot a visualization of the model:

# x1 is the variable we want to show on the x-axis
plot(x1, y)

# generate a range of values for x1 in small increments to create a smooth line
xRange <- seq(min(x1), max(x1), length.out = 1000)

# generate the predicted y values (for a test value of x2 = 1)
yValues <- predict(fm, newdata=list(x1=xRange, x2=1))

#draw the curve
lines(xRange, yValues, col="blue")

# generate the predicted y values (for a test value of x2 = 0)
yValues <- predict(fm, newdata=list(x1=xRange, x2=0))

#draw the curve
lines(xRange, yValues, col="red")

If you really want to look at R2, it's best to linearize your model.

Observe that R2 doesn't make sense for non linear general models, as discussed in another topic: https://stackoverflow.com/questions/14530770/calculating-r2-for-a-nonlinear-least-squares-fit

Note that the r squared is not defined for non-linear models, or at least very tricky, quote from R-help:

There is a good reason that an nls model fit in R does not provide r-squared - r-squared doesn't make sense for a general nls model.

One way of thinking of r-squared is as a comparison of the residual sum of squares for the fitted model to the residual sum of squares for a trivial model that consists of a constant only. You cannot guarantee that this is a comparison of nested models when dealing with an nls model. If the models aren't nested this comparison is not terribly meaningful.

So the answer is that you probably don't want to do this in the first place.

If you want peer-reviewed evidence, see this article for example; it's not that you can't compute the R^2 value, it's just that it may not mean the same thing/have the same desirable properties as in the linear-model case.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.