2017-10-10 15 views

答えて

0

パッケージdplyrは、lag()機能で役立ちます。これは、ts(時系列)データフレーム用ではありません。ここでは例:単純に設定月次データの場合は

#using the library "dplyr" 
library(dplyr) 
#setting seed 
set.seed(20) 
#creating a random dataframe 
df <- data.frame(date=paste(rep(2000:2017, each=4),"Q",rep(1:4, 18)), GDP= cumsum(sample(c(-0.5, 3), 72, TRUE))) 
#calculating the annual percentage change 
df <- df %>% mutate(change=(GDP-lag(GDP,4))/lag(GDP,4)*100) 

:VARは関心

の変数を表します lag(VAR,12)