R TaskViewを使って独り立ちする

  • ある程度Rが使えるようになったら、あとは自分がやりたいことをするための調べ物ができるようになることが大事
  • 短期集中セミナーである程度Rが使えるようになったら、調べ物のやり方について知ることが大事
  • そんな短期集中セミナーの最終回のテキスト(Rmd フォーマット)
---
title: "TimeSeries時系列"
author: "ryamada"
date: "2016年7月24日"
output: html_document
---

# R for Time Series Analyses 時系列解析のためのR

When you are new to a type of analysis, "time series analysis" for example, you may want to check packages useful for it.

新しいタイプの解析、たとえば時系列解析、をしようとするときに、有用なパッケージを探したいと思うかもしれない。

Task view is one place to look for. "Task view"に探しに行くこともできる。

Goocle with "CRAN time series task view".
"CRAN time series task view"でグーグル。

https://cran.r-project.org/web/views/TimeSeries.html


# List topics handled in the article 記事で扱っているトピックスを確認

- Basics
- Times and Dates
- Time Series Classes
- Forecasting and Univariate Modeling
- Frequency analysis
- Decomposition and Filtering
- Seasonality
- Stationarity, Unit Roots, and Cointegration
- Nonlinear Time Series Analysis
- Dynamic Regression Models
- Multivariate Time Series Models
- Continuous time models
- Resampling
- Time Series Data
- Miscellaneous

# Understand Time Series Analyses Based on the List of Topics トピックスリストに基づいて時系列解析を理解する

## Data handling
* Basics
    + Regularly spaced time series (using numeric time stamps)
    + 規則的な時間間隔系列。数値でタイムスタンプ
    + IRRegularly spaced data needs something special
    + 不規則な時間間隔データには特別な何かが必要
* Time Series Classes
    + ts class is the base and there are many other classes. Grab the ts class first and then select one of others if necessary
    + tsクラスを基本にたくさんのクラスがあるから、tsの概要を理解した上で必要に応じて選ぶ

## Our task is what kind of time-series analysis and how? 自分のやりたいことはどういうタイプの時系列解析で、どうやるか?
* Tasks handled 扱う仕事
    + Forecasting and Univariate Modeling 予測・1変数
    + Frequency analysis 周波数解析
    + Decomposition and Filtering 分解・フィルタリング
    + Seasonality 季節性変動
    + Stationarity, Unit Roots, and Cointegration 定常性…?
* How to analyze どう解析するか
    + Nonlinear Time Series Analysis
    + Linear analysis is the basic and NONlinear is special
    + Dynamic Regression Models
    + Regular regression analysis is "Non-dynamic"
    + Multivariate Time Series Models
    + Mono-variate is regular and MULTI-variate is special
    + Continuous time models
    + NON-continuous = DISCRETE is regular and Continuous is special
    


# Learn with examples Examples を使って学ぶ

## ts

```{r}
help(ts)
```

```{r}
#example(ts)
```

```{r}
require(graphics)

ts(1:10, frequency = 4, start = c(1959, 2)) # 2nd Quarter of 1959
print( ts(1:10, frequency = 7, start = c(12, 2)), calendar = TRUE)
# print.ts(.)
## Using July 1954 as start date:
gnp <- ts(cumsum(1 + round(rnorm(100), 2)),
          start = c(1954, 7), frequency = 12)
plot(gnp) # using 'plot.ts' for time-series plot

## Multivariate
z <- ts(matrix(rnorm(300), 100, 3), start = c(1961, 1), frequency = 12)
class(z)
head(z) # as "matrix"
plot(z)
plot(z, plot.type = "single", lty = 1:3)

## A phase plot:
plot(nhtemp, lag(nhtemp, 1), cex = .8, col = "blue",
     main = "Lag plot of New Haven temperatures")

```

Look inside of objects. オブジェクトを見てみる
```{r}
gnp
head(gnp)
str(gnp) # structure
class(gnp)
```

## Decomposition and Filtering

```
Filters and smoothing : filter() in stats provides autoregressive and moving average linear filtering of multiple univariate time series. The robfilter package provides several robust time series filters, while mFilter includes miscellaneous time series filters useful for smoothing and extracting trend and cyclical components. smooth() from the stats package computes Tukey's running median smoothers, 3RS3R, 3RSS, 3R, etc. sleekts computes the 4253H twice smoothing method.
```

```{r}
help(filter)
# example(filter)
x <- rnorm(100)
x <- cumsum(x)
par(mfrow=c(2,3))
plot(x,type="l",main="original")
tmp.x <- x
for(i in 2:6){
  tmp.x <- filter(tmp.x,rep(1,3)/3) 
  plot(tmp.x,col=i,type="l",main=paste("",i-1))
}
```

```{r}
help(decompose)
# example(decompose)
require(graphics)
plot(co2)
m <- decompose(co2)
m$figure
plot(m)

## example taken from Kendall/Stuart
x <- c(-50, 175, 149, 214, 247, 237, 225, 329, 729, 809,
       530, 489, 540, 457, 195, 176, 337, 239, 128, 102, 232, 429, 3,
       98, 43, -141, -77, -13, 125, 361, -45, 184)
x <- ts(x, start = c(1951, 1), end = c(1958, 4), frequency = 4)
m <- decompose(x)
## seasonal figure: 6.25, 8.62, -8.84, -6.03
round(decompose(x)$figure / 10, 2)
```
```{r}
x <- ts(cumsum(rnorm(1000)),frequency=12) # No seasonality
m <- decompose(x)
plot(m)
str(m)
```



# Optional

## Packages and Task Views パッケージとTask View

See https://mran.revolutionanalytics.com/rpackages/ 

You can download ALL PACKAGES listed in a task view at once, (if you really want to do so).

あるTask viewに扱われているパッケージを一括でダウンロードすることもできる(もし本当にそうしたいのなら)## Search tools 探し物

http://www.r-bloggers.com/21-r-navigation-tools/