吴裕雄--天生自然 R语言开发学习:时间序列(续三)




#-----------------------------------------#
# R in Action (2nd ed): Chapter 15 #
# Time series #
# requires forecast, tseries packages #
# install.packages("forecast", "tseries") #
#-----------------------------------------# par(ask=TRUE) # Listing 15.1 - Creating a time series object in R
sales <- c(18, 33, 41, 7, 34, 35, 24, 25, 24, 21, 25, 20,
22, 31, 40, 29, 25, 21, 22, 54, 31, 25, 26, 35)
tsales <- ts(sales, start=c(2003, 1), frequency=12)
tsales
plot(tsales) start(tsales)
end(tsales)
frequency(tsales) tsales.subset <- window(tsales, start=c(2003, 5), end=c(2004, 6))
tsales.subset # Listing 15.2 - Simple moving averages
library(forecast)
opar <- par(no.readonly=TRUE)
par(mfrow=c(2,2))
ylim <- c(min(Nile), max(Nile))
plot(Nile, main="Raw time series")
plot(ma(Nile, 3), main="Simple Moving Averages (k=3)", ylim=ylim)
plot(ma(Nile, 7), main="Simple Moving Averages (k=7)", ylim=ylim)
plot(ma(Nile, 15), main="Simple Moving Averages (k=15)", ylim=ylim)
par(opar) # Listing 15.3 - Seasonal decomposition using slt()
plot(AirPassengers)
lAirPassengers <- log(AirPassengers)
plot(lAirPassengers, ylab="log(AirPassengers)")
fit <- stl(lAirPassengers, s.window="period")
plot(fit)
fit$time.series
exp(fit$time.series) par(mfrow=c(2,1))
library(forecast)
monthplot(AirPassengers, xlab="", ylab="")
seasonplot(AirPassengers, year.labels="TRUE", main="")
par(opar) # Listing 15.4 - Simple exponential smoothing
library(forecast)
fit <- HoltWinters(nhtemp, beta=FALSE, gamma=FALSE)
fit forecast(fit, 1) plot(forecast(fit, 1), xlab="Year",
ylab=expression(paste("Temperature (", degree*F,")",)),
main="New Haven Annual Mean Temperature") accuracy(fit) # Listing 15.5 - Exponential smoothing with level, slope, and seasonal components
fit <- HoltWinters(log(AirPassengers))
fit accuracy(fit) pred <- forecast(fit, 5)
pred
plot(pred, main="Forecast for Air Travel",
ylab="Log(AirPassengers)", xlab="Time")
pred$mean <- exp(pred$mean)
pred$lower <- exp(pred$lower)
pred$upper <- exp(pred$upper)
p <- cbind(pred$mean, pred$lower, pred$upper)
dimnames(p)[[2]] <- c("mean", "Lo 80", "Lo 95", "Hi 80", "Hi 95")
p # Listing 15.6 - Automatic exponential forecasting with ets()
library(forecast)
fit <- ets(JohnsonJohnson)
fit
plot(forecast(fit), main="Johnson and Johnson Forecasts",
ylab="Quarterly Earnings (Dollars)", xlab="Time") # Listing 15.7 - Transforming the time series and assessing stationarity
library(forecast)
library(tseries)
plot(Nile)
ndiffs(Nile)
dNile <- diff(Nile)
plot(dNile)
adf.test(dNile) # Listing 15.8 - Fit an ARIMA model
fit <- arima(Nile, order=c(0,1,1))
fit
accuracy(fit) # Listing 15.9 - Evaluating the model fit
qqnorm(fit$residuals)
qqline(fit$residuals)
Box.test(fit$residuals, type="Ljung-Box") # Listing 15.10 - Forecasting with an ARIMA model
forecast(fit, 3)
plot(forecast(fit, 3), xlab="Year", ylab="Annual Flow") # Listing 15.11 - Automated ARIMA forecasting
library(forecast)
fit <- auto.arima(sunspots)
fit
forecast(fit, 3)
accuracy(fit)
吴裕雄--天生自然 R语言开发学习:时间序列(续三)的更多相关文章
- 吴裕雄--天生自然 R语言开发学习:R语言的安装与配置
下载R语言和开发工具RStudio安装包 先安装R
- 吴裕雄--天生自然 R语言开发学习:数据集和数据结构
数据集的概念 数据集通常是由数据构成的一个矩形数组,行表示观测,列表示变量.表2-1提供了一个假想的病例数据集. 不同的行业对于数据集的行和列叫法不同.统计学家称它们为观测(observation)和 ...
- 吴裕雄--天生自然 R语言开发学习:导入数据
2.3.6 导入 SPSS 数据 IBM SPSS数据集可以通过foreign包中的函数read.spss()导入到R中,也可以使用Hmisc 包中的spss.get()函数.函数spss.get() ...
- 吴裕雄--天生自然 R语言开发学习:使用键盘、带分隔符的文本文件输入数据
R可从键盘.文本文件.Microsoft Excel和Access.流行的统计软件.特殊格 式的文件.多种关系型数据库管理系统.专业数据库.网站和在线服务中导入数据. 使用键盘了.有两种常见的方式:用 ...
- 吴裕雄--天生自然 R语言开发学习:R语言的简单介绍和使用
假设我们正在研究生理发育问 题,并收集了10名婴儿在出生后一年内的月龄和体重数据(见表1-).我们感兴趣的是体重的分 布及体重和月龄的关系. 可以使用函数c()以向量的形式输入月龄和体重数据,此函 数 ...
- 吴裕雄--天生自然 R语言开发学习:基础知识
1.基础数据结构 1.1 向量 # 创建向量a a <- c(1,2,3) print(a) 1.2 矩阵 #创建矩阵 mymat <- matrix(c(1:10), nrow=2, n ...
- 吴裕雄--天生自然 R语言开发学习:图形初阶(续二)
# ----------------------------------------------------# # R in Action (2nd ed): Chapter 3 # # Gettin ...
- 吴裕雄--天生自然 R语言开发学习:图形初阶(续一)
# ----------------------------------------------------# # R in Action (2nd ed): Chapter 3 # # Gettin ...
- 吴裕雄--天生自然 R语言开发学习:图形初阶
# ----------------------------------------------------# # R in Action (2nd ed): Chapter 3 # # Gettin ...
- 吴裕雄--天生自然 R语言开发学习:基本图形(续二)
#---------------------------------------------------------------# # R in Action (2nd ed): Chapter 6 ...
随机推荐
- [原]排错实战——解救加载调试符号失败的IDA
原调试IDA排错troubleshootsymbolspdbsysinternalprocess monitor 缘起 最近想借助IDA逆向一个函数.在windows下,调试器(比如vs, windb ...
- MySQL--数据插入
1.创建表的同时插入其他表的数据 CREATE TABLE table_name SELECT ... FROM ... [...] 例: CREATE TABLE tabl_memory EN ...
- 论文或github中一些通用思想
(1) 云从 上海交大 ECCV2018 http://openaccess.thecvf.com/content_ECCV_2018/papers/Yao_Feng_Joint_3D_Face_EC ...
- 6.docker container
1. 什么是container 通过 image 创建 (copy得到的) 在 image layer (在image 层) 上 增加了一层 container layer (container 层 ...
- 1.windows-oracle实战第一课
一.oracle是目前最流行的数据库之一,功能强大.性能卓越.学习要有信心.oracle也做软件,不仅仅是数据库.比如ERP(企业资源计划,用友.金蝶) 二.目前的数据库 相对而言: ...
- Java统计内存
在目标代码前放置 Runtime r = Runtime.getRuntime(); r.gc(); long startMem = r.freeMemory(); // 开始时的剩余内存 目标代码执 ...
- 功能区按钮调用Excel、PowerPoint、Word中的VBA宏:RunMacro
功能区按钮调用Excel.PowerPoint.Word中的VBA宏:RunMacro 众所周知,Excel.PPT.Word文档或加载宏文件中可以写很多过程和函数,调试的过程中当然可以按F8或F5直 ...
- HDU-1828 Picture(扫描线 求矩形并的周长)
http://acm.hdu.edu.cn/showproblem.php?pid=1828 Time Limit: 6000/2000 MS (Java/Others) Memory Limi ...
- 42)PHP,mysqli函数功能总结
fetch----------------一个一个的取值,这个注意 fetch_array(),fetch_assoc(),fetch_object(),这三个方法的使用请看手册 请注意是FETCH, ...
- 吴裕雄--天生自然C语言开发:判断
if(boolean_expression) { /* 如果布尔表达式为真将执行的语句 */ } #include <stdio.h> int main () { /* 局部变量定义 */ ...