# 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 # # 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 ) # results that1 # thfin <- coef(that1) print("coefficients") print(thfin) # summary summary(that1) # # done rm(list=ls()) q()