#Program to analyze the data in Table 13.1 for Example 13.1
library(MASS)
yd=read.table("C:\\Research\\Book\\Data\\table131.txt")
yd
xv = yd[,1]
yv = yd[,2]
b1 = 200
b2 = 0.1
x0 = c(b1, b2) #Initial estimates for parameters beta1 and beta2
nl = yv ~ b1*xv/(b2+xv)
fm1= nls(nl, start=list(b1=200,b2=0.1),trace=TRUE)
fm1
summary(fm1)
pred = cbind(predict(fm1))
resid=yv-pred
res=cbind(xv,yv,pred,resid)
colnames(res)=c("xy","yv","predicted","residual")
res
tt = seq(min(xv), max(xv), length=200)
plot(yv ~ xv)
lines(tt, predict(fm1, list(xv = tt)))

#######################################################################

Program to analyze the data in Table 13.2 for Example 13.2
library(MASS)
yd=read.table("C:\\Research\\Book\\Data\\table132.txt")
yd
yv = yd[,1]
xv = yd[,2]
b0 = 0.0
b1 = 0.0
b2 = 0.1
x0 = c(b0, b1, b2) #Initial estimates for parameters beta0, beta1 and beta2

#Computing the function to minimize
f.ss = function(xf, yd) 
{
yv=yd[,1]
xv=yd[,2]
b0=xf[1]
b1=xf[2]
b2=xf[3]
de=1+(xv/b2)**b1
ra = b0-b0/de
f = sum((yv - ra)**2)
}
print("Results from non-linear regression model")
I.nl=nlminb(start=x0, f.ss, lower=c(-Inf,-Inf), upper=c(Inf, Inf), yd=yd)
I.nl[[1]]
I.nl[[2]]
I.nl[[3]]
I.nl[[4]]
I.nl[[5]]
es=I.nl[[1]]

#Predicted values
de=1+(xv/es[3])**es[2]
y.fit = es[1]-es[1]/de
residual=yv-y.fit
cbind(xv,yv,y.fit,residual)