$ontext Linear Least Squares Regression example Erwin Kalvelagen, may 2008 Reference: http://www.statsci.org/data/oz/dugongs.html Age and Length of Dugongs near Townsville Description The data give the age and the length of dugongs (Dugong dugon) (Müller) captured near Townsville in north Queensland, Australia. The lifespan of a dugong is 50-60 years. These data were working estimates. In particular the method of determining the age of dugong has changed somewhat since the data were recorded. -------------------------------------------------------------------------------- Variable Description -------------------------------------------------------------------------------- Age Age in years Length Length in metres -------------------------------------------------------------------------------- Source Marsh, H. R. (1980). Age determination of the dugong [Dugon dugon (Müller)] in Northern Australia and its biological implications. In W. F. Perrin and A. C. Myrick (Eds.), Age Determination in Toothed Cetaceans and Sirenians, International Whaling Commission, Cambridge. Dr Helene March, James Cook University, Australia. Ratkowsky, D. A. (1983). Nonlinear Regression Modelling. Marcel Dekker, New York. $offtext *----------------------------------------------------------------------------- * data *----------------------------------------------------------------------------- set i /i1*i27/; table data(i,*) Age Length i1 1 1.8 i2 1.5 1.85 i3 1.5 1.87 i4 1.5 1.77 i5 2.5 2.02 i6 4 2.27 i7 5 2.15 i8 5 2.26 i9 7 2.35 i10 8 2.47 i11 8.5 2.19 i12 9 2.26 i13 9.5 2.4 i14 9.5 2.39 i15 10 2.41 i16 12 2.5 i17 12 2.32 i18 13 2.43 i19 13 2.47 i20 14.5 2.56 i21 15.5 2.65 i22 15.5 2.47 i23 16.5 2.64 i24 17 2.56 i25 22.5 2.7 i26 29 2.72 i27 31.5 2.57 ; parameters length(i) 'meters' age(i) 'years' ; length(i) = data(i,'length'); age(i) = data(i,'age'); variables a0 'parameter to be estimated' a1 'parameter to be estimated' sse 'sum of squared errors' ; equation fit1(i) 'equation to fit' fit2(i) 'equation to fit' sumsq 'dummy objective' ; sumsq.. sse =n= 0; fit1(i).. length(i) =e= a0 + a1*age(i); fit2(i).. log(length(i)) =e= a0 + a1*log(age(i)); option lp=ls; models m1 /sumsq,fit1/ m2 /sumsq,fit2/ ; scalars p0,p1,q0,q1; solve m1 minimizing sse using lp; p0 = a0.L; p1 = a1.L; solve m2 minimizing sse using lp; q0 = a0.L; q1 = a1.L; * * plot results * file pltdat /dugongs.dat/; loop(i, put pltdat Age(i):17:5,Length(i):17:5,log(Age(i)):17:5,log(Length(i)):17:5/; ); putclose; file plt /dugongs.plt/; put plt; put "p0=",p0:0:16/; put "p1=",p1:0:16/; put "q0=",q0:0:16/; put "q1=",q1:0:16/; put "fit1(x)=p0+p1*x"/; put "fit2(x)=q0+q1*x"/; putclose 'set term png'/ 'set key off'/ 'set output "dugongs.png"'/ 'set multiplot layout 1,2 scale 1,0.5'/ 'set xlabel "Age"'/ 'set ylabel "Length"'/ 'plot "dugongs.dat" using 1:2,fit1(x)'/ 'set xlabel "log(Age)"'/ 'set ylabel "log(Length)"'/ 'plot "dugongs.dat" using 3:4,fit2(x)'/ 'unset multiplot'/ ; execute 'type dugongs.plt'; execute '=wgnuplot.exe dugongs.plt'; execute '=shellexecute dugongs.png';