R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

px <- seq(as.POSIXct('2023-01-01 01:00:00'),
          as.POSIXct('2023-01-01 12:00:00'), by = 60 * 60)

n <- length(px)

# ホワイトノイズ
set.seed(5963)
x <- rnorm(n, mean = 0, sd = 1)

# 時系列を扱うts型オブジェクトを作成
y <- ts(x) # 原系列
x <- 1:n

# 時系列を扱うts型オブジェクトを作成
y <- ts(x) # 原系列

y.lag1 <- lag(y, k = -1) # 1次ラグ系列
y.lag2 <- lag(y, k = -2) # 2次ラグ系列
y.lag3 <- lag(y, k = -3) # 3次ラグ系列

ts4 <- cbind(y, y.lag1, y.lag2, y.lag3)[1:n, ]

d <- as.data.frame(ts4)
d
##     y y.lag1 y.lag2 y.lag3
## 1   1     NA     NA     NA
## 2   2      1     NA     NA
## 3   3      2      1     NA
## 4   4      3      2      1
## 5   5      4      3      2
## 6   6      5      4      3
## 7   7      6      5      4
## 8   8      7      6      5
## 9   9      8      7      6
## 10 10      9      8      7
## 11 11     10      9      8
## 12 12     11     10      9
matplot(x = px, y = d, type = 'o', lty = 1:4, col = 1:4, pch = 1:4, 
        main = '原系列とラグ系列', xlab = '時刻', ylab = '値')

grid()

legend('topleft', lty = 1:4, col = 1:4, pch = 1:4,
       legend = c('原系列', '1次ラグ系列', '2次ラグ系列', '3次ラグ系列'))