' ******************************************************************
' Two-way analysis of variance (one observation per sample)
' ******************************************************************

#INCLUDE "fbmath.bi"

#DEFINE NA    3     ' Number of modalities of factor A
#DEFINE NB    4     ' Number of modalities of factor B
#DEFINE Alpha 0.05  ' Significance level

' The samples are stored in a matrix Z such that
' Z(I, J) contains the observation for the I-th
' modality of factor A and the J-th modality of factor B
DIM AS DOUBLE Z(1 TO NA, 1 TO NB) = _
{{2, 1, 3, 1}, _
 {3, 2, 3, 2}, _
 {3, 4, 5, 3}}

DIM AS DOUBLE  S(1 TO NA, 1 TO NB)  ' Standard dev.

DIM AS DOUBLE  V(1 TO 3)    ' Variances (A, B, interaction)
DIM AS INTEGER DoF(1 TO 3)  ' Degrees of freedom (A, B, interaction)
DIM AS DOUBLE  F(1 TO 2)    ' Variance ratios (A, B)
DIM AS DOUBLE  Fc(1 TO 2)   ' Critical values
DIM AS DOUBLE  P            ' Probability
DIM AS INTEGER I            ' Loop variable

' Compare means. The matrix of means is equal to the data matrix.
' The matrix of standard deviations will be ignored.
AnOVa2 1, Z(), S(), V(), F(), DoF()

' Compute critical values
P = 1 - Alpha
FOR I = 1 TO 2
  Fc(I) = InvSnedecor(DoF(I), DoF(3), P)
NEXT I

' Print results
PRINT "Two-way ANOVA"
PRINT
PRINT USING "Source        Variance    D.o.F.     F      F(p = #.###)"; Alpha
PRINT "--------------------------------------------------------"
PRINT USING "Factor A     ####.####    ####  ####.####  ####.####"; V(1); DoF(1); F(1); Fc(1)
PRINT USING "Factor B     ####.####    ####  ####.####  ####.####"; V(2); DoF(2); F(2); Fc(2)
PRINT USING "Interaction  ####.####    ####"; V(3); DoF(3)

SLEEP
