if (!require(plotly))   install.packages("plotly")
##  要求されたパッケージ plotly をロード中です
##  要求されたパッケージ ggplot2 をロード中です
## 
##  次のパッケージを付け加えます: 'plotly'
##  以下のオブジェクトは 'package:ggplot2' からマスクされています:
## 
##     last_plot
##  以下のオブジェクトは 'package:stats' からマスクされています:
## 
##     filter
##  以下のオブジェクトは 'package:graphics' からマスクされています:
## 
##     layout
if (!require(quantmod)) install.packages("quantmod")
##  要求されたパッケージ quantmod をロード中です
##  要求されたパッケージ xts をロード中です
##  要求されたパッケージ zoo をロード中です
## 
##  次のパッケージを付け加えます: 'zoo'
##  以下のオブジェクトは 'package:base' からマスクされています:
## 
##     as.Date, as.Date.numeric
##  要求されたパッケージ TTR をロード中です
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
year  <- 2011:2017
sales <- c(1, 3, 2, 7, 3, 8, 7)
n <- length(year)
d <- data.frame(year, sales)
d
plot_ly(x = d$year, y = d$sales, type = "scatter", mode = "markers+lines") |>
  layout(title = "ロボット販売",
         xaxis = list(title = "会計年"),
         yaxis = list(title = "台数"))
cairo_pdf('robot_sales.pdf')
matplot(x = year, y = sales, type = 'o', pch = 1, col = 4,
        main = 'ロボット販売',
        xlab = '会計年',
        ylab = '台数')
grid()
dev.off()
## png 
##   2
d$ma3 <- NA 

for (i in 2:(n-1))
{
  d$ma3[i] <- (sales[i-1] + sales[i] + sales[i+1]) / 3
}

d$ma3
## [1] NA  2  4  4  6  6 NA
ma3 <- stats::filter(sales, rep(1/3, 3))
ma3
## Time Series:
## Start = 1 
## End = 7 
## Frequency = 1 
## [1] NA  2  4  4  6  6 NA
library(zoo)
ma3 <- rollmean(sales, 3, na.pad = T)
ma3
## [1] NA  2  4  4  6  6 NA
d$ma4 <- NA 

for (i in 3:(n-2)) 
{
  d$ma4[i] <-
    (0.5 * sales[i-2] + sales[i-1] + sales[i] + sales[i+1] + 0.5 * sales[i+2]) / 4
}
ma4 <- stats::filter(sales, c(0.5/4, 1/4, 1/4, 1/4, 0.5/4))
  ma4
## Time Series:
## Start = 1 
## End = 7 
## Frequency = 1 
## [1]    NA    NA 3.500 4.375 5.625    NA    NA
 ma4 <- rollmean(sales, k = 4, na.pad = T)
  ma4
## [1]   NA 3.25 3.75 5.00 6.25   NA   NA
options(digits = 2)
d
plot_ly(type = "scatter", mode = "markers+lines") |>
  add_trace(x = d$year, y = d$sales, name = "台数") |>
  add_trace(x = d$year, y = d$ma3,   name = "3項移動平均") |>
  add_trace(x = d$year, y = d$ma4,   name = "4項移動平均") |>
  layout(title = "ロボット販売",
         xaxis = list(title = "会計年"),
         yaxis = list(title = "台数"))
cairo_pdf('robot_sales_moving_average.pdf')

matplot(x = year, y = sales, type = 'o', pch = 1,
        main = 'ロボット販売', xlab = '会計年', ylab = '台数')
grid()

matlines(x = year, y = d$ma3, type = 'o', pch = 2, col = 2)
matlines(x = year, y = d$ma4, type = 'o', pch = 4, col = 4)

legend('topleft', col = c(1, 2, 4), pch = c(1, 2, 4), lty = 1,
       legend = c('台数', '3項移動平均', '4項移動平均'))

dev.off()
## png 
##   2
d0 <- read.csv(file = 'C:/Users/naruk/Downloads/newly_confirmed_cases_daily.csv')
d0
x <- as.POSIXct(d0$Date, format = '%Y/%m/%d') 
y <- d0$ALL

yhat <- rollmean(y, 31, na.pad = T)

plot_ly(type = "scatter", mode = "lines") |>
  add_trace(x = x, y = y,    name = "原系列") |>
  add_trace(x = x, y = yhat, name = "31日移動平均") |>
  config(locale = "ja") |> 
  layout(title = "新規陽性者数の推移(日別)",
         xaxis = list(title = "年"),
         yaxis = list(title = "陽性者数"))
cairo_pdf('C:/Users/naruk/Downloads/pcr_tested_daily.csv')

matplot(x = x, y = y, type = 'l', col = 3, 
        main = '新規陽性者数の推移(日別)', xlab = '年', ylab = '陽性者数')
matlines(x = x, y = yhat, col = 1)
grid()
legend('topleft', col = c(3, 1), lty = 1, legend = c('原系列', '31日移動平均'))

dev.off()
## png 
##   2