# 【数据分析 R语言实战】学习笔记 第十一章 对应分析

11.2对应分析

11.2.1理论基础

11.2.2 R语言实现

R中的程序包MASS提供了两个函数，corresp()用于做简单一的对应分析，mca()用于计算多重对应分析，通常使用前者，其调用格式为corresp(x,nf=1,……)

x是数据矩阵:nf表示因子分析中计算因子的个数，通常取2.

【例】

> ch=data.frame(A=c(47,22,10),B=c(31,32,11),C=c(2,21,25),D=c(1,10,20))

> rownames(ch)=c("Pure-Chinese","Semi-Chinese","Pure-English")

> library(MASS)

> ch.ca=corresp(ch,nf=2)

> options(digits=4)

> ch.ca

First canonical correlation(s): 0.5521 0.1409

Row scores:

[,1] [,2]

Pure-Chinese 1.2069 0.6383

Semi-Chinese -0.1368 -1.3079

Pure-English -1.3051 0.9010

Column scores:

[,1] [,2]

A 0.9325 0.9196

B 0.4573 -1.1655

C -1.2486 -0.5417

D -1.5346 1.2773

MASS程序包的函数功能还是有限的，于是一些R软件使用者开发了专门用来处理对应分析的程序包，如ca包，该包专门用于计算并可视化简单对应分析、多重及联合对应分析。

> brand=data.frame(low=c(2,49,4,4,15,1),medium=c(7,7,5,49,2,7),high=c(16,3,23,5,5,14))

> rownames(brand)=c("A","B","C","D","E","F")

> library(ca)

> options(digits=3)

> brand.ca=ca(brand)

> brand.ca

Principal inertias (eigenvalues):

1 2

Value 0.530966 0.343042

Percentage 60.75% 39.25%

Rows:

A B C D E F

Mass 0.1147 0.271 0.147 0.266 0.101 0.1009

ChiDist 0.7704 1.026 0.906 1.029 0.738 0.7939

Inertia 0.0681 0.285 0.120 0.282 0.055 0.0636

Dim. 1 -0.7267 1.399 -0.581 -0.850 0.988 -0.8296

Dim. 2 0.9553 -0.200 1.368 -1.403 0.281 0.8786

Columns:

low medium high

Mass 0.3440 0.353 0.303

ChiDist 1.0058 0.861 0.934

Inertia 0.3480 0.262 0.264

Dim. 1 1.3792 -0.778 -0.659

Dim. 2 -0.0663 -1.107 1.367

> names(brand.ca)

[1] "sv" "nd" "rownames" "rowmass"

[5] "rowdist" "rowinertia" "rowcoord" "rowsup"

[9] "colnames" "colmass" "coldist" "colinertia"

[13] "colcoord" "colsup" "call"

> brand.ca\$rowcoord

Dim1 Dim2

A -0.727 0.955

B 1.399 -0.200

C -0.581 1.368

D -0.850 -1.403

E 0.988 0.281

F -0.830 0.879

plot(brand.ca)