<?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/categories/%E8%AE%A1%E9%87%8F%E7%BB%8F%E6%B5%8E%E5%AD%A6/</link><description>Recent content in 计量经济学 on 格物堂</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Thu, 27 Oct 2011 00:00:00 +0000</lastBuildDate><atom:link href="https://gewutang.cn/categories/%E8%AE%A1%E9%87%8F%E7%BB%8F%E6%B5%8E%E5%AD%A6/index.xml" rel="self" type="application/rss+xml"/><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><item><title>R与单位根检验</title><link>https://gewutang.cn/2011/05/08/use-r-to-do-unitroots-test/</link><pubDate>Sun, 08 May 2011 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2011/05/08/use-r-to-do-unitroots-test/</guid><description>&lt;p&gt;R里面的fUnitRoots包可以轻松实现时间序列的单位根检验。两个主函数分别是adfTest和unitrootTest。对于以应用统计为主要目的的人而言，看懂单位根检验的结果可能比探讨单位根检验的原理更重要。&lt;/p&gt;&#10;&lt;p&gt;为了清晰地表明有单位根的序列的检验结果和无单位根的序列的检验结果之间的差别，在这里生成两个可对比的单位根序列来举例：&lt;/p&gt;&#10;&lt;p&gt;首先，载入fUnitRoots包：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;library(fUnitRoots);&#10;&#10;# 生成没有单位根的序列x和由单位根的序列y&#10;&#10;x &amp;lt;- rnorm(1000) &#10;y &amp;lt;- cumsum(c(0, x)) &#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h1 id="对序列x进行adf检验"&gt;&lt;a href="#%e5%af%b9%e5%ba%8f%e5%88%97x%e8%bf%9b%e8%a1%8cadf%e6%a3%80%e9%aa%8c" class="header-anchor"&gt;&lt;/a&gt;对序列x进行adf检验：&#10;&lt;/h1&gt;&lt;pre&gt;&lt;code class="language-r"&gt;adfTest(x)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Warning in adfTest(x): p-value smaller than printed p-value&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Title:&#10;## Augmented Dickey-Fuller Test&#10;## &#10;## Test Results:&#10;## PARAMETER:&#10;## Lag Order: 1&#10;## STATISTIC:&#10;## Dickey-Fuller: -21.1357&#10;## P VALUE:&#10;## 0.01 &#10;## &#10;## Description:&#10;## Mon May 5 17:44:36 2025 by user:&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;注意上述结果中的P值。&lt;/p&gt;&#10;&lt;p&gt;对序列y进行adf检验：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;adfTest(y) &#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Title:&#10;## Augmented Dickey-Fuller Test&#10;## &#10;## Test Results:&#10;## PARAMETER:&#10;## Lag Order: 1&#10;## STATISTIC:&#10;## Dickey-Fuller: 0.8508&#10;## P VALUE:&#10;## 0.8875 &#10;## &#10;## Description:&#10;## Mon May 5 17:44:36 2025 by user:&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;注意上述结果的p值，跟x序列的结果有什么区别？&lt;/p&gt;&#10;&lt;p&gt;对序列x和序列y进行单位根检验，观察下两者的区别。&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;unitrootTest(x)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Title:&#10;## Augmented Dickey-Fuller Test&#10;## &#10;## Test Results:&#10;## PARAMETER:&#10;## Lag Order: 1&#10;## STATISTIC:&#10;## DF: -21.1357&#10;## P VALUE:&#10;## t: &amp;lt; 2.2e-16 &#10;## n: 0.001252 &#10;## &#10;## Description:&#10;## Mon May 5 17:44:36 2025 by user:&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;unitrootTest(y)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Title:&#10;## Augmented Dickey-Fuller Test&#10;## &#10;## Test Results:&#10;## PARAMETER:&#10;## Lag Order: 1&#10;## STATISTIC:&#10;## DF: 0.8508&#10;## P VALUE:&#10;## t: 0.8938 &#10;## n: 0.8854 &#10;## &#10;## Description:&#10;## Mon May 5 17:44:36 2025 by user:&#10;&lt;/code&gt;&lt;/pre&gt;&#10;</description></item><item><title>用R进行Jarque-Bera检验</title><link>https://gewutang.cn/2011/05/06/use-r-to-do-jb-test/</link><pubDate>Fri, 06 May 2011 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2011/05/06/use-r-to-do-jb-test/</guid><description>&lt;p&gt;线性回归模型的检验是建立在假设随机误差 &lt;code&gt;\(u_t\)&lt;/code&gt; 服从正态分布的基础之上的。但问题是我们没有办法直接观察到真实的 &lt;code&gt;\(u_t\)&lt;/code&gt; ，那么，如何验证它是否服从正态分布呢？一般来说，会用残差 &lt;code&gt;\(e_t\)&lt;/code&gt; 近似表示 &lt;code&gt;\(u_t\)&lt;/code&gt; ，再通过 &lt;code&gt;\(e_t\)&lt;/code&gt; 的正态性来推断 &lt;code&gt;\(u_t\)&lt;/code&gt; 的正态性。&lt;/p&gt;&#10;&lt;p&gt;一种比较常用的正态性检验方法叫Jarque-Bera(雅克-贝拉)检验，简称JB检验。其思路大概是这样的：&lt;/p&gt;&#10;&lt;p&gt;首先，计算偏度系数 &lt;code&gt;\(s_{k}\)&lt;/code&gt; 和 峰度系数 &lt;code&gt;\(k\)&lt;/code&gt; ：&lt;/p&gt;&#10;&lt;p&gt;$$&#10;s_{k}=\frac{\frac{1}{n}\sum_{i=1}^{n}(x_{i}-\bar x)^{3}}{(\frac{1}{n}\sum_{i=1}^{n}(x_{i}-\bar x)^{2})^{\frac{3}{2}} }&#10;$$&lt;/p&gt;&#10;&lt;p&gt;$$&#10;\text{k}=\frac{\frac{1}{n}\sum_{i=1}^{n}(x_{i}-\bar x)^{4}}{(\frac{1}{n}\sum_{i=1}^{n}(x_{i}-\bar x)^{2})^{2} }-3&#10;$$&lt;/p&gt;&#10;&lt;p&gt;Jarqe和Bera建立了如下检验统计量：&lt;/p&gt;&#10;&lt;p&gt;$$&#10;\text{JB}=\frac{n}{6}(sk^{2}+\frac{k^{2}}{4})&#10;$$&lt;/p&gt;&#10;&lt;p&gt;在正态性分布的假定下，JB统计量渐近服从自由度为2的 &lt;code&gt;\(\chi^2\)&lt;/code&gt; 分布。&lt;/p&gt;&#10;&lt;p&gt;显然，如果变量服从正态分布，则S为0，K为0，因而JB统计量的值为零。如果变量不是服从正态分布的变量，则JB统计量将为一个逐渐增大值。而针对每一个JB统计量，都可以算出其对应的P值，根据P值和事先确定的置信水平便可以确定是否拒绝变量为正态分布的假设。&lt;/p&gt;&#10;&lt;p&gt;在R软件中，Jarque-Bera检验可以用tseries包中的jarque.bera.test来实现。我们这里虚构两个数据，第一个例子中的数据服从正态分布，第二例子中的数据是1到200的序列。&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;library(tseries)&#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;set.seed(123)&#10;x=rnorm(20)&#10;jb &amp;lt;- jarque.bera.test(x)&#10;jb&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## &#9;Jarque Bera Test&#10;## &#10;## data: x&#10;## X-squared = 0.081917, df = 2, p-value = 0.9599&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;返回结果中p-value等于0.9598692，大于0.05，因此在95%置信水平下，无法拒绝x服从正态分布的假设。&lt;/p&gt;&#10;&lt;p&gt;另外一个例子：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;library(tseries)&#10;x &amp;lt;- 1:200 #不服从正态分布&#10;jb &amp;lt;- jarque.bera.test(x)&#10;jb&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## &#9;Jarque Bera Test&#10;## &#10;## data: x&#10;## X-squared = 12.001, df = 2, p-value = 0.002477&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;返回结果中p-value等于0.0024773，远小于0.05，因此在95%置信水平下，拒绝x服从正态分布的假设（这是显而易见的）。&lt;/p&gt;&#10;</description></item><item><title>用R拟合缺项的ARIMA模型</title><link>https://gewutang.cn/2011/04/17/use-r-to-fit-sparse-arma-model/</link><pubDate>Sun, 17 Apr 2011 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2011/04/17/use-r-to-fit-sparse-arma-model/</guid><description>&lt;p&gt;标准的ARMA（p,q）模型是这样的：&lt;/p&gt;&#10;&lt;p&gt;$$&#10;X_t=a_1X_{t-1}+\ldots+a_pX_{t-p}+\epsilon_t+b_1\epsilon_{t-1}+\ldots+b_q\epsilon_{t-q}&#10;$$&lt;/p&gt;&#10;&lt;p&gt;比如，ARMA(3,2)的形式如下：&lt;/p&gt;&#10;&lt;p&gt;$$&#10;X_t=a_1X_{t-1}+a_2X_{t-2}+a_3X_{t-3}+\epsilon_t+b_1\epsilon_{t-1}+b_2\epsilon_{t-2}&#10;$$&lt;/p&gt;&#10;&lt;p&gt;拟合ARMA（3,2）模型的R代码如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;arima(lh, order = c(3,0,2))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;运行结果如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;Call:&#10;arima(x = lh, order = c(3, 0, 2))&#10;&#10;Coefficients:&#10; ar1 ar2 ar3 ma1 ma2 intercept&#10; 0.0402 0.4567 -0.4164 0.6602 -0.1109 2.3911&#10;s.e. 0.3952 0.2032 0.2310 0.4403 0.3977 0.1010&#10;&#10;sigma^2 estimated as 0.1711: log likelihood = -26.2, aic = 66.4&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;代码清晰易懂。order=c(3,0,2)中的3便是模型中的p，2是模型中的q。中间的0是序列X在建立ARMA模型前的差分次数。&lt;/p&gt;&#10;&lt;p&gt;有时候，我们不需要拟合完整的ARMA模型。比如，要拟合的模型如下：&lt;/p&gt;&#10;&lt;p&gt;$$&#10;X_t=a_1X_{t-1}+a_3X_{t-3}+\epsilon_t+b_1\epsilon_{t-1}+b_2\epsilon_{t-2}&#10;$$&lt;/p&gt;&#10;&lt;p&gt;虽然模型的最高阶依然是（3,2）可是AR项里面缺少了第二项。其实，这个问题解决起来很简单的。分3步：&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;观察完整的ARMA模型有几个参数，上例中共有6个参数。&lt;/li&gt;&#10;&lt;li&gt;观察缺失项的参数在原模型参数中的位置，上例中缺失项是6个参数中的第2个参数。&lt;/li&gt;&#10;&lt;li&gt;在arima()函数中，加入fixed=c(NA,0,NA,NA,NA,NA)，transform.pars = FALSE。&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;上例中的R代码如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;arima(lh, order = c(3,0,2),fixed=c(NA,0,NA,NA,NA,NA),transform.pars = FALSE)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;返回结果如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;Call:&#10;arima(x = lh, order = c(3, 0, 2), transform.pars = FALSE, fixed = c(NA, 0, NA, &#10; NA, NA, NA))&#10;&#10;Coefficients:&#10; ar1 ar2 ar3 ma1 ma2 intercept&#10; 0.4367 0 -0.2255 0.2294 0.0742 2.3935&#10;s.e. 0.4278 0 0.1560 0.4537 0.2919 0.1008&#10;&#10;sigma^2 estimated as 0.1776: log likelihood = -26.97, aic = 65.93&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;arima()中fixed=c(NA,0,NA,NA,NA,NA)的长度为6，与不缺项的ARMA（3,2）模型的参数个数相同。第二个参数为0，意思是把6个参数中的第二个参数固定为0。transform.pars = FALSE是一个辅助项。&lt;/p&gt;&#10;&lt;p&gt;当然了，如果你愿意，也可以将参数固定为其它数值，比如0.5,0.6之类的，这些都无伤大雅。&lt;/p&gt;&#10;</description></item><item><title>CPI数据经过调整吗</title><link>https://gewutang.cn/2011/02/20/is-cpi-true/</link><pubDate>Sun, 20 Feb 2011 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2011/02/20/is-cpi-true/</guid><description>&lt;p&gt;不久前，2011年1月份的CPI数据出炉。这个引人瞩目的数据低于人们对于1月份CPI破5的预期，尴尬地站在4.9的位置上。坊间纷纷传言说统计局人为调低食品类权重而达到了此效果。孰真孰假？不妨来算一下。&lt;/p&gt;&#10;&lt;p&gt;国家统计局公布的居民价格指数的如数据1：（统计局所公布的2010年1月至11月的数据）：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;Index y x1 x2 x3 x4 x5 x6 x7 x8&#10;1 101.5 103.7 101.5 99.6 98.9 102.3 99.5 98.8 102.5&#10;2 102.7 106.2 101.6 98.7 99.2 102.4 100.1 100.8 103.0&#10;3 102.4 105.2 101.7 98.9 99.3 102.5 100.0 100.3 103.3&#10;4 102.8 105.9 101.7 98.7 99.5 102.8 100.0 100.4 104.5&#10;5 103.1 106.1 101.7 98.8 99.7 103.2 100.1 100.6 105.0&#10;6 102.9 105.7 101.7 99.0 100.0 103.2 99.7 100.9 105.0&#10;7 103.3 106.8 101.6 99.2 100.2 103.3 99.3 101.1 104.8&#10;8 103.5 107.5 101.5 98.8 100.4 103.3 99.4 101.2 104.4&#10;9 103.6 108.0 101.4 98.5 100.4 103.4 99.3 101.2 104.3&#10;10 104.4 110.1 101.5 98.7 100.5 103.7 99.5 100.9 104.9&#10;11 105.1 111.7 101.6 99.3 100.7 104.0 99.3 100.6 105.8&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;各符号的含义如下：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;居民消费价格指数:y&lt;/li&gt;&#10;&lt;li&gt;食品:x1&lt;/li&gt;&#10;&lt;li&gt;烟酒及用品:x2&lt;/li&gt;&#10;&lt;li&gt;衣着:x3&lt;/li&gt;&#10;&lt;li&gt;家庭设备用品及服务:x4&lt;/li&gt;&#10;&lt;li&gt;医疗保健及个人用品:x5&lt;/li&gt;&#10;&lt;li&gt;交通和通信:x6&lt;/li&gt;&#10;&lt;li&gt;娱乐教育文化用品及服务:x7&lt;/li&gt;&#10;&lt;li&gt;居住:x8&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;八大类的权数分别为：a,b,c,d,e,f,g,h。&lt;/p&gt;&#10;&lt;p&gt;将上述数据减去100，得到数据2：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;Index y x1 x2 x3 x4 x5 x6 x7 x8&#10;1 1.5 3.7 1.5 -0.4 -1.1 2.3 -0.5 -1.2 2.5&#10;2 2.7 6.2 1.6 -1.3 -0.8 2.4 0.1 0.8 3.0&#10;3 2.4 5.2 1.7 -1.1 -0.7 2.5 0.0 0.3 3.3&#10;4 2.8 5.9 1.7 -1.3 -0.5 2.8 0.0 0.4 4.5&#10;5 3.1 6.1 1.7 -1.2 -0.3 3.2 0.1 0.6 5.0&#10;6 2.9 5.7 1.7 -1.0 0.0 3.2 -0.3 0.9 5.0&#10;7 3.3 6.8 1.6 -0.8 0.2 3.3 -0.7 1.1 4.8&#10;8 3.5 7.5 1.5 -1.2 0.4 3.3 -0.6 1.2 4.4&#10;9 3.6 8.0 1.4 -1.5 0.4 3.4 -0.7 1.2 4.3&#10;10 4.4 10.1 1.5 -1.3 0.5 3.7 -0.5 0.9 4.9&#10;11 5.1 11.7 1.6 -0.7 0.7 4.0 -0.7 0.6 5.8&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;数据2中各变量之间的关系如下：&lt;/p&gt;&#10;&lt;p&gt;$$&#10;a·x1+b·x2+c·x3+d·x4+e·x5+f·x6+g·×7+h·x8=y&#10;$$&#10;$$&#10;a+b+c+d+e+f=1&#10;$$&lt;/p&gt;&#10;&lt;p&gt;由于a,b,c,d,e,f,g,h一直不为人知。因此，我们用2010年1月至11月的数据对其进行估计。同时，为了保证估计出的系数和为1，我们将估计方程设定为如下形式：&lt;/p&gt;&#10;&lt;p&gt;$$&#10;(y-x8)\sim (x1-x8)+(x12-x8)+(x3-x8)+(x4-x8)+(x5-x8)+(x6-x8)+(x7-x8)&#10;$$&#10;估计模型所用的数据为数据3：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;Index y x1 x2 x3 x4 x5 x6 x7&#10;1 -1.0 1.2 -1.0 -2.9 -3.6 -0.2 -3.0 -3.7&#10;2 -0.3 3.2 -1.4 -4.3 -3.8 -0.6 -2.9 -2.2&#10;3 -0.9 1.9 -1.6 -4.4 -4.0 -0.8 -3.3 -3.0&#10;4 -1.7 1.4 -2.8 -5.8 -5.0 -1.7 -4.5 -4.1&#10;5 -1.9 1.1 -3.3 -6.2 -5.3 -1.8 -4.9 -4.4&#10;6 -2.1 0.7 -3.3 -6.0 -5.0 -1.8 -5.3 -4.1&#10;7 -1.5 2.0 -3.2 -5.6 -4.6 -1.5 -5.5 -3.7&#10;8 -0.9 3.1 -2.9 -5.6 -4.0 -1.1 -5.0 -3.2&#10;9 -0.7 3.7 -2.9 -5.8 -3.9 -0.9 -5.0 -3.1&#10;10 -0.5 5.2 -3.4 -6.2 -4.4 -1.2 -5.4 -4.0&#10;11 -0.7 5.9 -4.2 -6.5 -5.1 -1.8 -6.5 -5.2&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;估计的结果如下:&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;Call:&#10;lm(formula = y ~ ., data = data)&#10;&#10;Coefficients:&#10;(Intercept) x1 x2 x3 x4&#10;0.59960 0.32473 -0.23855 0.14522 0.13161&#10;x5 x6 x7&#10;0.09067 0.26724 0.13645&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;得到八大类的权重为：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;a=0.324733&#10;b=-0.238554&#10;c=0.145224&#10;d=0.131614&#10;e=0.090674&#10;f=0.267241&#10;g=0.136449&#10;h=0.142618&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;2011年1月份八大类的数据分别是：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;x1 x2 x3 x4 x5 x6 x7 x8&#10;110.3 101.8 99.8 101.4 103.2 99.9 101.0 106.8&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;用predict()函数进行预测，得到2011年1月的CPI数据应为5.04，95%置信水平下的预测区间为[3.23, 6.85]。计算结果表明似乎没有足够的理由来证明统计局通过调整权重而导致CPI的明显调低。&lt;/p&gt;&#10;&lt;p&gt;唯一可以确认的事实在于：民众“认为”CPI数据被明显调低了，且是无充足理由的“认为”。民众的这种信念暴露了一种可怕的讯号。&lt;/p&gt;&#10;&lt;p&gt;有一句话说：&lt;/p&gt;&#10;&#10; &lt;blockquote&gt;&#10; &lt;p&gt;什么是意识形态？&#10;意识形态就是在了解对方的话语之前，首先怀疑对方的动机！&lt;/p&gt;&#10;&#10; &lt;/blockquote&gt;&#10;&lt;p&gt;现在，中国民众的普遍意识形态便是如此。在理解政府的话语之前，民众倾向于无限怀疑政府的动机。长此以往，政府的号召力倾于式微，而民众也极有可能会过多地否认政府的真实话语。&lt;/p&gt;&#10;</description></item></channel></rss>