/* ANOVA - Two Way */ options results if ~show('P','TCALC') then do address command 'run sys:turbocalc/turbocalc' address command 'waitforport TCALC' loadflag=1 end address 'TCALC' /* Add-in Rexx Math Library needed for some routines */ signal on syntax if ~show('l','rexxmathlib.library') then call addlib('rexxmathlib.library',0,-30) if ~show('l','rexxreqtools.library') then call addlib('rexxreqtools.library',0,-30) /* add to library list */ signal off syntax /* Start Main Routine */ if loadflag=1 then 'Load()' 'ActivateWindow()' ;'LiesCursorPos' range=rtgetstring(,"Enter Cell Range for Input","Input Request",,'rt_pubscrname="TurboCalc"') colon=pos(":",range) if colon=0 then do 'Message "Please select a range~before executing this script"' exit end /* Find cell references and cell, column numbers */ start_cell=substr(range,1,colon-1) end_cell=substr(range,colon+1) start_row=cellrow(start_cell) end_row=cellrow(end_cell) start_col=cellcol(start_cell) end_col=cellcol(end_cell) NRows=end_row-start_row+1 NCols=end_col-start_col+1 /* Get Number of Rows per Sample */ rows=0 rows=rtgetlong(,"Enter Number of Rows in each sample","Get Rows",,'rt_pubscrname="TurboCalc"') if rows=0 then exit if ~datatype(left(rows,1),'n') then do 'Message "Invalid row number"' exit end /* Get cell reference for output range */ out_cell=rtgetstring(,"Enter Cell Reference for Output","Input Request",,'rt_pubscrname="TurboCalc"') if out_cell="" then exit if length(out_cell)<2 | datatype(left(out_cell,1),'n') then do 'Message "Invalid cell reference"' exit end /* Suppress Screen Redraw to Speed Things Up */ 'Refresh 0' /* Get cell references for top cell in each column */ 'SelectCell' start_cell do col=start_col to end_col 'GetCursorPos' top_cell.col=result 'Column 1' end /* Get labels for later use on output */ 'SelectCell' start_cell 'Column 1' 'GetValue' testlabel=result testlabel=strip(testlabel) if datatype(testlabel,'n')=1 then do labelflag=0 do x=start_col+1 to end_col title.x="Column "||x end end else do labelflag=1 do x=start_col+1 to end_col 'GetValue' title.x=result 'Column 1' end end 'SelectCell' start_cell w=0 do x=1 to NRows 'GetValue' testlabel=result testlabel=strip(testlabel) if datatype(testlabel,'a')=1 then do w=w+1 'GetValue' sampletitle.w=result say sampletitle.w end 'Down 1' end NumSamples=w /* Number of samples */ /* Get data from cell range */ col=start_col s=0 z=0 lav=0 tot=0 data.=0 count.=0 total.=0 cell.=0 do x=2 to NCols s=1 col=col+1 'SelectCell' top_cell.col if labelflag=1 then 'Down 1' if labelflag=0 then NRows=NRows+1 do y=1 to NRows-1 z=z+1 'GetValue' valtest=result if datatype(valtest)='NUM' then do 'GetValue' val=result data.s.x.y=val tot=tot+val total.x=tot count.x=1+count.x cell.s.x=(cell.s.x)+val end 'Down 1' if z=rows then do z=0 s=s+1 end end tot=0 lav=0 val=0 end rowtot.=0 do s=1 to NumSamples do x=2 to NCols rowtot.s=(cell.s.x)+rowtot.s end end scount=rows*(NCols-1) /* Calculate Means */ meancol.=0 meanrow.=0 meanrc.=0 /* Means within cells of sample */ GM=0 /* Grand Mean of all observations */ NumObs=0 /* Total number of observations */ GT=0 /* Grand Total of all observations */ do x=2 to NCols meancol.x=total.x/count.x NumObs=NumObs+count.x GT=GT+total.x end do s=1 to NumSamples meanrow.s=(rowtot.s)/scount end GM=GT/NumObs do s=1 to NumSamples do x=2 to NCols meanrc.s.x=(cell.s.x)/rows end end /* Calculate Standard deviation and Variance */ dat=0 meenx=0 sum.=0 sd.=0 var.=0 z=0 N=rows-1 do x=2 to NCols s=1 do y =1 to NRows-1 meen=meanrc.s.x z=z+1 dat=data.s.x.y sum.s.x=(dat-meen)**2+(sum.s.x) if z=rows then do var.s.x=(sum.s.x)/N /* Variance & SD within cells */ sd.s.x=sqrt(var.s.x) z=0 s=s+1 end end end summ.=0 varrow.=0 sdrow.=0 s=1 z=0 N=((Ncols-1)*rows)-1 do y=1 to NRows-1 z=z+1 do x=2 to NCols meen=meanrow.s dat=data.s.x.y summ.s=(dat-meen)**2+(summ.s) end if z=rows then do varrow.s=(summ.s)/N /* Variance & SD of sample */ sdrow.s=sqrt(varrow.s) z=0 s=s+1 end end summcol.=0 varcol.=0 sdcol.=0 z=0 N=NRows-2 do x=2 to NCols s=1 meen=meancol.x do y=1 to NRows-1 z=z+1 dat=data.s.x.y summcol.x=(dat-meen)**2+(summcol.x) if z=rows then do z=0 s=s+1 end end varcol.x=(summcol.x)/N /* Variance & SD of columns */ sdcol.x=sqrt(varcol.x) end /* Calculate Sum of Squares */ SSR=0 /* Sum of Squares Rows */ SSC=0 /* Sum of Squares Columns */ SSW=0 /* Sum of Squares Within Cells */ SSTotal=0 /* Sum of Squares Total */ SSI=0 /* Sum of Squares Interaction */ /* Calculate SSR */ ss=0 do s=1 to NumSamples ss=ss+((meanrow.s)-GM)**2 end SSR=ss*(NCols-1)*rows /* Calculate SSC */ ss=0 do x=2 to NCols ss=ss+((meancol.x)-GM)**2 end SSC=ss*NumSamples*rows /* Calculate SSI */ ss=0 do s=1 to NumSamples do x=2 to NCols ss=ss+((meanrc.s.x)-(meanrow.s)-(meancol.x)+GM)**2 end end SSI=ss*rows /* Calculate SSW */ ss=0 z=0 do x=2 to NCols s=1 do y=1 to NRows-1 z=z+1 ss=ss+((data.s.x.y)-(meanrc.s.x))**2 if z=rows then do z=0 s=s+1 end end end SSW=ss /* Calculate SSTotal */ ss=0 z=0 do x=2 to NCols s=1 do y=1 to NRows-1 z=z+1 ss=ss+((data.s.x.y)-GM)**2 if z=rows then do z=0 s=s+1 end end end SSTotal=ss /* Calculate Variances */ DFR=NumSamples-1 /* Degrees of Freedom Rows */ DFC=NCols-2 /* Degrees of Freedom Columns */ DFI=DFR*DFC /* Degress of Freedom Interaction */ DFW=NumSamples*(NCols-1)*(Rows-1) /* Degrees of Freedom Within Cells */ DFT=rows*NumSamples*(NCols-1)-1 /* Degrees of Freedom Total */ VarR=SSR/DFR /* Variance Estimate for Samples */ VarC=SSC/DFC /* Variance Estimate for Columns */ VarW=SSW/DFW /* Variance estimate for Within Cells */ VarI=SSI/DFI /* Variance Estimate for Interaction */ Fcol=VarC/VarW /* F Ratio for Columns */ Frow=VarR/VarW /* F Ratio for Rows */ Finter=VarI/VarW /* F Ratio for Interaction */ /* Output */ 'SelectCell' out_cell 'ColumnWidth 10' 'Put "ANOVA - Two Way: With Replication"' 'Down 2' 'Put "Summary Statistics"' 'Down 2' 'GetCursorPos' go_cell=result do x=start_col+1 to end_col 'ColumnWidth 10' 'Column 1' title=""""||title.x||"""" 'Put' title end 'Column 1' 'ColumnWidth 10' 'Put' "Total" 'SelectCell' go_cell 'Down 2' do s=1 to NumSamples 'GetCursorPos' start_cell=result sampletitle=""""||sampletitle.s||"""" 'Put' sampletitle 'Down 1' 'Put' "Count" 'Down 1' 'Put' "Sum" 'Down 1' 'Put' "Mean" 'Down 1' 'Put' "Variance" 'Down 1' 'GetCursorPos' end_cell=result 'SelectCell' start_cell 'Down 1' do x=2 to NCols 'Column 1' 'Put' rows 'Down 1' 'Put' cell.s.x 'Down 1' 'Put' meanrc.s.x 'Down 1' 'Put' var.s.x 'Up 3' end 'Column 1' 'Put' scount 'Down 1' 'Put' rowtot.s 'Down 1' 'Put' meanrow.s 'Down 1' 'Put' varrow.s 'Down 1' 'SelectCell' end_cell 'Down 2' end 'Put "Col. Total"' 'Down 2' 'GetCursorPos' start_cell=result 'Put' "Count" 'Down 1' 'Put' "Sum" 'Down 1' 'Put' "Mean" 'Down 1' 'Put' "Variance" 'GetCursorPos' end_cell=result 'SelectCell' start_cell do x=2 to NCols 'Column 1' 'Put' count.x 'Down 1' 'Put' total.x 'Down 1' 'Put' meancol.x 'Down 1' 'Put' varcol.x 'Up 3' end 'SelectCell' end_cell 'Down 2' 'Put' "ANOVA" 'Down 2' 'GetCursorPos' start_cell=result 'Put "Source of"' 'Down 1' 'Put " Variation"' 'Column 1' 'Up 1' 'Put "Sum of"' 'Down 1' 'Put " Squares"' 'Column 1' 'Up 1' 'Put "Degrees of"' 'Down 1' 'Put " Freedom"' 'Column 1' 'UP 1' 'Put' "Variance" 'Down 1' 'Put " Estimate"' 'Column 1' 'Up 1' 'Put "F Ratio"' 'SelectCell' Start_cell 'Down 2' 'Put' "Sample" 'Down 1' 'Put' "Column" 'Down 1' 'Put' "Interaction" 'Down 1' 'Put "Within Cells"' 'Down 1' 'Put' "Total" 'SelectCell' Start_cell 'Down 2' 'Column 1' 'Put' SSR 'Down 1' 'Put' SSC 'Down 1' 'Put' SSI 'Down 1' 'Put' SSW 'Down 1' 'Put' SSTotal 'Up 4' 'Column 1' 'Put' DFR 'Down 1' 'Put' DFC 'Down 1' 'Put' DFI 'Down 1' 'Put' DFW 'Down 1' 'Put' DFT 'Up 4' 'Column 1' 'Put' VarR 'Down 1' 'Put' VarC 'Down 1' 'Put' VarI 'Down 1' 'Put' VarW 'Up 3' 'Column 1' 'Put' Frow 'Down 1' 'Put' Fcol 'Down 1' 'Put' Finter 'Refresh 1' 'Refresh 2' 'Message' "Finished" exit /* Procedures */ cellrow: procedure do parse arg cell do charpos=2 to length(cell) if datatype(substr(cell,charpos,1),n) then return substr(cell,charpos) end return 0 end Return cellcol: procedure do parse arg cell labels="ABCDEFGHIJKLMNOPQRSTUVWXYZ" cell=upper(cell) len=length(cell) val=0 do charpos=1 to len if datatype(substr(cell,charpos,1),n) then do cell=reverse(substr(cell,1,charpos-1)) do x=1 to length(cell) val=(26**(x-1))*pos(substr(cell,x,1),labels)+val end return val end end return 0 end Return syntax: if arg(1)='FAIL' then do 'Message' "Library is unavailable." exit end 'Message' "Unknown Syntax Error" exit