[关闭]
@fanxy 2020-03-29T08:55:15.000000Z 字数 5938 阅读 3884

第五讲 统计学习基础与应用

樊潇彦 复旦大学经济学院 金融数据


0. 准备工作

下载数据:Ch5_Data.rar

  1. setwd("D:\\...\\Ch05")
  2. rm(list=ls())
  3. install.packages("ISLR")
  4. # 调用
  5. library(ISLR)
  6. library(tidyverse)
  7. library(ggplot2)

1. 统计学习基本概念

1.1 什么是统计学习?

1.2 相关概念比较

2. 主成分分析(Principal Component Analysis)

2.1 思想与方法

  1. 问题:

    • 当数据集包含太多的变量指标时,我们很难通过相关性分析或做成对散点图的方法来了解变量之间的关系。
    • 当数据集的样本量小于指标数 时,无法对 进行回归分析。
    • 当我们所关心的指标(如企业发展潜力、国家治理能力等)无法直接观测时,就要在一系列可观测指标 的基础上,构造出少数指标 来评判或预测
  2. 定义:

    • 主成分分析(PCA)是将多个原始变量组合为少数几个主成分,并保留绝大部分信息的降维分析技术。
    • 数学表述: 给定经过标准化处理的数据集 ,假定存在矩阵 所表示的超平面(其中 ),将 投影到 得到 维向量:

      的方差满足():

      我们称 为第一主成分的得分(score), 为第一主成分的载荷向量(loading),是原 维空间中数据变异最大的方向。进而在 正交的条件下,提取第二主成分:

      以此类推,直至得到最多 维的向量

2.2 程序实现

  1. library(ISLR) # 调用数据包
  2. dimnames(USArrests) # 美国各州犯罪数据指标
  3. apply(USArrests,2,mean) # 查看各指标均值和方差
  4. apply(USArrests,2, var)

在进行主成分分析之前,需要将各指标进行标准化,否则将直接影响分析结果:


scale.jpg-101.6kB

  1. pca.out=prcomp(USArrests, scale=TRUE) # 主成分分析,用scale=T 进行标准化,或者用x=scale(x)手动处理
  2. pca.out # 主成分载荷矩阵 Phi
  3. names(pca.out) # 其他可报告的变量
  4. head(pca.out$x) # 主成分得分矩阵 Z
  5. pve=pca.out$sdev^2/sum(pca.out$sdev^2) # 每个主成分的方差解释比
  6. plot(pve, type="l")
  7. cumpve=cumsum(pve) # 累积方差解释比
  8. plot(cumpve, type="l")
  9. biplot(pca.out, scale=0) # 前两个主成分的双标图

3. 聚类分析

3.1 什么是聚类分析?

3.2 K均值聚类(K-means Clustering)

3.3 系统聚类(Hierarchical Clustering)

3.4 程序实现

  1. # K均值聚类
  2. set.seed(101)
  3. x=matrix(rnorm(100*2),100,2) # 随机生成样本量为100、维度为2的数据集
  4. xmean=matrix(rnorm(8,sd=4),4,2) # 生成4组、2维的随机数
  5. which=sample(1:4,100,replace=TRUE) # 将100个样本随机分为4组
  6. x=x+xmean[which,] # 通过改变4组数据均值的方式将其分开
  7. plot(x,col=which,pch=19) # 做图
  8. km.out=kmeans(x,4,nstart=15) # K均值聚类,K=4, 重复15次,报告最好结果
  9. km.out
  10. plot(x,col=km.out$cluster,cex=0.5,pch=1,lwd=2)
  11. which=c(4,3,2,1)[which] # 原数据分组倒序排列
  12. km.result=data.frame(km=as.factor(km.out$cluster),which=as.factor(which), x)
  13. table(km.result[,1:2]) # 检查聚类结果,发现有两个样本匹配错了
  14. km.result$miskm=(km.result$km!=km.result$which)
  15. ggplot(km.result, aes(x=X1, y=X2, color=km, size=miskm)) +
  16. geom_point() + theme_bw()
  17. # 系统聚类
  18. hc.complete=hclust(dist(x),method="complete") # 最长距离法
  19. plot(hc.complete)
  20. hc.single=hclust(dist(x),method="single") # 最短距离法
  21. plot(hc.single)
  22. hc.average=hclust(dist(x),method="average") # 类平均法
  23. plot(hc.average)
  24. hc.cut=cutree(hc.complete,4) # 基于最长距离法的聚类结果分为4组
  25. table(hc.cut,which) # 与真实数据比较,有三个样本分错
  26. result=data.frame(x)
  27. result$hc=as.factor(c(2,3,4,1)[hc.cut])
  28. result$mishc=(result$hc!=which)
  29. ggplot(result, aes(x=X1, y=X2, color=hc, size=mishc)) +
  30. geom_point() + theme_bw()

4. 金融数据应用

4.1 Tsay(2013) 多支个股的主成分分析

  1. rtn=read.table("m-5clog-9008.txt",header=T) # 读取5支个股收益率数据
  2. pca.cov = princomp(rtn) # 对协方差矩阵做主成分分析
  3. summary(pca.cov)
  4. pca.corr=princomp(rtn,cor=T) # 对相关系数矩阵做主成分分析
  5. summary(pca.corr)
  6. names(pca.corr)
  7. pca2=prcomp(rtn, scale=T) # 用prcomp命令的结果与pca.corr一致
  8. round(data.frame(pca.corr$sdev, pca2$sdev),3)
  9. pca.corr$loadings
  10. round(pca2$rotation,3)

4.2 聚类方法应用于信用卡数据

  1. # 数据读取
  2. credit=read.csv("Credit.csv", header=T)[,-1]
  3. str(credit)
  4. # 将种族等因子变量转换为数值变量
  5. credit= credit %>%
  6. mutate(Gender=as.numeric(as.character(Gender)==" Male"),
  7. Student=as.numeric(as.character(Student)=="Yes"),
  8. Married=as.numeric(as.character(Married)=="Yes"),
  9. Ethtype=as.numeric(Ethnicity))%>%
  10. dplyr::select(-Ethnicity)
  11. # 聚类分析
  12. km.out=kmeans(credit,3,nstart=15)
  13. credit$km3=km.out$cluster
  14. hc.out=hclust(dist(credit),method="complete")
  15. hc.cut=cutree(hc.out,3)
  16. credit$hc=hc.cut
  17. # 比较两种聚类结果
  18. table(hc.cut,km.out$cluster)
  19. ggplot(credit,
  20. aes(x=Income, y=Rating, color=as.factor(hc), shape=as.factor(km3)))+
  21. geom_point() + theme_bw()

参考材料

  1. L. Breiman, 2001: Statistical modeling: The two cultures, Institute of Mathematical Statistics
  2. G. James, D. Vitten, T. Hastie, R. Tibshirani 著:《统计学习导论:基于R应用》,王星译,机械工业出版社,2015,(在线课程链接
  3. Ruey S. Tsay 著:《金融时间序列分析(第3版)》,王远林、王辉、潘家柱译,人民邮电出版社, 2013
    Ch09 主成分分析和因子模型.pdf5084.8kB
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注