Reputation: 23
I have two vectors X and y2, and I wish to fit an exponential curve to the data.
I tried many approaches described on Stack Overflow topics but all of them give me just a straight line. e.g. I tried this:
model.three <- lm(log(y2) ~ log(X))
plot(X,predict(model.three))
abline(model.three)
My data:
X <- seq(1:50)
Y <- rnorm(50,mean=0,sd=1)
y2 <- exp(X)
y2 <- Y+y2
Upvotes: 2
Views: 508
Reputation: 71
Your data expresses an exponential relationship between Y and X, which is Y = exp(X) + eps
where eps
is some noise.
Therefore, I would suggest fitting a model between log(Y)
and X
, to capture the linear relationship between the two:
model.three <- lm(log(y2) ~ X)
summary(model.three)
The summary confirms that the relationship captured is as expected (i.e. the coefficient for X is very close to 1).
Since plotting the data on a linear scale will not be useful, I think it is a good idea to plot the fitted straight line with abline
.
Note: to be exact, it would be more accurate to capture the relationship between y2 and exp(X), but with your data, the fit is essentially perfect.
Upvotes: 1
Reputation: 2353
Is this what you are looking for?
model.three <- lm(log(y2) ~ log(X))
plot(X,predict(model.three))
## Instead of abline(), use this:
lines(model.three$fitted.values)
Upvotes: 1