見出し画像

小売ガソリン価格推計誤差の季節循環

概要

ガソリンETF UGAの価格を利用して全米小売ガソリン価格を推計する。
推計した値と実勢値の誤差を月毎にまとめヒストグラムにする。

サンプルコード

w <- residuals(lm(apply.monthly(GASREGW,mean)["2011::"] ~ apply.monthly(UGA[,4],mean)["2011::"])) %>% as.vector() %>% c(.,rep(NA,6)) %>% matrix(.,nrow=12) %>% t() %>% apply(.,2,mean,na.rm=T)
df <- data.frame(gap=w,mon=factor(seq(1,12,1)))
p <- ggplot(df, aes(x=mon,y=gap,fill =mon)) 
p <- p + scale_x_discrete(label=c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))  #x -axis label
p <- p + geom_bar(stat = "identity") # need identity to draw value itself. 
p <- p + theme(legend.position = 'none')  # erase legend
plot(p)

出力例

スクリーンショット 2022-06-28 9.29.23

サンプルコードその2

理論値からの誤差ではなく価格データをそのまま使用し、平均値からのプラスマイナスを表示している。
カラーはcolorspace::rainbow_hcl()を使って24色を生成。そのうちの12色を使用した。
パレットを使用せず、scale_fill_manual()を利用して色データをそのまま使用。

w <- as.vector(apply.monthly(GASREGW,mean)["1992::"])
df(d=(head(w,360) %>% matrix(.,nrow=12) %>% t() %>% apply(.,2,mean) ),m=seq(1,12,1))
df <- data.frame(d=(head(w,360) %>% matrix(.,nrow=12) %>% t() %>% apply(.,2,mean) ),m=factor(seq(1,12,1)))
df$d <- df$d -  mean(df$d)
p <- ggplot(df,aes(x=m,y=d,fill=m))
p <- p + scale_x_discrete(label=c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))  #x  -axis label
p <- p + geom_bar(stat = "identity") # need identity to draw value itself.
p <- p + theme(legend.position = 'none')  # erase legend
library(colorspace)
p <- p + scale_fill_manual(values =rainbow_hcl(24))
plot(p)

出力例その2

スクリーンショット 2022-07-03 10.50.01


この記事が気に入ったらサポートをしてみませんか?