多元统计第五次上机实验报告

一、美国法官评分的主成分分析

1
2
3
4
5
6
7
8
9
10
11
12
#prepare data for PCA
data=USJudgeRatings[,1:11]
#step1:calculate corr/cov
sigma=cor(data)
#step2:calculate eigenvalue and eigenvector
vv=eigen(sigma)
#vv$values&vv$vectors
eigenvalue=vv$values
eigenvector=vv$vectors
#step3:select principal component
#step4:calculate the principal component score
score=scale(as.matrix(data))%*%vv$vector

we can also do this in R by using the function named princomp

1
2
3
4
pca=princomp(data,cor=T,scores=T)
pca#show the result
#plot Garaval map
screeplot(pca,type='lines')

From the analysis,we can select the first two compnent as our principal compnent.

1
2
3
4
summary=summary(pca, loadings = T)
pca_data <- predict(pca)
#select the first two compnents as our new_data
new_data=pca_data[,1:2]

refer:https://blog.csdn.net/wangyajie_11/article/details/53785528
Now let’s use the result that we selected just now to do cluster analysis and discriminant analysis

1
2
#cluster analysis
kmeans(new_data,center=3,iter.max=100,nstart=2333)

1
2
3
4
5
6
7
#discriminant analysis
D=dist(data)
library(MASS)
yy=data[,'PHYS']
l=lda(yy~.,data=data.frame(new_data))
#visualize
plot(l)

二、应用以下R自带数据集进行因子分析。

use_data: state.x77

1
2
library('psych')
fa(state.x77, nfactors = 2, score=c("regression"),rotate = "varimax", fm = "ml")

refer: https://www.jianshu.com/p/33ad4e2b29b3

三、R语言的作图

1.常用统计图

1
2
mean=apply(USJudgeRatings,2,mean)
barplot(mean)
1
pie(mean)
1
boxplot(USJudgeRatings)
1
stars(USJudgeRatings)
1
2
3
#unable to download the library
#library('faces')
#faces(USJudgeRatings)

2.基础绘图命令

1
plot(x <- rnorm(10), y=rnorm(10),type = "l", main = "main title",xlab='x axis',ylab='y axis',sub='sub title')
凡希 wechat
喜欢所以热爱,坚持干货分享,欢迎订阅我的微信公众号
呐,请我吃辣条