program Simple Linear Regression parameter (N=31) dimension y(N),x(N) c An example : data x/19.,25.,22.,-1.,4.,14.,21.,22.,23.,27., * 29.,25.,29.,15.,29.,24.,0.,2.,26.,17., *19.,9.,20.,-6.,-13.,-13.,-11.,-4.,-4.,11.,23./ data y/28.,28.,26.,19.,16.,24.,26.,24.,24.,29., * 29.,27.,31.,26.,38.,23.,13.,14.,28.,19., * 19.,17.,22.,2.,4.,5.,7.,8.,14.,14.,23./ ! Call function to calculate regression coefficient and others call SLRegress(x,y,n,a,b,q,p,umax,umin,u,dyy) ! Output result on screen write(*,*) write(*,10)a,b 10 format(/1x,'a=',f10.5,' b=',f10.5) write(*,100) dyy 100 format(1x,'Total sum of squares=',f10.4) write(*,102) p 102 format(1x,'Regression sum of squares=',f10.4) write(*,104) q 104 format(1x,'Residual sum of squares=',f10.4) f=p/(q/(n-2)) write(*,106) f 106 format(1x,'F=',f10.4) write(*,108) umax,umin,u 108 format(1x,'Maximum error=',f10.4,' Minimum error=',f10.4,' * Mean error=',f10.4) pause 'Press any key to continue...' ! Output the Analysis-of-Variance Table write(*,'(//)') write(6,200) 200 format(/1x, 'Generic Analysis of Variance Table for the simple * Linear Regression') write(6,202) 202 format(/1x,'------------------------------------------------------ *---------------') write(6,204) 204 format(/3x,'Source df SS MS') write(6,202) write(6,206) n-1,dyy 206 format(/1x,'Total n-1=',i2,' SST=',f8.3) write(6,208) 1,p,p 208 format(/1x,'Regression K=',i2,' SSR=',f8.3,' MSR=SSR/K=' *,f8.3) q2=q/real(n-1-1) write(6,209) n-1-1,q,q2 209 format(/1x,'Residual n-k-1=',i2,' SSE=',f8.3,' MSE=SSE/(n-k-1) *=',f8.3) f=p/(q/real(n-1-1)) write(6,220) f 220 format(/1x,' F=MSR/MSE=',f9.4) write(6,202) end !------------------------------------------------------------------------ ! Simple Linear Regression ! Input parameter ! x: Predictor Variable ! y: Predictand Variable ! n: Seiral length ! Output parameter ! a,b: Regress coefficient, y=a+bx ! q: Residual sum of squares ! p: Regression sum of squares ! umax: Maximum error ! umin: Minimum error ! u: Mean error ! dyy: Total sum of squares **************************** subroutine SLRegress(x,y,n,a,b,q,p,umax,umin,u,dyy) dimension x(n),y(n) xx=0.0 yy=0.0 do 10 i=1,n xx=xx+x(i)/n yy=yy+y(i)/n 10 continue dx=0.0 dxy=0.0 dyy=0.0 do 20 i=1,n q=x(i)-xx dx=dx+q*q dxy=dxy+q*(y(i)-yy) dyy=dyy+(y(i)-yy)*(y(i)-yy) 20 continue b=dxy/dx a=yy-b*xx q=0.0 u=0.0 p=0.0 umax=0.0 umin=1.0d+30 do 30 i=1,n s=a+b*x(i) q=q+(y(i)-s)*(y(i)-s) p=p+(s-yy)*(s-yy) dx=abs(y(i)-s) if(dx.gt.umax) umax=dx if(dx.lt.umin) umin=dx u=u+dx/n 30 continue s=sqrt(q/n) return end !------------------------------------------------------------------------