Reputation: 1306
I'm trying to predict something in R, and I'm not sure what syntax I should use. I know that, for a polynomial, I can do:
predict(glm(Y ~ X +I(X^2) + I(X^3) +... I(X^n),data=mydata))
which I have been successful with, but I am wondering how to predict equations of the form
y = C(1-e^(-kx))
or
y = a/(1 + b*e^(-kx)), k>0
I'm not sure what example data I can give to illustrate this well...
Upvotes: 0
Views: 2270
Reputation: 3728
An example:
set.seed(1234)
# Parameters for simulated data
C<-1
k<-2
# Set x values and compute y for them
x<-seq(-100,120,1)/100
y<-C*(1-exp(-k*x))+rnorm(length(x),sd=0.1)
# Plot the points
plot(x,y); grid()
# Do the fit
fit<-nls(y ~ C*(1-exp(-k*x)), data=data.frame(y,x), start=list(C=5,k=5))
# Plot the fit
lines(x, predict(fit, list=(x=x)), col="red")
> fit
Nonlinear regression model
model: y ~ C * (1 - exp(-k * x))
data: data.frame(y, x)
C k
1.022 1.987
residual sum-of-squares: 2.147
Number of iterations to convergence: 8
Achieved convergence tolerance: 1.338e-07
Upvotes: 1