[R] Using nls.lm to fit a non-continuous dates range

Ahmed Attia ahmedatia80 at gmail.com
Tue Jul 25 19:18:29 CEST 2017


Dear  R users,

Can I fit nls.lm to a non-continuous date data. looked at previous
examples but still not able to fit the model to my data. There are 25
rows of observations as below;

df <- data.frame(Date=as.Date(rownames(df),'%m/%d/%Y'),Y=df$height)
df$days <- as.numeric(df$Date - df[1,]$Date)
head(df)
 Date         Y         days
1 2009-12-01 0.2631250    0
2 2010-01-08 0.4436012   38
3 2010-02-04 0.7151786   65
4 2010-03-03 1.1379762   92
5 2010-04-05 1.7986866  125
6 2010-05-04 2.2982635  154

The following code, tried different values of the list, did not work.

f <- function(parS, xx) {parS$a + parS$b*log(parS$tc - xx)}
     resids <- function(p, observed, xx) {df$Y - f(p,xx)}
     nls.out <- nls.lm(par=list(a=1,b=-0.001,tc=25), fn = resids,
              observed = df$Y, xx = df$days)

and the following error is always produced.

Warning messages:
1: In log(parS$c - xx) : NaNs produced
2: In log(parS$c - xx) : NaNs produced
3: In log(parS$c - xx) : NaNs produced
4: In log(parS$c - xx) : NaNs produced
5: In log(parS$c - xx) : NaNs produced

then the nls.out should integrated in nls.final as proposed by jlhoward

# use output of L-M algorithm as starting estimates in nls(...)
par <- nls.out$par
nls.final <- nls(Y~a+b*log(tc-days),data=df,
                 start=c(a=par$a, b=par$b, tc=par$tc))

summary(nls.final)      # display statistics of the fit
# append fitted values to df
df$pred <- predict(nls.final)

Also attempting to fit a selfstart model

a_start<-8
b_start<-2*log(2)/a_start
m<-nls(df$Y~a*exp(-b*df$days),start=list(a=a_start,b=b_start))
Error in nls(df$Y ~ a * exp(-b * df$days), start = list(a = a_start, b
= b_start)) :
  parameters without starting value in 'data': Y, days

Could you please help

Thank you

Ahmed Attia, Ph.D.
Agronomist & Soil Scientist



More information about the R-help mailing list