<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>协整 on 格物堂</title><link>https://gewutang.cn/tags/%E5%8D%8F%E6%95%B4/</link><description>Recent content in 协整 on 格物堂</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Thu, 19 Jan 2012 00:00:00 +0000</lastBuildDate><atom:link href="https://gewutang.cn/tags/%E5%8D%8F%E6%95%B4/index.xml" rel="self" type="application/rss+xml"/><item><title>R语言书籍的学习路线图</title><link>https://gewutang.cn/2012/01/19/how-to-learn-r-by-reading-books/</link><pubDate>Thu, 19 Jan 2012 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2012/01/19/how-to-learn-r-by-reading-books/</guid><description>&lt;p&gt;现在对R感兴趣的人越来越多，很多人都想快速地掌握R。然而，由于目前大部分高校都没有开设R课程，这就导致很多人不知道如何着手学习R。&lt;/p&gt;&#10;&lt;p&gt;对于初学R，最常见的方式是看看百度上的简介。遇到不会的地方，就跑到论坛上吼一嗓子，然后欣欣然（得到解答）或者悲伤（未得到解答）地离去。一直到遇到下一个问题再回来。当然，这不是最好的学习方式。最好的方式是看书。&lt;/p&gt;&#10;&lt;p&gt;目前，市面上介绍R语言的书籍已经很多，中英文都有。那么，众多书籍中，一个菜鸟应该从哪一本入门呢？入门之后如何才能把自己练就成某个方面的高手呢？相信这是很多人心中的疑问。有这种疑问的人有福了，因为笔者将根据自己的经历总结一下R书籍的学习路线图以使大家少走些弯路。&lt;/p&gt;&#10;&lt;p&gt;本文分为6个部分，分别介绍初级入门、高级入门、绘图与可视化、计量经济学、时间序列分析和金融等。&lt;/p&gt;&#10;&lt;h4 id="1初级入门"&gt;&lt;a href="#1%e5%88%9d%e7%ba%a7%e5%85%a5%e9%97%a8" class="header-anchor"&gt;&lt;/a&gt;1.初级入门&#10;&lt;/h4&gt;&lt;p&gt;&lt;a class="link" href="http://cran.r-project.org/doc/manuals/R-intro.pdf" target="_blank" rel="noopener"&#10; &gt;《An Introduction to R》&lt;/a&gt;，这是R官网公布的入门小册子，其有中文版，由丁国徽翻译的，译名是&lt;a class="link" href="http://cran.r-project.org/doc/contrib/Ding-R-intro_cn.pdf" target="_blank" rel="noopener"&#10; &gt;《R导论》&lt;/a&gt;。&lt;a class="link" href="http://cran.r-project.org/doc/contrib/Paradis-rdebuts_en.pdf" target="_blank" rel="noopener"&#10; &gt;《R4Beginners》&lt;/a&gt;，这本小册子有中文版应该叫&lt;a class="link" href="http://www.biosino.org/R/R-doc/files/R4beg_cn_2.0.pdf" target="_blank" rel="noopener"&#10; &gt;《R入门》&lt;/a&gt;。除此之外，还可以去读&lt;a class="link" href="http://www.bjt.name/" target="_blank" rel="noopener"&#10; &gt;刘思喆&lt;/a&gt;的&lt;a class="link" href="http://cos.name/cn/topic/7673" target="_blank" rel="noopener"&#10; &gt;《153分钟学会R》&lt;/a&gt;，对R有一个感性的认识，这个册子收集了R初学者提问频率最高的153个问题。为什么叫153分钟呢？因为最初作者写了153个问题，阅读一个问题花费1分钟时间，全局下来也就是153分钟了。有了这些基础之后，要去读一些经典书籍比较全面的入门书籍，比如《统计建模与R软件》，国外还有《R Cookbook》和《R in action》（中文翻译为《R实战》）等。&lt;/p&gt;&#10;&lt;p&gt;最后隆重推荐《R in a Nutshell》，直译是“果壳里面的R”。当然，这是开玩笑的，”in a Nutshell“是英文俚语，意思是“简单的说”。很多技术类书籍都有Nutshell版本，这类书籍一般翻译为”**核心技术手册“，目前，我们已经译好这本书的中文版，预计今年会面世。这本书很不错，深度和广度都超越同类书籍，大家可以从现在开始期待，并广而告知一下！&lt;/p&gt;&#10;&lt;h4 id="2高级入门"&gt;&lt;a href="#2%e9%ab%98%e7%ba%a7%e5%85%a5%e9%97%a8" class="header-anchor"&gt;&lt;/a&gt;2.高级入门&#10;&lt;/h4&gt;&lt;p&gt;读了上述书籍之后，你就可以去高级入门阶段了。这时候要读的书有两本很经典的。《Statistics with R》和《The R book》以及《Modern Statistics with S》。之所以说这三本书高级，是因为这三本书已经不再限于R了，而是结合了数据分析的各种常见方法来写就的，比较系统的介绍了R在线性回归、方差分析、多元统计、R绘图、时间序列分析、数据挖掘等各方面的内容。看完之后你会发现，哇，原来R能做的事情这么多，而且做起来是那么简洁。&lt;/p&gt;&#10;&lt;p&gt;读到这里已经差不多了，剩下的估计就是你要专门攻读的某个方面内容了。下面大致说一说。&lt;/p&gt;&#10;&lt;h4 id="3绘图与可视化"&gt;&lt;a href="#3%e7%bb%98%e5%9b%be%e4%b8%8e%e5%8f%af%e8%a7%86%e5%8c%96" class="header-anchor"&gt;&lt;/a&gt;3.绘图与可视化&#10;&lt;/h4&gt;&lt;p&gt;亚里斯多德说，“较其他感觉而言，人类更喜欢观看”。因此，绘图和可视化得到很多人的关注和重视。那么，如何学习R画图和数据可视化呢？再简单些，如何画直方图？如何往直方图上添加密度曲线呢？我想读完下面这几本书你就大致会明白了。&lt;/p&gt;&#10;&lt;p&gt;首先，画图入门可以读《R Graphics》，个人认为这本是比较经典的，全面介绍了R中绘图系统。该书对应的有一个网站，google之就可以了。更深入的可以读《Lattice：Multivariate Data Visualization with R》，Lattice是R中针对多元数据可视化而开发的一套绘图系统。上面这些都是比较普通的。对应的有比较文艺和优雅的ggplot2系统，看《ggplot2：Elegant Graphics for Data Analysis》，中文版是《ggplot2:数据分析与图形艺术》。这本书理论性较强，实战比较弱。与之互补的要推荐《The R Graphics cookbook》，中文对应的是《R图形可视化手册》，译者是邓一硕、魏太云和肖楠，这本书详细列举了所有常见数据分析图形的绘制方法，适合做工具书。再有就是交互式图形的书了，著名的交互系统是ggobi，这个我已经喜欢多年，关于ggobi的书有《Interactive and Dynamic Graphics for Data Analysis With R and GGobi》，不过，也只是适宜入门，更多更全面的还是去ggobi的主页吧，上面有各种资料以及包的更新信息。&lt;/p&gt;&#10;&lt;p&gt;此外，R还推出了shiny和rChart以及rechart等包，可以将图形绘制到网页上，这个可以说是未来的主流，不得不关注。&lt;/p&gt;&#10;&lt;p&gt;特别推荐一下，中文版绘图书籍中谢益辉的《现代统计图形》和我们翻译的《R图形可视化手册》。&lt;/p&gt;&#10;&lt;h4 id="4计量经济学"&gt;&lt;a href="#4%e8%ae%a1%e9%87%8f%e7%bb%8f%e6%b5%8e%e5%ad%a6" class="header-anchor"&gt;&lt;/a&gt;4.计量经济学&#10;&lt;/h4&gt;&lt;p&gt;关于计量经济学，首先推荐一本很薄的小册子:《Econometrics In R》，做入门用。然后，是《Applied Econometrics with R》，该书对应的R包是AER包，可以安装之后配合使用，效果甚佳。计量经济学中很大一部分是关于时间序列分析的，这一块内容在下面的地方说。&lt;/p&gt;&#10;&lt;h4 id="5时间序列分析"&gt;&lt;a href="#5%e6%97%b6%e9%97%b4%e5%ba%8f%e5%88%97%e5%88%86%e6%9e%90" class="header-anchor"&gt;&lt;/a&gt;5.时间序列分析&#10;&lt;/h4&gt;&lt;p&gt;时间序列书籍的书籍分两类，一种是比较普适的书籍，典型的代表是：《Time Series Analysis and Its Applications ：with R examples》。该书介绍了各种时间序列分析的经典方法及实现各种经典方法的R代码，该书有中文版。如果不想买的话，建议去作者主页直接下载，英文版读起来很简单。时间序列分析中有一大块儿是关于金融时间序列分析的。这方面比较流行的书有两本《Analysis of financial time series》，这本书的最初是用的S-plus代码，不过新版已经以R代码为主了。这本书适合有时间序列分析基础和金融基础的人来看，因为书中关于时间序列分析的理论以及各种金融知识讲解的不是特别清楚，将极值理论计算VaR的部分就比较难看懂。另外一个比较有意思的是Rmetrics推出的《TimeSeries FAQ》，这本书是金融时间序列入门的东西，讲的很基础，但是很难懂。对应的中文版有《金融时间序列分析常见问题集》。经济领域的时间序列有一种特殊的情况叫协整，很多人很关注这方面的理论，关心这个的可以看《Analysis of Integrated and Cointegrated Time Series with R》。最后，比较高级的一本书是关于小波分析的，看《Wavelet Methods in Statistics with R》。附加一点，关于时间序列聚类的书籍目前比较少见，是一个处女地，有志之士可以开垦。&lt;/p&gt;&#10;&lt;h4 id="6金融"&gt;&lt;a href="#6%e9%87%91%e8%9e%8d" class="header-anchor"&gt;&lt;/a&gt;6.金融&#10;&lt;/h4&gt;&lt;p&gt;金融的领域很广泛，如果是大金融的话，保险也要被纳入此间。用R做金融更多地需要掌握的是金融知识，只会数据分析技术意义寥寥。我觉得这些书对于懂金融、不同数据分析技术的人比较有用，只懂数据分析技术而不动金融知识的人看起来肯定如雾里看花，甚至有人会觉得金融分析比较低级。这方面比较经典的书籍有：《Advanced Topics in Analysis of Economic and Financial Data Using R》以及《Modelling Financial Time Series With S-plus》。金融产品定价之类的常常要用到随机微分方程，有一本叫《Simulation Inference Stochastic Differential Equations：with R examples》的书是关于这方面的内容的，有实例，内容详实。此外，风险度量与管理类比较经典的有《Simulation Techniques in Financial Risk Management》、《Modern Actuarial Risk Theory Using R》和《Quantitative Risk Management：Concepts, Techniques and Tools》。投资组合分析类和期权定价类可以分别看《Portfolio Optimization with R》和《Option Pricing and Estimation of Financial Models with R》。&lt;/p&gt;&#10;&lt;h4 id="7数据挖掘"&gt;&lt;a href="#7%e6%95%b0%e6%8d%ae%e6%8c%96%e6%8e%98" class="header-anchor"&gt;&lt;/a&gt;7.数据挖掘&#10;&lt;/h4&gt;&lt;p&gt;这方面的书有《Data Mining with R:learing with case studies》，《Machine learning for hacks》以及不过《Data Mining with Rattle and R》，书中主要讲了基于Rattle包进行数据挖掘的操作方法，这本书是罕有的全流程数据挖掘工具书，书中详细讲解了从导入数据、清洗数据、数据重整、数据可视化、模型构建、变量选择、模型评价等各个数据挖掘环节，是学习数据挖掘的不二之选。当然，个人比较喜欢Rattle，这本书对应的中文版是《Rattle:R中的数据挖掘GUI》，是由邓一硕、郝智恒、谭丽等翻译的。除了Rattle，Rweka做数据挖掘也很棒。R中数据挖掘方面的包已经足够多了，参考包中的帮助文档就足够了。&lt;/p&gt;&#10;</description></item><item><title>用R检验股票的协整关系</title><link>https://gewutang.cn/2011/10/27/use-r-to-test-cointegration/</link><pubDate>Thu, 27 Oct 2011 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2011/10/27/use-r-to-test-cointegration/</guid><description>&lt;h2 id="需要用到的r包"&gt;&lt;a href="#%e9%9c%80%e8%a6%81%e7%94%a8%e5%88%b0%e7%9a%84r%e5%8c%85" class="header-anchor"&gt;&lt;/a&gt;需要用到的R包&#10;&lt;/h2&gt;&lt;p&gt;xts和zoo是两个强大的处理时序对象的R包。xts包本身又相当于是一个加强版的zoo包。zoo包处理的对象是zoo，zoo对象与数据框（dataframe)类似，由行列构成。不同的是，zoo对象比数据框多了一个时间戳。&lt;/p&gt;&#10;&lt;p&gt;假设我们有一个zoo对象t，index(t)返回时间戳，也就是一个表示日期的向量。start(t)和end（t）可以查看zoo对象的起始值和末端值。&lt;/p&gt;&#10;&lt;h2 id="数据预处理"&gt;&lt;a href="#%e6%95%b0%e6%8d%ae%e9%a2%84%e5%a4%84%e7%90%86" class="header-anchor"&gt;&lt;/a&gt;数据预处理&#10;&lt;/h2&gt;&lt;p&gt;分四个步骤：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;读入数据（data frame格式）&lt;/li&gt;&#10;&lt;li&gt;将日期转化为Date对象&lt;/li&gt;&#10;&lt;li&gt;将data frame数据转换为zoo对象&lt;/li&gt;&#10;&lt;li&gt;合并数据&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="相应的r代码"&gt;&lt;a href="#%e7%9b%b8%e5%ba%94%e7%9a%84r%e4%bb%a3%e7%a0%81" class="header-anchor"&gt;&lt;/a&gt;相应的R代码：&#10;&lt;/h2&gt;&lt;pre&gt;&lt;code class="language-r"&gt;library(quantmod) # 金融数据获取&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：xts&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：zoo&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## 载入程序包：'zoo'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from 'package:base':&#10;## &#10;## as.Date, as.Date.numeric&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：TTR&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by 'quantmod':&#10;## method from&#10;## as.zoo.data.frame zoo&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;library(urca) # 协整检验&#10;library(ggplot2) # 可视化&#10;&#10;# 获取2010-2011年股票数据&#10;getSymbols(c(&amp;quot;AAPL&amp;quot;,&amp;quot;MSFT&amp;quot;))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot; &amp;quot;MSFT&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 提取收盘价并转换为对数序列&#10;aapl &amp;lt;- log(Ad(AAPL$AAPL.Adjusted))&#10;msft &amp;lt;- log(Ad(MSFT$MSFT.Adjusted))&#10;&#10;# 创建合并数据集&#10;prices &amp;lt;- merge(aapl, msft)&#10;colnames(prices) &amp;lt;- c(&amp;quot;log_AAPL&amp;quot;, &amp;quot;log_MSFT&amp;quot;)&#10;&#10;# 绘制对数价格趋势&#10;ggplot(prices, aes(x=Index)) +&#10; geom_line(aes(y=log_AAPL, color=&amp;quot;苹果&amp;quot;)) +&#10; geom_line(aes(y=log_MSFT, color=&amp;quot;微软&amp;quot;)) +&#10; labs(title=&amp;quot;苹果与微软对数收盘价序列&amp;quot;, y=&amp;quot;log(Price)&amp;quot;, x=&amp;quot;日期&amp;quot;) +&#10; scale_color_manual(values=c(&amp;quot;#FF6B6B&amp;quot;, &amp;quot;#4ECDC4&amp;quot;)) +&#10; theme_minimal(base_family = &amp;quot;PingFang SC&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2011/10/27/use-r-to-test-cointegration/index_files/figure-html/unnamed-chunk-1-1.png" width="672" /&gt;&#10;&lt;h2 id="平稳性检验"&gt;&lt;a href="#%e5%b9%b3%e7%a8%b3%e6%80%a7%e6%a3%80%e9%aa%8c" class="header-anchor"&gt;&lt;/a&gt;平稳性检验&#10;&lt;/h2&gt;&lt;pre&gt;&lt;code class="language-r"&gt;# 原始序列检验&#10;# 苹果价格ADF检验&#10;summary(ur.df(prices$log_AAPL, type=&amp;quot;drift&amp;quot;, lags=5)) #P-value大于0.05则序列不平稳&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## ############################################### &#10;## # Augmented Dickey-Fuller Test Unit Root Test # &#10;## ############################################### &#10;## &#10;## Test regression drift &#10;## &#10;## &#10;## Call:&#10;## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)&#10;## &#10;## Residuals:&#10;## Min 1Q Median 3Q Max &#10;## -0.197346 -0.008942 0.000164 0.010330 0.141513 &#10;## &#10;## Coefficients:&#10;## Estimate Std. Error t value Pr(&amp;gt;|t|) &#10;## (Intercept) 0.0016271 0.0008139 1.999 0.0457 *&#10;## z.lag.1 -0.0002047 0.0002258 -0.907 0.3646 &#10;## z.diff.lag1 -0.0321993 0.0147383 -2.185 0.0290 *&#10;## z.diff.lag2 -0.0155993 0.0147150 -1.060 0.2892 &#10;## z.diff.lag3 -0.0090648 0.0147162 -0.616 0.5379 &#10;## z.diff.lag4 0.0250380 0.0147149 1.702 0.0889 .&#10;## z.diff.lag5 0.0145020 0.0147113 0.986 0.3243 &#10;## ---&#10;## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1&#10;## &#10;## Residual standard error: 0.01992 on 4600 degrees of freedom&#10;## Multiple R-squared: 0.00234,&#9;Adjusted R-squared: 0.001039 &#10;## F-statistic: 1.798 on 6 and 4600 DF, p-value: 0.09538&#10;## &#10;## &#10;## Value of test-statistic is: -0.9067 5.4705 &#10;## &#10;## Critical values for test statistics: &#10;## 1pct 5pct 10pct&#10;## tau2 -3.43 -2.86 -2.57&#10;## phi1 6.43 4.59 3.78&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 微软价格ADF检验&#10;summary(ur.df(prices$log_MSFT, type=&amp;quot;drift&amp;quot;, lags=5)) &#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## ############################################### &#10;## # Augmented Dickey-Fuller Test Unit Root Test # &#10;## ############################################### &#10;## &#10;## Test regression drift &#10;## &#10;## &#10;## Call:&#10;## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)&#10;## &#10;## Residuals:&#10;## Min 1Q Median 3Q Max &#10;## -0.148299 -0.008044 0.000121 0.008836 0.160776 &#10;## &#10;## Coefficients:&#10;## Estimate Std. Error t value Pr(&amp;gt;|t|) &#10;## (Intercept) 0.0001374 0.0010067 0.137 0.89142 &#10;## z.lag.1 0.0001571 0.0002344 0.670 0.50269 &#10;## z.diff.lag1 -0.1088675 0.0147498 -7.381 1.86e-13 ***&#10;## z.diff.lag2 -0.0421217 0.0148530 -2.836 0.00459 ** &#10;## z.diff.lag3 0.0054135 0.0148661 0.364 0.71576 &#10;## z.diff.lag4 -0.0431566 0.0148530 -2.906 0.00368 ** &#10;## z.diff.lag5 -0.0185213 0.0147761 -1.253 0.21010 &#10;## ---&#10;## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1&#10;## &#10;## Residual standard error: 0.01756 on 4600 degrees of freedom&#10;## Multiple R-squared: 0.01478,&#9;Adjusted R-squared: 0.0135 &#10;## F-statistic: 11.5 on 6 and 4600 DF, p-value: 8.16e-13&#10;## &#10;## &#10;## Value of test-statistic is: 0.6703 4.8403 &#10;## &#10;## Critical values for test statistics: &#10;## 1pct 5pct 10pct&#10;## tau2 -3.43 -2.86 -2.57&#10;## phi1 6.43 4.59 3.78&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 一阶差分平稳性验证&#10;# 差分处理&#10;diff_aapl &amp;lt;- diff(prices$log_AAPL)[-1]&#10;diff_msft &amp;lt;- diff(prices$log_MSFT)[-1]&#10;&#10;# 差分序列ADF检验&#10;summary(ur.df(diff_aapl, type=&amp;quot;none&amp;quot;)) &#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## ############################################### &#10;## # Augmented Dickey-Fuller Test Unit Root Test # &#10;## ############################################### &#10;## &#10;## Test regression none &#10;## &#10;## &#10;## Call:&#10;## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)&#10;## &#10;## Residuals:&#10;## Min 1Q Median 3Q Max &#10;## -0.197910 -0.007903 0.001180 0.011336 0.140719 &#10;## &#10;## Coefficients:&#10;## Estimate Std. Error t value Pr(&amp;gt;|t|) &#10;## z.lag.1 -1.04076 0.02112 -49.286 &amp;lt;2e-16 ***&#10;## z.diff.lag 0.01349 0.01473 0.916 0.36 &#10;## ---&#10;## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1&#10;## &#10;## Residual standard error: 0.01999 on 4608 degrees of freedom&#10;## Multiple R-squared: 0.5134,&#9;Adjusted R-squared: 0.5131 &#10;## F-statistic: 2430 on 2 and 4608 DF, p-value: &amp;lt; 2.2e-16&#10;## &#10;## &#10;## Value of test-statistic is: -49.2864 &#10;## &#10;## Critical values for test statistics: &#10;## 1pct 5pct 10pct&#10;## tau1 -2.58 -1.95 -1.62&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;summary(ur.df(diff_msft, type=&amp;quot;none&amp;quot;)) &#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## ############################################### &#10;## # Augmented Dickey-Fuller Test Unit Root Test # &#10;## ############################################### &#10;## &#10;## Test regression none &#10;## &#10;## &#10;## Call:&#10;## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)&#10;## &#10;## Residuals:&#10;## Min 1Q Median 3Q Max &#10;## -0.149197 -0.007368 0.000777 0.009507 0.165463 &#10;## &#10;## Coefficients:&#10;## Estimate Std. Error t value Pr(&amp;gt;|t|) &#10;## z.lag.1 -1.14697 0.02190 -52.367 &amp;lt; 2e-16 ***&#10;## z.diff.lag 0.03991 0.01475 2.705 0.00685 ** &#10;## ---&#10;## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1&#10;## &#10;## Residual standard error: 0.01758 on 4608 degrees of freedom&#10;## Multiple R-squared: 0.5521,&#9;Adjusted R-squared: 0.5519 &#10;## F-statistic: 2840 on 2 and 4608 DF, p-value: &amp;lt; 2.2e-16&#10;## &#10;## &#10;## Value of test-statistic is: -52.3667 &#10;## &#10;## Critical values for test statistics: &#10;## 1pct 5pct 10pct&#10;## tau1 -2.58 -1.95 -1.62&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h2 id="协整检验"&gt;&lt;a href="#%e5%8d%8f%e6%95%b4%e6%a3%80%e9%aa%8c" class="header-anchor"&gt;&lt;/a&gt;协整检验&#10;&lt;/h2&gt;&lt;p&gt;我们用Engle-Granger两步法进行协整检验。&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 第一步：构建回归模型&#10;coint_model &amp;lt;- lm(log_AAPL ~ log_MSFT, data=prices)&#10;summary(coint_model) # 显示回归系数与R-squared&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Call:&#10;## lm(formula = log_AAPL ~ log_MSFT, data = prices)&#10;## &#10;## Residuals:&#10;## Min 1Q Median 3Q Max &#10;## -1.20523 -0.17828 0.03798 0.22297 0.77306 &#10;## &#10;## Coefficients:&#10;## Estimate Std. Error t value Pr(&amp;gt;|t|) &#10;## (Intercept) -1.343406 0.020650 -65.06 &amp;lt;2e-16 ***&#10;## log_MSFT 1.131850 0.004805 235.54 &amp;lt;2e-16 ***&#10;## ---&#10;## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1&#10;## &#10;## Residual standard error: 0.3607 on 4611 degrees of freedom&#10;## Multiple R-squared: 0.9233,&#9;Adjusted R-squared: 0.9233 &#10;## F-statistic: 5.548e+04 on 1 and 4611 DF, p-value: &amp;lt; 2.2e-16&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 提取残差序列&#10;residuals &amp;lt;- residuals(coint_model)&#10;&#10;# 第二步：残差平稳性检验&#10;adf_test &amp;lt;- ur.df(residuals, type=&amp;quot;none&amp;quot;, lags=5)&#10;print(adf_test@teststat) # ADF统计量&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## tau1&#10;## statistic -2.893062&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;print(adf_test@cval) # 临界值&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 1pct 5pct 10pct&#10;## tau1 -2.58 -1.95 -1.62&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;ADF统计量为-2.893062,小于1%置信水平对应的临界值-2.58，故拒绝原假设，说明残差序列平稳，两股票存在协整关系。&lt;/p&gt;&#10;&lt;h2 id="对协整关系进行可视化"&gt;&lt;a href="#%e5%af%b9%e5%8d%8f%e6%95%b4%e5%85%b3%e7%b3%bb%e8%bf%9b%e8%a1%8c%e5%8f%af%e8%a7%86%e5%8c%96" class="header-anchor"&gt;&lt;/a&gt;对协整关系进行可视化&#10;&lt;/h2&gt;&lt;pre&gt;&lt;code class="language-r"&gt;# 计算价差序列&#10;spread &amp;lt;- prices$log_AAPL - coint_model$coefficients[2] * prices$log_MSFT&#10;&#10;# 绘制价差通道&#10;ggplot(spread, aes(x=Index)) +&#10; geom_line(aes(y=spread), color=&amp;quot;#2C3E50&amp;quot;) +&#10; geom_hline(yintercept=mean(spread), linetype=&amp;quot;dashed&amp;quot;, color=&amp;quot;#E74C3C&amp;quot;) +&#10; geom_hline(yintercept=mean(spread)+2*sd(spread), color=&amp;quot;#27AE60&amp;quot;, alpha=0.5) +&#10; geom_hline(yintercept=mean(spread)-2*sd(spread), color=&amp;quot;#27AE60&amp;quot;, alpha=0.5) +&#10; labs(title=&amp;quot;协整价差序列与2σ通道&amp;quot;, y=&amp;quot;标准化价差&amp;quot;)+&#10; theme_minimal(base_family = &amp;quot;PingFang SC&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Don't know how to automatically pick scale for object of type &amp;lt;xts/zoo&amp;gt;.&#10;## Defaulting to continuous.&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2011/10/27/use-r-to-test-cointegration/index_files/figure-html/unnamed-chunk-4-1.png" width="672" /&gt;&#10;</description></item></channel></rss>