金融时间序列分析:6. AR模型实例(R语言)

来源:互联网 发布:exe文件加密网络授权 编辑:程序博客网 时间:2024/05/16 17:30

0. 目录

金融时间序列分析:9. ARMA自回归移动平均模型
金融时间序列分析:8. MA模型实例(Python)
金融时间序列分析:7. MA滑动平均模型
金融时间序列分析:6. AR模型实例
金融时间序列分析:5. AR模型实例(Python)
金融时间序列分析:4. AR自回归模型
金融时间序列分析:3. First Demo By Python
金融时间序列分析:2. 数学分析模型
金融时间序列分析:1. 基础知识


1. 前言

前一篇写了如何用Python构建AR模型,但是由于不太熟悉,很多问题都没有说清楚,本文用R语言详细的讲一讲,算是为前面两篇文章补漏吧。

2. 获取数据

library(quantmod)library(xts)library(zoo)library(quantmod)library(TTR)#获取数据getSymbols("300033.SZ")View(`300033.SZ`)

这里写图片描述

3. 数据预处理

THS.Close = `300033.SZ`$`300033.SZ.Adjusted`THS.Closedim(THS.Close)library(fBasics)pacf(THS.Close, lag.max = 30)acf(THS.Close, lag.max = 30)THS.ret = diff(THS.Close)tail(THS.ret)log_data = log(THS.Close)log_ret = diff(log_data)

数据清理:

na.omit(log_ret)log_ret <- na.omit(log_ret)dim(log_ret)basicStats(log_data)basicStats(log_ret)

这里写图片描述

这里写图片描述

4. 数据预览

 plot(log_ret)hist(log_ret, nclass = 200)

这里写图片描述

这里写图片描述

5. 定阶

pacf(log_ret, lag.max = 30)acf(log_ret, lag.max = 30)Box.test(log_ret, lag = 2, type = 'Ljung-Box')Box.test(log_ret, lag = 1, type = 'Ljung-Box')

这里写图片描述

6. AR模型

arx2 = arima(log_ret, order = c(2, 0, 0))plot(arx2$residuals)plot(arx2$residuals, log_ret)

这里写图片描述
上面几个数值的意义:
yt=xt0.0011
y(t)=0.0829y(t1)+0.0076y(t2)+a(t)

Box.test(arx2$residuals, lag = 2, type = 'Ljung-Box')

这里写图片描述

这里写图片描述

这里写图片描述

tsdiag(arx2)

这里写图片描述

predict(arx2, 8)

这里写图片描述

0 0
原创粉丝点击