Solved – How to perform an exponential regression with multiple variables in R

multiple regressionnonlinear regressionrregression

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.

Best Answer

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)