21
$\begingroup$

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!

$\endgroup$

migrated from stackoverflow.com Jun 15 '11 at 15:09

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

  • 6
    $\begingroup$ 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?) $\endgroup$ – Ben Bolker Jun 15 '11 at 12:09
  • $\begingroup$ 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) $\endgroup$ – Joris Meys Jun 15 '11 at 12:27
  • $\begingroup$ 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?!? $\endgroup$ – Chase Jun 15 '11 at 13:55
  • $\begingroup$ @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) $\endgroup$ – sbg Jun 15 '11 at 14:08
  • $\begingroup$ 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" $\endgroup$ – Ben Bolker Jun 15 '11 at 14:21
20
$\begingroup$

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))

# add fitted curve
lines(temp$x, predict(mod, list(x = temp$x)))
$\endgroup$
  • $\begingroup$ thanks for your answer, I have taken the word "independent" out, as you pointed out, it didn't make sense. Using your code for my data I can fit the model but the result is dozens of lines in the graph instead of just one. Any idea why? $\endgroup$ – sbg Jun 15 '11 at 13:44
  • $\begingroup$ @sbg -- No, sorry, I can't think of a reason why. Does nls() fit a model? $\endgroup$ – Richard Herron Jun 15 '11 at 13:51
  • $\begingroup$ I think so, I get: Nonlinear regression model model: y ~ exp(a + b * x) data: DF a b -0.535834 -0.002024 residual sum-of-squares: 18.62 Number of iterations to convergence: 6 Achieved convergence tolerance: 8.08e-06 $\endgroup$ – sbg Jun 15 '11 at 14:03
  • 3
    $\begingroup$ @sbg try sorting your x variable: lines(sort(temp$x),predict(mod, list(x=sort(temp$x))) $\endgroup$ – Ben Bolker Jun 15 '11 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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