Solved – Correspondence analysis for a three-way contingency table

contingency tablescorrespondence-analysisr

I'm wondering how to proceed to perform Canonical Correspondence Analysis and Multiple Correspondence Analysis in R on the following three-way contingency table

Eco_region3  Eco_region4  Species Freq
    A           A1          S1      10
    A           A1          S2      12
    A           A1          S3       8
    A           A2          S1      10
    A           A2          S2       6
    A           A2          S3      11
    A           A3          S1       2
    A           A3          S2       9
    A           A3          S3      13

    B           B1          S1      13
    B           B1          S2      15
    B           B1          S3       7
    B           B2          S1       9
    B           B2          S2       8
    B           B2          S3      13
    B           B3          S1      15
    B           B3          S2      12
    B           B3          S3      13

    C           C1          S1      12
    C           C1          S2      18
    C           C1          S3      20
    C           C2          S1      12
    C           C2          S2       0
    C           C2          S3      11
    C           C3          S1      18
    C           C3          S2      10
    C           C3          S3      16

Eco_region4 is nested within Eco_region3.

Thanks

Best Answer

assuming your data are in a data.frame called dt, you can turn it into a table object using xtabs

tbl <- xtabs(Freq ~ Species + Eco_region4, data=dt)
tbl
          Eco_region4
   Species A1 A2 A3 B1 B2 B3 C1 C2 C3
        S1 10 10  2 13  9 15 12 12 18
        S2 12  6  9 15  8 12 18  0 10
        S3  8 11 13  7 13 13 20 11 16

You can get a three dimensional table by adding Eco_region3 to the end of the formula, but then the correspondence analysis would fail because of the nested structure of your data.

You can perform correspondence analysis with the ca function from the ca package.

Related Question