R practice of the principal component analysis method of R language learning

Source: Internet
Author: User

Principal component Analysis R software implementation program (i):

>d=read.table ("clipboard", header=t) #从剪贴板读取数据

>sd=scale (d) #对数据进行标准化处理

>SD #输出标准化后的数据和属性信息, copy standardized data to clipboard backup

>d=read.table ("clipboard", header=t) #从剪贴板读取标准化数据

>pca=princomp (d,cor=t) #主成分分析函数

>screeplot (pca,type= "line", mian= "gravel chart", lwd=2) #画出碎石图

From the gravel map can be seen, the first two principal components of the variance contribution ratio is larger, the following calculation of the first two principal components of the cumulative variance contribution rate is more than 80% ...

>dcor=cor (d) #求相关矩阵

>deig=eigen (Dcor) #求相关矩阵的特征值和特征向量

>deig$values #输出特征值

>sumeigv=sum (deig$values)

>SUMEIGV Get K value

>sum (Deig$values[1:2])/k #求前两个主成分的累积方差贡献率

>pca$loadings[,1:2] #输出前2个主成分的载荷系数

The observed load factor can be obtained: Principal component C1 in ...

>deig$values[1]/k;deig$values[2]/k; Coefficient of #计算主成分C1, C2 B1, B2

C= (B1*C1+B2*C2)/(B1+B2) =Q1*C1+Q2*C2

>s=pca$scores[,1:2] #输出前两个主成分的得分

>C=S[1: Number of evaluated objects, 1]*Q1+S[1: Number of evaluated objects, 2]*Q2

>cbind (S,C)

Then the value of the comprehensive score C is sorted from small to large, and the final evaluation results are obtained.

Note: Here the value of C refers to the value of the real number of C, not an absolute value.

Principal component Analysis R software implementation program (II):

In Excel, click Copy to read the file

>data<-read.table ("clipboard", header=t,sep= ' \ t ')

>data

>data.pr<-princomp (data,cor=true) #data为数据矩阵或数据框, Cor is the correlation matrix, the default is the covariance matrix, scores is the output component score

>summary (data.pr,loading=true) #loading =ture option lists the coefficients of the primary component corresponding to the original variable

Among them: standard deviation standards deviation porportion of Variance contribution rate (variance ratio)

Comulative Proportion Cumulative contribution rate (cumulative ratio)

Draw three kinds of gravel graphs:

>screeplot (DATA.PR) #条型

>biplot (DATA.PR) #分散型

>screeplot (data.pr,type= ("line")) #线型

R practice of the principal component analysis method of R language learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.