# Fitting an exponential model to data

I have 2 variables, both from class "numeric":

> head(y) [1] 0.4651804 0.6185849 0.3766175 0.5489810 0.3695258 0.4002567
> head(x) [1] 59.32820 68.46436 80.76974 132.90824 216.75995 153.25551

I plotted them, and now I would like to fit an exponential model to the data (and add it to the plot) but I cannot find any info on fitting models to multivariate data in R! Only to univariate data, can somebody help? I don't even know where to start... Thanks!

-
This is a bit confusing. You say you have two "independent" variables (I prefer "predictor", but that's not important). Do you have any "dependent"/"response" variables? If these were both response variables, I can imagine fitting a parametric, bivariate probability distribution (with or without predictor variables on which the distribution's parameters depended) -- or a 2D kernel density estimate. Perhaps you could explain the context a bit more. (PS whoever's upvoting the question must know what it means ... anyone care to chime in?) –  Ben Bolker Jun 15 '11 at 12:09
In any case, you'd better go to crossvalidated.com for such questions. or to an obscure website called Google. It did find information on fitting models to multivariate data. Quite a lot (4 million two hundred and thirty thousand to be exact) –  Joris Meys Jun 15 '11 at 12:27
I'd recommend bing - it is a decision engine after all, search engines are so 20th century...just look at Yahoo and Ask Jeeves, how irrelevant are they today?!? –  Chase Jun 15 '11 at 13:55
@Ben Bolker - Thanks for helping, I have taken the independent out, cause it was not correct. What I have is distance between locations (x) and correlations of rainfall between locations (y) –  sbg Jun 15 '11 at 14:08
note that you will have to use special methods if you want to make statistical inferences on these data, because if the distances were computed on a common set of locations, they are not independent -- search e.g. for "Mantel test" –  Ben Bolker Jun 15 '11 at 14:21

## migrated from stackoverflow.comJun 15 '11 at 15:09

This question came from our site for professional and enthusiast programmers.

I am not completely sure what you're asking, because your lingo is off. But assuming that your variables aren't independent of one another (if they were, then they're be no relation to find) I'll give it a try. If x is your independent (or predictor) variable and y is your dependent (or response) variable, then this should work.

# generate data
beta <- 0.05
n <- 100
temp <- data.frame(y = exp(beta * seq(n)) + rnorm(n), x = seq(n))

# plot data
plot(temp$x, temp$y)

# fit non-linear model
mod <- nls(y ~ exp(a + b * x), data = temp, start = list(a = 0, b = 0))

lines(temp$x, predict(mod, list(x = temp$x)))

@sbg -- No, sorry, I can't think of a reason why. Does nls() fit a model? –  Richard Herron Jun 15 '11 at 13:51
@sbg try sorting your x variable: lines(sort(temp$x),predict(mod, list(x=sort(temp$x))) –  Ben Bolker Jun 15 '11 at 14:16