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. > # nllsqt2.r October 2007, 2008 > # > # test problem for nonlinear least squares > # from Gallant (1975) > # > all <- matrix(scan("gallant.dat"),30,5, byrow=TRUE) > # > # make a data frame > gallant <- data.frame( y=all[,2], x1=all[,3], x2=all[,4], x3=all[,5] ) > print(gallant) # see data frame y x1 x2 x3 1 0.98610 1 1 6.28 2 0.95482 1 1 9.11 3 1.02324 1 1 8.11 4 0.96263 1 1 6.58 5 0.98861 1 1 6.52 6 0.98982 1 1 9.86 7 0.66768 1 1 0.47 8 0.96822 1 1 4.07 9 0.59759 1 1 0.17 10 1.01962 1 1 4.39 11 1.04255 1 1 4.73 12 0.97526 1 1 8.90 13 0.80219 1 1 0.77 14 0.95196 1 1 4.51 15 0.50811 1 1 0.08 16 1.03848 0 1 9.86 17 1.04184 0 1 8.43 18 0.90475 0 1 1.82 19 1.05026 0 1 5.02 20 1.03437 0 1 3.75 21 1.01214 0 1 7.31 22 0.55107 0 1 0.07 23 0.98823 0 1 4.61 24 0.99418 0 1 6.99 25 0.69163 0 1 0.39 26 1.04343 0 1 9.42 27 1.04969 0 1 3.02 28 1.01046 0 1 3.31 29 0.97658 0 1 2.65 30 0.91840 0 1 6.11 > # > # another way is to define residual function > res <- function(th1,th2,th3,th4,y,x1,x2,x3,x4) { + res <- y - th1*x1 - th2*x2 - th4*exp(th3*x3) } > # > # nonlinear least squares > that1 <- nls( ~res(th1,th2,th3,th4,y,x1,x2,x3) , data=gallant, + start=list( th1=-.05, th2=1.04, th3=-1.2, th4=-.53), trace=TRUE ) 0.04025717 : -0.05 1.04 -1.20 -0.53 0.03049629 : -0.02582531 1.01558996 -1.11355727 -0.50431161 0.03049554 : -0.02588913 1.01567783 -1.11577342 -0.50490932 0.03049554 : -0.02588972 1.01567973 -1.11569442 -0.50490262 > # results > that1 Nonlinear regression model model: 0 ~ res(th1, th2, th3, th4, y, x1, x2, x3) data: gallant th1 th2 th3 th4 -0.02589 1.01568 -1.11569 -0.50490 residual sum-of-squares: 0.03050 Number of iterations to convergence: 3 Achieved convergence tolerance: 3.376e-06 > # > thfin <- coef(that1) > print("coefficients") [1] "coefficients" > print(thfin) th1 th2 th3 th4 -0.02588972 1.01567973 -1.11569442 -0.50490262 > # summary > summary(that1) Formula: 0 ~ res(th1, th2, th3, th4, y, x1, x2, x3) Parameters: Estimate Std. Error t value Pr(>|t|) th1 -0.025890 0.012624 -2.051 0.0505 . th2 1.015680 0.009938 102.202 < 2e-16 *** th3 -1.115694 0.163542 -6.822 3.06e-07 *** th4 -0.504903 0.025657 -19.679 < 2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.03425 on 26 degrees of freedom Number of iterations to convergence: 3 Achieved convergence tolerance: 3.376e-06 > # > # done > rm(list=ls()) > q()