load(file='../../BookCode/nlrwr/data/ScotsPine.rda') # Plot the data to get an idea of what it looks like: # plot(ScotsPine$age, ScotsPine$lai, xlab='age', ylab='lai') grid() # Guess at some initial values: # a0 = min(ScotsPine$lai)* 0.95 y = log(ScotsPine$lai - a0) x = ScotsPine$age lm_fit = lm(y ~ x) cs = as.double(coefficients(lm_fit)) b0 = exp(cs[1]) c0 = -1/cs[2] print(c( b0, c0 )) fit = nls(lai ~ a + b * exp(-age/c), data=ScotsPine, start=c(a=a0, b=b0, c=c0), trace=FALSE) # Plot the curve and the fit: # ageVals = with(ScotsPine, seq(min(age), max(age), length.out=100)) lai_predicted = predict(fit, newdata=data.frame(age=ageVals)) plot(ScotsPine$age, ScotsPine$lai, xlab='age', ylab='lai') lines(ageVals, lai_predicted, col='blue') grid() print(summary(fit)) #print(confint(fit)) # this does not seem to converge