if (!require(plotly)) install.packages("plotly")
if (!require(quantmod)) install.packages("quantmod")
library(plotly)
library(quantmod)
d <- read.csv("C:/Users/Sota al/Downloads/pcr_tested_daily (1).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 = "人数"))
x <- as.POSIXct(a, format = '%Y/%m/%d') # 「/」で区切られていることに注意
y <- b
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") |> # x軸ラベルの月を日本語表示
layout(title = "PCR検査実施人数(日別)",
xaxis = list(title = "年"),
yaxis = list(title = "実施人数"))
cairo_pdf('corona.pdf')
matplot(x = x, y = y, type = 'l', col = 3,
main = 'PCR検査実施人数(日別)', 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
z <- getSymbols('3092', src = 'yahooj',
from = '2022-01-01',
to = '2022-12-31',
auto.assign = F)
# データの保存
write.zoo(z, file = 'zozo.csv', sep = ',', quote = F)
#write.zoo(z, file = "C:/Users/Sota al/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('topleft', col = c(3, 2, 4, 1), lty = 1,
legend = c('株価',
'5日移動平均',
'25日移動平均',
'25日中心化移動平均(参考)'))

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
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("topleft", 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