Reputation: 329
I'm working on a exponential decay model where I would like to estimate the decay rate. My current model uses a self-starting function, SSasymp
from the stats
package. I've also written a second model, where I just eyeball the starting parameters, which requires minpack.lm
package. My question is, is there another way I can estimate the starting parameters to cross check the SSasymp
function. I (think) I understand what the code is doing to estimate the starting parameters, but I wanted to get some feedback on wether the SSasymp
is the right function to use with this data or if there is another function I could potentially use.
library(stats)
library(minpack.lm)
library(broom)
library(ggplot2)
df<-data.frame(Date=seq(1:66),
Level=c(1438072839.75, 1397678053.5, 1358947420.5, 1313619938.25, 1269224528.25,
1246776954.75, 1207201162.5, 1176229091.25, 1136063160, 1103721704.25, 1080591637.5,
1048286667, 1017840460.5, 1001057052, 975815001, 943568665.5, 932026210.5, 916996593.75,
903904288.5, 887578544.25, 871428547.5, 855417720, 843504839.25, 825835607.25,
816060303.75, 803506361.25, 801213123, 797977217.25, 793483994.25, 780060123, 766265609.25,
756172471.5, 746615497.5, 738002936.25, 723741644.25, 711969181.5, 696032998.5,
686162453.25, 671953166.25, 674184571.5, 664739475, 641091932.25, 627358484.25,
616740068.25, 602261552.25, 592440797.25, 584160403.5, 569780103.75, 556305753,
551682927, 546535062, 537782506.5, 524251944.75, 519277188.75, 503598795, 498481312.5,
487907885.25, 479760227.25, 474773064.75, 468246932.25, 460561701, 455266345.5,
448451890.5, 447760119, 441236056.5, 438884417.25))
dfDecay<-nls(Level~ SSasymp(Date, Asym, R0, lrc), data = df)
dfFitted<-augment(dfDecay)
ggplot(df, aes(x=Date,y=Level))+geom_point()+ geom_line( aes(y=dfFitted$.fitted), color="red")
dfDecay2<-nlsLM(Level~b*exp(-a*Date),
data = df,
start= list(a=.01,b=1.5e+09),
algorithm = "LM")
fitDecay2<-augment(dfDecay2)
ggplot(df, aes(x=Date,y=Level))+geom_point()+ geom_line( aes(y=fitDecay2$.fitted), color="red")
Upvotes: 1
Views: 1049
Reputation: 270010
Regarding starting values:
Level/1e9
in place of Level
. This just changes the units in which Level is measured.nls
should be sufficient.This gives:
fm0 <- lm(log(Level/1e9) ~ Date, df)
st <- list(a = exp(coef(fm0)[[1]]), b = -coef(fm0)[[2]])
nls(Level/1e9 ~ a * exp(-b * Date ), df, start = st)
giving:
Nonlinear regression model
model: Level/1e+09 ~ a * exp(-b * Date)
data: df
a b
1.3532 0.0183
residual sum-of-squares: 0.08055
Number of iterations to convergence: 4
Achieved convergence tolerance: 4.023e-07
Upvotes: 1