Reputation: 6771
Apologies for the noobish question, my R seems to have gone very rusty since last time I was here. I have several series of ratio data taken over time, like so:
years <- c(1890, 1891, 1894, 1896, 1899, 1905, 1917)
ratio <- c(0.8, 0.9, 0.5, 0.25, 0.1, 0.02, 0)
plot(ratio~years)
Superimposed on the scatterplot I need a smoothish line that is respectful of the 0 to 1 limits of ratio data. Straight-up lines(predict(loess(ratio~years))~years)
is a little too jerky, and a few methods I tried of smoothing it dip below zero sometimes with my real data. What function do I need here?
Upvotes: 1
Views: 490
Reputation: 179468
It seems like you your model is exponential decay. You can estimate this with non-linear least squares regression, using nls()
:
fit <- nls(ratio ~ exp(-b*(years-1890)), start=list(b=0.5))
fit
Nonlinear regression model
model: ratio ~ exp(-b * (years - 1890))
data: parent.frame()
b
0.2051
residual sum-of-squares: 0.05669
Number of iterations to convergence: 5
Achieved convergence tolerance: 5.016e-06
Now plot the results:
plot(ratio~years, ylim=c(0,1))
newdata <- data.frame(years=1890:1917)
lines(newdata$years, predict(fit, newdata=newdata), col="red")
Upvotes: 4