Instantly share code, notes, and snippets.

# Gedevan-Aleksizde/common.R Last active Sep 6, 2017

 # ------ common part ---- require(ggplot2) require(dplyr) require(tidyr) require(dlm) # 1.1-4 require(KFAS) # 1.2.9 require(bsts) # 0.7.1 # calculate the reference price calc_RP <- function(RP, p, a){ for( i in 2:length(RP)){ RP[i] <- a * RP[i-1] + (1-a) *p[i-1] } return(RP) } # calculate Z calc_Z <- function(RP, p){ z_1<-0 z_2<-0 v_wk<- p - RP n <- length(RP) for (i in 1:n){ if (v_wk[i]>=0) { z_1[i] <- v_wk[i]/100 } else { z_1[i] <- 0 } if (v_wk[i] < 0) { z_2[i] <- -v_wk[i]/100 } else { z_2[i] <- 0 } } return(list(z1=z_1, z2=z_2)) } # read the data #ID_W:週ID #end:エンド陳列の有無 #logPI:点数PIの対数 #AP:売価 #RP095:参照価格(繰越パラメータ0.95：RP_t=0.95*RP_{t-1}+(1-0.95)*AP_{t-1}) df <- read.csv("data/test.csv") ggplot(gather(df, key=series, value=value, -ID_W)) + geom_line(aes(x=ID_W, y=value, group=series), color="grey") + facet_wrap(~series, scales = "free") + labs(x="week", y="") + scale_color_discrete(guide=F) + expand_limits(y=0) + theme_bw()