/* Adapted from linregasc.aml by Michael Silberbauer 2005-03-07 /* Assumes that the call is from an ARCPLOT environment. /* Returns linear regression variables /* 2007-12-13 Corrected output of r to r^2 Michael Silberbauer /* 2007-12-14 Removed incorrect variance code Michael Silberbauer &args r2name yintname slopename xvar yvar file &if [null %file%] &then &return &warn Usage: LINREGFIL \~ linregasc.aml takes the nCases variables in x and y, placing the results\~ in r2name yintname slopename\~ e.g. LINREGASC .RSQ .YINT .SLOPE LOGQ LOGC LOAD.TMP\~ plots the LOGQ,LOGC points in LOAD.TMP and returns\~ the regression constants as '%.RSQ%, %.YINT% and %.SLOPE%' &type running %AML$FULLFILE% &type variables: &type r2name %r2name% &type yintname %yintname% &type slopename %slopename% &type xvar %xvar% &type yvar %yvar% &type file %file% containing items and first two records: items %file% info list %file% info 1 1 &sv nCases = [extract 1 [show select %file% info]] &if %nCases% <= 0 &then &return %AML$FILE%: no data in %file% &sv Meanx = 0 &sv Meany = 0 &sv SumX = 0 &sv SumY = 0 &sv XY = 0 &sv SumXY = 0 &sv Sumx2 = 0 &sv Sumy2 = 0 /*&sv StDx = 0 /*&sv StDy = 0 &do n = 1 &to %nCases% &sv x = [show select %file% info %n% item %xvar% ] &sv y = [show select %file% info %n% item %yvar% ] &sv SumX = [calc %SumX% + %x%] &sv SumY = [calc %SumY% + %y%] &sv XY = [calc %x% * %y%] &sv SumXY = [calc %SumXY% + %XY% ] &sv x2 = [calc %x% * %x%] &sv y2 = [calc %y% * %y%] &sv Sumx2 = [calc %Sumx2% + %x2%] &sv Sumy2 = [calc %Sumy2% + %y2%] &if %n% = 1 &then &sv Maxx = %x% &else &if %x% > %Maxx% &then &sv Maxx = %x% &if %n% = 1 &then &sv Maxy = %y% &else &if %y% > %Maxy% &then &sv Maxy = %y% /*&ty %n%: %x% %y% &end &sv Meanx = [calc %SumX% / %nCases%] &sv Meany = [calc %SumY% / %nCases%] /*&sv StDx2 = [calc ( %Sumx2% - ( %nCases% * %x2% ) ) / ( %nCases% - 1 ) ] /*&sv StDy2 = [calc ( %Sumy2% - ( %nCases% * %y2% ) ) / ( %nCases% - 1 ) ] /* Calculate the slope: &sv slopea = [calc ( ( %sumxy% ) - ( %nCases% * %Meanx% * %Meany% ) )] &sv slopeb = [calc ( ( %Sumx2% ) - ( %nCases% * %Meanx% * %Meanx% ) ) ] &sv slope = [calc ( %slopea% / %slopeb% ) ] /* Calculate the y-intercept: &sv yInt = [calc %MeanY% - ( %slope% * %MeanX% ) ] /* Calculate r-squared: &sv topr = [calc ( %nCases% * %SumXY% ) - ( %SumX% * %SumY% )] &sv bot1 = [calc ( %nCases% * %SumX2% ) - ( %SumX% * %SumX% )] &sv bot2 = [calc ( %nCases% * %SumY2% ) - ( %SumY% * %SumY% )] &sv botr = [sqrt [calc %bot1% * %bot2%]] &if %botr% = 0 &then &do &sv r = -9 &sv rSquare1 = -9 &sv rSquare = -9 &TYPE R-Squared = INVALID, slope = %slope%, y-intercept = %yInt% &end &else &do &sv r = [calc %topr% / %botr%] &sv rSquare1 = [calc %r% * %r%] &sv rSquare = [calc [round [calc %rSquare1% * 100]] / 100] &TYPE R-Squared = %rSquare%, slope = %slope%, y-intercept = %yInt% &end &sv %r2name% = %rSquare% &sv %yintname% = %yInt% &sv %slopename% = %slope% &sv yInt2 = [calc ( %slope% * %Maxx% ) + %yInt% ] &return