R version 2.9.0 (2009-04-17) Copyright (C) 2009 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # nllsqt1.r October 2007 > # > # test problem for nonlinear least squares > # from Fuller (1976) > # > both <- matrix(scan("fulnls.dat"),12,2, byrow=TRUE) > y <- both[,1] > x <- both[,2] > fulnls <- list(y,x) # need data frame or list > print(fulnls) # see list [[1]] [1] 47.3 87.0 120.1 130.4 58.8 111.9 136.5 132.0 68.8 138.1 145.7 143.0 [[2]] [1] 0.0 0.4 0.8 1.6 0.0 1.0 2.0 4.0 0.0 1.5 3.0 5.9 > # > # nonlinear least squares > # > # find better starting values > t1 <- y[ x==max(x)] # max value as asymptote > xs <- x[ x < 2 ] # use smallest x's > ys <- log( t1 - y[ x < 2 ] ) # transform reversed y's > # simple linear regression > t3 <- sum( (xs - mean(xs) )*ys )/ sum( (xs-mean(xs))**2 ) # slope > t2 <- mean(ys)-t3*mean(xs) # intercept > # transform back from log and reverse > t2 <- -exp(t2) > t3 <- - t3 > # done -- now have better starting values > c(t1,t2,t3) [1] 143.000000 -88.203926 1.475360 > # > # call nls using defaults + trace > that1 <- nls( y ~ th1 + th2*exp(-th3*x), data=fulnls, + start=list( th1=t1, th2=t2, th3=t3), trace=TRUE ) 630.1842 : 143.000000 -88.203926 1.475360 555.4333 : 141.701493 -83.987048 1.333616 555.1984 : 141.585853 -83.960237 1.349271 555.1962 : 141.608235 -83.973907 1.347612 555.1962 : 141.605989 -83.972601 1.347799 555.1962 : 141.606243 -83.972750 1.347778 > that1 Nonlinear regression model model: y ~ th1 + th2 * exp(-th3 * x) data: fulnls th1 th2 th3 141.606 -83.973 1.348 residual sum-of-squares: 555.2 Number of iterations to convergence: 5 Achieved convergence tolerance: 2.944e-06 > thfin <- coef(that1) > print("coefficients") [1] "coefficients" > print(thfin) th1 th2 th3 141.606243 -83.972750 1.347778 > # > # nonlinear least squares using nl2sol > that1 <- nls( y ~ th1 + th2*exp(-th3*x), data=fulnls, algorithm="port", + start=list( th1=t1, th2=t2, th3=t3), trace=TRUE ) 0: 315.09208: 143.000 -88.2039 1.47536 1: 288.81805: 142.296 -86.7454 1.40558 2: 277.61497: 141.668 -83.9996 1.34196 3: 277.59829: 141.598 -83.9679 1.34841 4: 277.59810: 141.607 -83.9732 1.34771 5: 277.59809: 141.606 -83.9727 1.34779 6: 277.59809: 141.606 -83.9727 1.34778 7: 277.59809: 141.606 -83.9727 1.34778 > that1 Nonlinear regression model model: y ~ th1 + th2 * exp(-th3 * x) data: fulnls th1 th2 th3 141.606 -83.973 1.348 residual sum-of-squares: 555.2 Algorithm "port", convergence message: relative convergence (4) > # > # cov matrix of coefficients > print("Cov(coefficients)") [1] "Cov(coefficients)" > vcov(that1) th1 th2 th3 th1 20.2976505 -18.6255768 -0.85709424 th2 -18.6255768 36.8083599 0.50091546 th3 -0.8570942 0.5009155 0.07038162 > print("Residuals, etc.") [1] "Residuals, etc." > resid(that1) [1] -10.333482 -5.627781 7.061237 -1.487593 1.166518 -7.888724 [7] 0.562326 -9.223563 11.166518 7.614637 5.566564 1.423342 attr(,"label") [1] "Residuals" > sum(resid(that1)**2) [1] 555.1962 > # done > rm(list=ls()) > q()