# How to perform an exponential regression with multiple variables in R

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.

• 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/… – queenbee Apr 1 '14 at 11:49
• 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)) . – whuber Aug 16 '18 at 12:56

## 2 Answers

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)
set.seed(44)
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)

• Thank you! I've also posted the technique I used to plot a curve for this model below: – TheRyBerg Mar 5 '13 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")