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
d <- read.csv("C:/Users/n_oku/Downloads/pcr_tested_daily.csv")
library(DT)
datatable(d)
a <- d$日付
b <- d$PCR.検査実施人数.単日.
plot_ly(x = a, y = b, type = "scatter", mode = "markers+lines") |>
layout(title = "PCR検査",
xaxis = list(title = "日付"),
yaxis = list(title = "人数"))
#zozo
z <- getSymbols('3092', src = 'yahooj',
from = '2022-01-01',
to = '2022-12-31',
auto.assign = F)
# データの保存
write.zoo(z, file = "C:/Users/n_oku/Downloads/zozo.csv", sep = ',', quote = F)
x <- as.POSIXct(index(z), format = '%Y-%m-%d')
y <- z[, 'YJ3092.Close']
# 1日分データをずらすことで当日の日付の株価が1日前のものになる。
ylag1 <- lag(y, k = 1)
# align = 'right'で右寄せ移動平均となる。
yhat5 <- rollmean(ylag1, 5, fill = NA, align = 'right')
yhat25 <- rollmean(ylag1, 25, fill = NA, align = 'right')
yhat25c <- rollmean(y, 25, fill = NA) # 参考(通常の移動平均:中心化移動平均)
matplot(x, y, type = 'l', col = 3,
main = 'ZOZO(3092)', xlab = '日', ylab = '円')
matlines(x, yhat5, col = 2)
matlines(x, yhat25, col = 4)
matlines(x, yhat25c, col = 1)
grid()
legend('bottomright', col = c(3, 2, 4, 1), lty = 1,
legend = c('株価',
'5日移動平均',
'25日移動平均',
'25日中心化移動平均(参考)'))

# (参考)quantmodパッケージのグラフ機能
chartSeries(z, TA = c(addBBands(), addMACD()), type='bar',
subset='2022-06-01/2022-12-31')

plot_ly(type = "scatter", mode = "lines") |>
add_trace(x = x, y = paste(y), name = "株価") |>
add_trace(x = x, y = paste(yhat5), name = "5日移動平均") |>
add_trace(x = x, y = paste(yhat25), name = "25日移動平均") |>
add_trace(x = x, y = paste(yhat25c), name = "25日中心化移動平均(参考)") |>
config(locale = "ja") |> # x軸ラベルの月を日本語表示
layout(title = "ZOZO(3092)",
xaxis = list(title = "日"),
yaxis = list(title = "円"))
## Warning: Can't display both discrete & non-discrete data on same axis
# LOESS(平滑化窓幅: span)
x <- seq_along(y)
fit10 <- loess(y ~ x, data = data.frame(x, y), span = 0.1) # 10%
fit30 <- loess(y ~ x, data = data.frame(x, y), span = 0.3) # 30%
matplot(x, y = y, type = "l", col = 3,
main = "ZOZO(3092)", xlab = "日", ylab = "円")
matlines(x, fit10$fitted, col = 1, lwd = 2)
matlines(x, fit30$fitted, col = 2, lwd = 2)
grid()
legend("bottomright", col = c(3, 1, 2), lty = 1,
legend = c("株価",
"LOESS(平滑化窓幅10%)",
"LOESS(平滑化窓幅30%)"))

plot_ly(type = "scatter", mode = "lines") |>
add_trace(x = x, y = paste(y), name = "株価") |>
add_trace(x = x, y = paste(fit10$fitted), name = "LOESS(平滑化窓幅10%)") |>
add_trace(x = x, y = paste(fit30$fitted), name = "LOESS(平滑化窓幅30%)") |>
layout(title = "ZOZO(3092)",
xaxis = list(title = "日"),
yaxis = list(title = "円"))
## Warning: Can't display both discrete & non-discrete data on same axis