This code loads and processes a CSV file containing time series data. It converts the data into time series objects, visualizes them using ggplot2, and performs forecasts using the Holt-Winters method. The code also detects breakpoints in the time series data with the strucchange package and calculates the correlation between Price and PSA10Total, displaying it with a scatter plot.
Keywords
Time Series Analysis, Holt-Winters Forecasting, Breakpoint Detection
# 日付データの整形data$Date <-as.Date(data$Date, format="%Y/%m/%d")# Priceの時系列データに変換price_ts <-ts(data$Price, frequency =12, start =c(2023, 4))# PSA10Totalの時系列データに変換psa10total_ts <-ts(data$PSA10Total, frequency =12, start =c(2023, 4))# 基本的な時系列データのプロットautoplot(price_ts, ts.colour ='blue') +ggtitle("Time Series Data Plot (Price)") +xlab("Time") +ylab("Price")
Code
autoplot(psa10total_ts, ts.colour ='blue') +ggtitle("Time Series Data Plot (PSA10Total)") +xlab("Time") +ylab("PSA10Total")
Code
# Holt-Winters法による予測 (Price)fit_price <-hw(price_ts, seasonal ="multiplicative")forecast_price <-forecast(fit_price, h =24)autoplot(forecast_price) +ggtitle("Forecast using Holt-Winters Method (Price)") +xlab("Time") +ylab("Price")
Code
# ブレークポイントの検出 (Price)breakpoints_price <-breakpoints(price_ts ~1)plot(price_ts, main ="Breakpoints in Price Data")lines(breakpoints_price, col ="red")title("Breakpoints in Price Data")
Code
# PriceとPSA10Totalの相関関係の計算correlation <-cor(data$Price, data$PSA10Total, method ="pearson")# 相関関係の表示print(paste("Correlation between Price and PSA10Total:", correlation))
[1] "Correlation between Price and PSA10Total: -0.580141293332746"
Code
# 散布図のプロットggplot(data, aes(x = Price, y = PSA10Total)) +geom_point(color ="blue") +ggtitle("Scatter Plot of Price vs PSA10Total") +xlab("Price") +ylab("PSA10Total") +geom_smooth(method ="lm", col ="red")
次に、ブレークポイントの検出を行う。breakpoints(price_ts ~ 1)を使用してprice_tsのブレークポイントを検出する。ここでは、定数項のみのモデル(~ 1)を使用している。plot(price_ts, main = "Breakpoints in Price Data")でprice_tsの時系列データをプロットし、lines(breakpoints_price, col = "red")で検出されたブレークポイントを赤色の線でプロットに追加する。title("Breakpoints in Price Data")でプロットにタイトルを追加する。
# 日付データの整形data$Date <-as.Date(data$Date, format="%Y/%m/%d")# Priceの時系列データに変換price_ts <-ts(data$Price, frequency =12, start =c(2023, 3))# PSA10Totalの時系列データに変換psa10total_ts <-ts(data$PSA10Total, frequency =12, start =c(2023, 3))# 基本的な時系列データのプロットautoplot(price_ts, ts.colour ='blue') +ggtitle("Time Series Data Plot (Price)") +xlab("Time") +ylab("Price")
Code
autoplot(psa10total_ts, ts.colour ='blue') +ggtitle("Time Series Data Plot (PSA10Total)") +xlab("Time") +ylab("PSA10Total")
Code
# Holt-Winters法による予測 (Price)fit_price <-hw(price_ts, seasonal ="multiplicative")forecast_price <-forecast(fit_price, h =24)autoplot(forecast_price) +ggtitle("Forecast using Holt-Winters Method (Price)") +xlab("Time") +ylab("Price")
Code
# ブレークポイントの検出 (Price)breakpoints_price <-breakpoints(price_ts ~1)plot(price_ts, main ="Breakpoints in Price Data")lines(breakpoints_price, col ="red")title("Breakpoints in Price Data")
Code
# PriceとPSA10Totalの相関関係の計算correlation <-cor(data$Price, data$PSA10Total, method ="pearson")# 相関関係の表示print(paste("Correlation between Price and PSA10Total:", correlation))
[1] "Correlation between Price and PSA10Total: -0.477975083396857"
Code
# 散布図のプロットggplot(data, aes(x = Price, y = PSA10Total)) +geom_point(color ="blue") +ggtitle("Scatter Plot of Price vs PSA10Total") +xlab("Price") +ylab("PSA10Total") +geom_smooth(method ="lm", col ="red")
次に、ブレークポイントの検出を行う。breakpoints(price_ts ~ 1)を使用してprice_tsのブレークポイントを検出する。ここでは、定数項のみのモデル(~ 1)を使用している。plot(price_ts, main = "Breakpoints in Price Data")でprice_tsの時系列データをプロットし、lines(breakpoints_price, col = "red")で検出されたブレークポイントを赤色の線でプロットに追加する。title("Breakpoints in Price Data")でプロットにタイトルを追加する。
# 日付データの整形data$Date <-as.Date(data$Date, format="%Y/%m/%d")# Priceの時系列データに変換price_ts <-ts(data$Price, frequency =12, start =c(2023, 4))# PSA10Totalの時系列データに変換psa10total_ts <-ts(data$PSA10Total, frequency =12, start =c(2023, 4))# 基本的な時系列データのプロットautoplot(price_ts, ts.colour ='blue') +ggtitle("Time Series Data Plot (Price)") +xlab("Time") +ylab("Price")
Code
autoplot(psa10total_ts, ts.colour ='blue') +ggtitle("Time Series Data Plot (PSA10Total)") +xlab("Time") +ylab("PSA10Total")
Code
# Holt-Winters法による予測 (Price)fit_price <-hw(price_ts, seasonal ="multiplicative")forecast_price <-forecast(fit_price, h =24)autoplot(forecast_price) +ggtitle("Forecast using Holt-Winters Method (Price)") +xlab("Time") +ylab("Price")
Code
# ブレークポイントの検出 (Price)breakpoints_price <-breakpoints(price_ts ~1)plot(price_ts, main ="Breakpoints in Price Data")lines(breakpoints_price, col ="red")title("Breakpoints in Price Data")
Code
# PriceとPSA10Totalの相関関係の計算correlation <-cor(data$Price, data$PSA10Total, method ="pearson")# 相関関係の表示print(paste("Correlation between Price and PSA10Total:", correlation))
[1] "Correlation between Price and PSA10Total: -0.783406892902581"
Code
# 散布図のプロットggplot(data, aes(x = Price, y = PSA10Total)) +geom_point(color ="blue") +ggtitle("Scatter Plot of Price vs PSA10Total") +xlab("Price") +ylab("PSA10Total") +geom_smooth(method ="lm", col ="red")
次に、ブレークポイントの検出を行う。breakpoints(price_ts ~ 1)を使用してprice_tsのブレークポイントを検出する。ここでは、定数項のみのモデル(~ 1)を使用している。plot(price_ts, main = "Breakpoints in Price Data")でprice_tsの時系列データをプロットし、lines(breakpoints_price, col = "red")で検出されたブレークポイントを赤色の線でプロットに追加する。title("Breakpoints in Price Data")でプロットにタイトルを追加する。