/* discriminant analysis example in Stata */
/*  requires Stata 10 or later */

input y1 y2 y3 grp
19.6 5.15 9.5 1
15.4 5.75 9.1 1
22.3 4.35 3.3 1
...
19.8 2.85 2.3 3
16.5 6.55 3.3 3
17.4 6.60 1.9 3
end

candisc y1 y2 y3, group(grp)

Canonical linear discriminant analysis

      |                                 | Like- 
      | Canon.   Eigen-     Variance    | lihood
  Fcn | Corr.    value   Prop.   Cumul. | Ratio     F      df1    df2  Prob>F
  ----+---------------------------------+------------------------------------
    1 | 0.6866  .891988  0.9942  0.9942 | 0.5258  3.5382     6     56  0.0049 e
    2 | 0.0722  .005242  0.0058  1.0000 | 0.9948  .07601     2     29  0.9270 e
  ---------------------------------------------------------------------------
  Ho: this and smaller canon. corr. are zero;                     e = exact F

Standardized canonical discriminant function coefficients

                 | function1  function2 
    -------------+----------------------
              y1 |  -1.09906  -.2473492 
              y2 |  .0209204  -.7741557 
              y3 | -1.109885   .5411163 

Canonical structure

                 | function1  function2 
    -------------+----------------------
              y1 | -.4469698  -.5912455 
              y2 | -.1799598   -.702846 
              y3 |  -.461776   .5722312 

Group means on canonical variables

             grp | function1  function2 
    -------------+----------------------
               1 | -1.272352  -.0041395 
               2 |  .6829395  -.0824017 
               3 |  .5894128   .0865412 

Resubstitution classification summary

    +---------+
    | Key     |
    |---------|
    | Number  |
    | Percent |
    +---------+
                 | Classified                     
    True grp     |      1       2       3 |  Total
    -------------+------------------------+-------
               1 |      9       0       2 |     11
                 |  81.82    0.00   18.18 | 100.00
                 |                        |       
               2 |      2       5       4 |     11
                 |  18.18   45.45   36.36 | 100.00
                 |                        |       
               3 |      2       5       4 |     11
                 |  18.18   45.45   36.36 | 100.00
    -------------+------------------------+-------
           Total |     13      10      10 |     33
                 |  39.39   30.30   30.30 | 100.00
                 |                        |       
          Priors | 0.3333  0.3333  0.3333 |  

estat anova

Univariate ANOVA summaries

               |                                          Adj.
      Variable |  Model MS   Resid MS   Total MS   R-sq   R-sq    F   Pr > F
  -------------+-------------------------------------------------------------
            y1 | 52.924238  293.96544  278.90037  .1526  .0961  2.701 0.0835
            y2 | 3.9751512  126.28728  118.64277  .0305 -.0341  .4722 0.6282
            y3 | 81.829694   426.3709  404.83707   .161  .1051  2.879 0.0718
  ---------------------------------------------------------------------------
                Number of obs = 33     Model df = 2      Residual df = 30

estat correlations

Pooled within-group correlation matrix

                 |       y1        y2        y3 
    -------------+------------------------------
              y1 |  1.00000                     
              y2 |  0.03400   1.00000           
              y3 | -0.58689   0.14732   1.00000 

estat errorrate

Error rate estimated by error count

                 | grp                                        
                 |         1          2          3 |     Total
    -------------+---------------------------------+----------
      Error rate |  .1818182   .5454545   .6363636 |  .4545455
    -------------+---------------------------------+----------
          Priors |  .3333333   .3333333   .3333333 |          

estat grdistance

Mahalanobis squared distances between groups

                 | grp                            
    grp          |         1          2          3
    -------------+--------------------------------
               1 |         0                      
               2 |  3.829291          0           
               3 |  3.474393    .037289          0

estat grmeans

Group means

                 | grp                             
                 |         1          2          3 
    -------------+---------------------------------
              y1 |  18.11818   15.52727   15.34545 
              y2 |  6.190909   5.581818   5.372727 
              y3 |  8.681818   5.109091   5.636364 

estat manova

                           Number of obs =      33

                           W = Wilks' lambda      L = Lawley-Hotelling trace
                           P = Pillai's trace     R = Roy's largest root

                  Source |  Statistic     df   F(df1,    df2) =   F   Prob>F
              -----------+--------------------------------------------------
                     grp | W   0.5258      2     6.0    56.0     3.54 0.0049 e
                         | P   0.4767            6.0    58.0     3.02 0.0122 a
                         | L   0.8972            6.0    54.0     4.04 0.0021 a
                         | R   0.8920            3.0    29.0     8.62 0.0003 u
                         |--------------------------------------------------
                Residual |                30
              -----------+--------------------------------------------------
                   Total |                32
              --------------------------------------------------------------
                           e = exact, a = approximate, u = upper bound on F