UCLA Academic Technology Services HomeServicesClassesContactJobs

R Code Fragment
Correspondence analysis from summary data

There are times when you want to do correspondence anlysis and the data have been collapsed into a summary with counts for each of the categories. For example, here is a text file with the number of degrees given in 12 disciplines over eight different years. The name of the file is degree2.txt
disc   v60  v65  v70  v71  v72  v73  v74  v75
Eng    794 2073 3432 3495 3475 3338 3144 2959
Math   291  685 1222 1236 1281 1222 1196 1149
Phy    530 1046 1655 1740 1635 1590  134 1293
Chem  1078 1444 2234 2204 2011 1849 1792 1762
Earth  253  375  511  550  580  577  570  556
Bio   1245 1963 3360 3633 3580 3636 3473 3498
Agri   414  576  803  900  855  853  830  904
Psych  772  954 1888 2116 2262 2444 2587 2749
Soc    162  239  504  583  638  599  645  680
Econ   341  538  826  791  863  907  833  867
Anth    69   82  217  240  260  324  381  385
Oth    314  502 1079 1392 1500 1609 1531 1550
First, we will read in the text file and create a dataframe
degrees<-read.table("data/degree2.txt", header=T)
deg<-data.frame(degrees[, 2:9], row.names=degrees[,1])

deg

       v60  v65  v70  v71  v72  v73  v74  v75
Eng    794 2073 3432 3495 3475 3338 3144 2959
Math   291  685 1222 1236 1281 1222 1196 1149
Phy    530 1046 1655 1740 1635 1590  134 1293
Chem  1078 1444 2234 2204 2011 1849 1792 1762
Earth  253  375  511  550  580  577  570  556
Bio   1245 1963 3360 3633 3580 3636 3473 3498
Agri   414  576  803  900  855  853  830  904
Psych  772  954 1888 2116 2262 2444 2587 2749
Soc    162  239  504  583  638  599  645  680
Econ   341  538  826  791  863  907  833  867
Anth    69   82  217  240  260  324  381  385
Oth    314  502 1079 1392 1500 1609 1531 1550
Now, we will run the actual correspondence analysis using the corresp() functions from the MASS package. This will be followed by the biplot() function.
library(MASS)

corresp(deg)

First canonical correlation(s): 0.1266166 

 Row scores:
         Eng         Math          Phy         Chem        Earth          Bio         Agri        Psych          Soc 
 0.119188175 -0.160491956  2.733744104  0.792237083  0.001462284 -0.143360952  0.161123882 -1.094334431 -0.962278316 
        Econ         Anth          Oth 
-0.026843187 -2.152931008 -1.164647625 

 Column scores:
       v60        v65        v70        v71        v72        v73        v74        v75 
 0.9020415  1.4344977  0.8278114  0.5480978  0.1991598 -0.0902502 -2.0634987 -0.6782863 

biplot(corresp(deg, nf = 2))


How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.