library('ggplot2')

prices <- read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\08-PCA\\data\\stock_prices.csv',stringsAsFactors = FALSE)

library('lubridate')

#把日期列转成日期对象

prices <- transform(prices, Date = ymd(Date))

#prices中的数据只有三列,日期,股票名,价格

library('reshape')

#转下格式,矩阵中,每一天是一行,一行中包含了所有股票当天的价格,Date~Stock,行方向是Date列,列方向是Stock列

date.stock.matrix <- cast(prices, Date ~ Stock, value = 'Close')

#以上矩阵有缺失数据,先去prices删除缺失数据,然后重新生成矩阵

prices <- subset(prices, Date != ymd('2002-02-01'))

prices <- subset(prices, Stock != 'DDR')

date.stock.matrix <- cast(prices, Date ~ Stock, value = 'Close')

#做出相关性矩阵

cor.matrix <- cor(date.stock.matrix[, 2:ncol(date.stock.matrix)])

#转成一个数值向量

correlations <- as.numeric(cor.matrix)

#画图

ggplot(data.frame(Correlation = correlations),aes(x = Correlation, fill = 1)) +geom_density() + theme(legend.position = 'none')

#从图上看出,大部分相关性是正数,因此适合使用PCA

pca <- princomp(date.stock.matrix[, 2:ncol(date.stock.matrix)])

#看下pca第一主成分的载荷,并画图

principal.component <- pca$loadings[, 1]

loadings <- as.numeric(principal.component)

ggplot(data.frame(Loading = loadings), aes(x = Loading, fill = 1)) + geom_density() + theme(legend.position = 'none')

#从图上看,几乎全是负数

#预测一下

market.index <- predict(pca)[, 1]

#加载道琼斯指数

dji.prices <- read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\08-PCA\\data\\DJI.csv', stringsAsFactors = FALSE)

#把日期列转化一下

dji.prices <- transform(dji.prices, Date = ymd(Date))

#只拿一个子集看一下

dji.prices <- subset(dji.prices, Date > ymd('2001-12-31'))

dji.prices <- subset(dji.prices, Date != ymd('2002-02-01'))

#道琼斯数据集中数据好多,只拿收盘价和日期来看一下

dji <- with(dji.prices, rev(Close))

dates <- with(dji.prices, rev(Date))

#弄个数据集,MarketIndex是预测的

comparison <- data.frame(Date = dates, MarketIndex = market.index,DJI = dji)

ggplot(comparison, aes(x = MarketIndex, y = DJI)) +  geom_point() +  geom_smooth(method = 'lm', se = FALSE)

#从图上看,预测值和DJI指数负相关

comparison <- transform(comparison, MarketIndex = -1 * MarketIndex)

ggplot(comparison, aes(x = MarketIndex, y = DJI)) +  geom_point() +  geom_smooth(method = 'lm', se = FALSE)

#comparison有三列,日期,预测值,道琼斯指数

alt.comparison <- melt(comparison, id.vars = 'Date')

#melt后alt.comparison有三列,日期,类别(预测/道琼斯),价格

names(alt.comparison) <- c('Date', 'Index', 'Price')

ggplot(alt.comparison,aes(x = Date, y = Price, group = Index, color = Index)) +  geom_point() +  geom_line()

#图上看,预测值太低了,没法比对

#用scale把两部分数值放在同一刻度下

comparison <- transform(comparison, MarketIndex = scale(MarketIndex))

comparison <- transform(comparison, DJI = scale(DJI))

#重新melt

alt.comparison <- melt(comparison, id.vars = 'Date')

names(alt.comparison) <- c('Date', 'Index', 'Price')

#重新画图

ggplot(alt.comparison, aes(x = Date, y = Price, group = Index, color = Index)) +  geom_point() +  geom_line()

Machine Learning for hackers读书笔记(八)PCA:构建股票市场指数的更多相关文章

  1. Machine Learning for hackers读书笔记(七)优化:密码破译

    #凯撒密码:将每一个字母替换为字母表中下一位字母,比如a变成b. english.letters <- c('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' ...

  2. Machine Learning for hackers读书笔记(六)正则化:文本回归

    data<-'F:\\learning\\ML_for_Hackers\\ML_for_Hackers-master\\06-Regularization\\data\\' ranks < ...

  3. Machine Learning for hackers读书笔记(三)分类:垃圾邮件过滤

    #定义函数,打开每一个文件,找到空行,将空行后的文本返回为一个字符串向量,该向量只有一个元素,就是空行之后的所有文本拼接之后的字符串 #很多邮件都包含了非ASCII字符,因此设为latin1就可以读取 ...

  4. Machine Learning for hackers读书笔记_一句很重要的话

    为了培养一个机器学习领域专家那样的直觉,最好的办法就是,对你遇到的每一个机器学习问题,把所有的算法试个遍,直到有一天,你凭直觉就知道某些算法行不通.

  5. Machine Learning for hackers读书笔记(十二)模型比较

    library('ggplot2')df <- read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\12-Model_C ...

  6. Machine Learning for hackers读书笔记(十)KNN:推荐系统

    #一,自己写KNN df<-read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\10-Recommendations\\ ...

  7. Machine Learning for hackers读书笔记(九)MDS:可视化地研究参议员相似性

    library('foreign') library('ggplot2') data.dir <- file.path('G:\\dataguru\\ML_for_Hackers\\ML_for ...

  8. Machine Learning for hackers读书笔记(五)回归模型:预测网页访问量

    线性回归函数 model<-lm(Weight~Height,data=?) coef(model):得到回归直线的截距 predict(model):预测 residuals(model):残 ...

  9. Machine Learning for hackers读书笔记(四)排序:智能收件箱

    #数据集来源http://spamassassin.apache.org/publiccorpus/ #加载数据 library(tm)library(ggplot2)data.path<-'F ...

随机推荐

  1. linux用户配置和用户权限

    一.查看用户: (1)在终端里.输入:cat /etc/passwd,查看/etc/passwd文件就行了.(2)看第三个参数:500以上的,就是后面建的用户了.其它则为系统的用户. 查看当前在线用户 ...

  2. Mac下配置Maven

    1.Java环境变量设置就不说. 但是配置Maven需要检查下Java环境变量的设置.需要检查JAVA_HOME环境变量以及Java命令 wanyakundeMacBook-Pro:Library w ...

  3. ios设备突破微信小视频6S限制的方法

    刷微信朋友圈只发文字和图片怎能意犹未竟,微信小视频是一个很好的补充,音视频到位,流行流行最流行.但小视频时长不能超过6S,没有滤镜等是很大的遗憾.but有人突破限制玩出了花样,用ios设备在朋友圈晒出 ...

  4. 13test07;字符排序,去重,三三输出

    #include<iostream> #include<string> using namespace std; void buddle(char*,int);//对输入字符的 ...

  5. POJ 2186

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22189   Accepted: 9076 Des ...

  6. 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 3(Sorting/Searching)

    第一题:340 - Master-Mind Hints UVA:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Item ...

  7. c#百分比

    <div class="inner" style="width:@string.Format("{0:P1}", item.Maturitys) ...

  8. ExtJs之Ext.each

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  9. Java 网络编程(一)

    网络通讯要素 IP地址(InetAddress) 端口号 传输协议 图示: TDP/IP模型 以OSI 7层模型讲解数据的传输 InetAddress   InetAddress类主要表示IP地址. ...

  10. 多线程包:java.util.concurrent,

    Java1.5提供了一个非常高效实用的多线程包:java.util.concurrent,