Гомогенизировать тип временных рядов, частоту и длину - PullRequest
0 голосов
/ 16 мая 2018

Я пытаюсь связать несколько переменных, которые я создал ранее.Тем не менее, даты начала и окончания временного ряда не равны среди переменных.Я пытаюсь сделать это следующим образом:

data.start <- c(1960,1)
data.end <- c(2018,2)
data.out <- window(cbind(gdp.log.ger, interest.ger, inflation, inflation.exp.ger),start = data.start, 
                   end = data.end)
write.table(data.out,file = 'InputData/rstar.data.ge.csv', sep = ',',
            col.names = c("gdp.log","inflation","inflation.expectations", "interest"),
            quote = FALSE, na = '.', row.names = FALSE)

Полученная ошибка:

Error in window.default(cbind(gdp.log.ger, interest.ger, inflation, inflation.exp.ger),  : 
  'start' cannot be after 'end'
In addition: Warning messages:
1: In cbind(gdp.log.ger, interest.ger, inflation, inflation.exp.ger) :
  number of rows of result is not a multiple of vector length (arg 1)
2: In window.default(cbind(gdp.log.ger, interest.ger, inflation, inflation.exp.ger),  :
  'end' value not changed

Возможно, это связано с тем, что даты начала и окончания не совпадаютсреди разных временных рядов?Обратите внимание, что для date.start и date.end установлены самые ранние и самые последние наблюдения.

PS.Чтобы воспроизвести проблему:

#------------------------------------------------------------------------------#
# File:        prepare.rstar.data.ger.R
#
# Description: This file prepares the data for Germany to use in the    
#              HLW methodology.
#------------------------------------------------------------------------------#
setwd("/Users/seanbagcik/Dropbox/Master Thesis (2017 - 2018)/R-Code") #set working directory

rm(list = ls()) # clear workspace

if (!require("tis")) {install.packages("tis"); library('tis')} # Load time series library

if (!require("seasonal")) {install.packages("seasonal"); library('seasonal')}
Sys.setenv(X13_PATH = "/Library/Frameworks/R.framework/Versions/3.3/Resources/library/x13binary/bin")

# library('forecast') # for seasonal adjustment
# install.packages("forecast"); 

#------------------------------------------------------------------------------#
# Import raw data: GDP
#------------------------------------------------------------------------------#
gdp.start <- c(1991,1) # type "double"
gdp.end   <- c(2017,4)

gdp.raw <- "rawData/germany_gdp.csv"
gdp.table <- read.table(gdp.raw, skip = 1, header = F, sep = ',', stringsAsFactors = F)
gdp.ger <- ts(gdp.table[,2], start = gdp.start, frequency = 4) # time-series representation

#------------------------------------------------------------------------------#
# Import raw data: inflation
#------------------------------------------------------------------------------#
inflation.start <- c(1960,1)
inflation.end <- c(2018,1)

inflation.raw <- "rawData/germany_inflation.csv"
inflation.table <- read.table(inflation.raw, skip = 1, header = F, sep = ',', stringsAsFactors = F)
inflation.ger <- ts(inflation.table[,2], start = inflation.start, frequency = 4)

inflation.seasadj.ger <- final(seas(as.ts(naWindow(inflation.ger),freq=4))) # seasonal adjustment
inflation.seasadj.ger <- as.tis(cpi,start=inflation.start,tif='quarterly')

# Measure inflation expectations: 4-quarter moving average of past inflation:
inflation.exp.ger <- (inflation.seasadj + Lag(inflation.seasadj, k=1) + Lag(inflation.seasadj, k=2) +
                             Lag(inflation.seasadj, k=3))/4 

#------------------------------------------------------------------------------#
# inflation.fit <- auto.arima(inflation, ic = 'aic') # fit ARIMA model
# plot(forecast(inflation.fit,h=20)) # forecasting
# inflation.seasadj <- seasadj(decompose(inflation.fit, 'multiplicative'))
# inflation.ge <- 400*log(cpi/Lag(cpi, k=1)) # create annual inflation series
#------------------------------------------------------------------------------#

#------------------------------------------------------------------------------#
# Import raw data: short-term nominal interest rate
#------------------------------------------------------------------------------#  
interest.start <- c(1960,2)
interest.end <- c(2018,2)

interest.raw <- 'rawData/germany_interest.csv'
interest.table <- read.table(interest.raw, skip = 1, header = F, sep = ',', stringsAsFactors = F)
interest.m <- ts(interest.table[,2], start = interest.start, frequency = 12) # monthly time-series

interest <- convert(interest.m, tif ="quarterly", observed ="averaged") # monthly to quaterly frequency
interest <- final(seas(as.ts(naWindow(interest),freq=4))) # seasonal adjustment
interest <- as.tis(interest,start=interest.start,tif='quarterly')

interest.ger <- 100*((1+interest/36000)^365 -1) #  365-day annualized basis

#------------------------------------------------------------------------------#
# Prepare Data
#------------------------------------------------------------------------------#

# Take log of real GDP
gdp.log.ger <- log(gdp.ger)

#------------------------------------------------------------------------------#
# Output Data
#------------------------------------------------------------------------------#
data.start <- c(1960,1)
data.end <- c(2018,2)
data.out <- window(cbind(gdp.log.ger, inflation.seasadj.ger, inflation.exp.ger, interest.ger),
                   start = data.start, end = data.end)
write.table(data.out,file = 'InputData/rstar.data.ge.csv', sep = ',',
            col.names = c("gdp.log","inflation","inflation.expectations", "interest"),
            quote = FALSE, na = '.', row.names = FALSE)

С наборами данных: R-Data

Ответы [ 2 ]

0 голосов
/ 16 мая 2018

Это работает:

#------------------------------------------------------------------------------#
# File:        prepare.rstar.data.ger.R
#
# Description: This file prepares the data for Germany to use in the    
#              HLW methodology.
#------------------------------------------------------------------------------#
setwd("/Users/seanbagcik/Dropbox/Master Thesis (2017 - 2018)/R-Code") #set working directory

rm(list = ls()) # clear workspace

if (!require("tis")) {install.packages("tis"); library('tis')} # Load time series library

Sys.setenv(X13_PATH = "/Library/Frameworks/R.framework/Versions/3.3/Resources/library/x13binary/bin")
if (!require("seasonal")) {install.packages("seasonal"); library('seasonal')}

# library('forecast')
# install.packages("forecast"); 

#------------------------------------------------------------------------------#
# Import raw data: GDP
#------------------------------------------------------------------------------#
gdp.start <- c(1991,1) # type "double"
gdp.end   <- c(2017,4)

gdp.raw <- "rawData/germany_gdp.csv"
gdp.table <- read.table(gdp.raw, skip = 1, header = F, sep = ',', stringsAsFactors = F)
gdp.ger <- ts(gdp.table[,2], start = gdp.start, frequency = 4) # time-series representation

#------------------------------------------------------------------------------#
# Import raw data: inflation
#------------------------------------------------------------------------------#
inflation.start <- c(1960,1)
inflation.end <- c(2018,1)

inflation.raw <- "rawData/germany_inflation.csv"
inflation.table <- read.table(inflation.raw, skip = 1, header = F, sep = ',', stringsAsFactors = F)
inflation.ger <- ts(inflation.table[,2], start = inflation.start, frequency = 4)

inflation.seasadj.ger <- final(seas(as.ts(naWindow(inflation.ger),freq=4))) # seasonal adjustment
inflation.seasadj.ger <- ts(inflation.seasadj.ger, start = inflation.start, frequency = 4)

# Measure inflation expectations: 4-quarter moving average of past inflation:
inflation.exp.ger <- (inflation.seasadj.ger + Lag(inflation.seasadj.ger, k=1) + 
                        Lag(inflation.seasadj.ger, k=2) + Lag(inflation.seasadj.ger, k=3))/4 

#------------------------------------------------------------------------------#
# inflation.fit <- auto.arima(inflation, ic = 'aic') # fit ARIMA model
# plot(forecast(inflation.fit,h=20)) # forecasting
# inflation.seasadj <- seasadj(decompose(inflation.fit, 'multiplicative'))
# inflation.ge <- 400*log(cpi/Lag(cpi, k=1)) # create annual inflation series
#------------------------------------------------------------------------------#

#------------------------------------------------------------------------------#
# Import raw data: short-term nominal interest rate
#------------------------------------------------------------------------------#  
interest.start <- c(1960,2)
interest.end <- c(2018,2)

interest.raw <- 'rawData/germany_interest.csv'
interest.table <- read.table(interest.raw, skip = 1, header = F, sep = ',', stringsAsFactors = F)
interest.m <- ts(interest.table[,2], start = interest.start, frequency = 12) # monthly time-series

interest <- convert(interest.m, tif ="quarterly", observed ="averaged") # monthly to quaterly frequency
interest <- final(seas(as.ts(naWindow(interest),freq=4))) # seasonal adjustment
interest <- ts(interest, start = interest.start, frequency = 4)

interest.ger <- 100*((1+interest/36000)^365 -1) #  365-day annualized basis

#------------------------------------------------------------------------------#
# Prepare Data
#------------------------------------------------------------------------------#

# Take log of real GDP
gdp.log.ger <- log(gdp.ger)

#------------------------------------------------------------------------------#
# Output Data
#------------------------------------------------------------------------------#
# save(gdp.log.ger, inflation.seasadj.ger, inflation.exp.ger, interest.ger, file="data_ger.RData")

data.start <- c(1960,1)
data.end <- c(2018,2)
data.out <- window(cbind(gdp.log.ger, inflation.seasadj.ger, inflation.exp.ger, interest.ger),
                   start = data.start, end = data.end)
write.table(data.out,file = 'InputData/rstar.data.ge.csv', sep = ',',
            col.names = c("gdp.log","inflation","inflation.expectations", "interest"),
           quote = FALSE, na = '.', row.names = FALSE)

DATA

0 голосов
/ 16 мая 2018

Основная проблема, как вы выяснили, заключается в том, что четыре временных ряда 'имеют совершенно разные типы, частоты и длины.Таким образом, решение состояло в том, чтобы гомогенизировать, но это было немного больше работы, чем я ожидал.Обычно эти вещи можно автоматизировать больше.

Я решил превратить квартальные временные ряды в месячные, а не наоборот.Поскольку они дополняются только NA, но сплайн / линейная / локальная интерполяция довольно прямолинейна.

Редактировать: С некоторыми дополнительными изменениями мне удалось упростить вещибит

library(tis)
library(zoo)
library(xts)
library(devtools)

source_gist("https://gist.github.com/AkselA/942097c99bfa22ddc2e3d68d8a198ab8",
  filename="data_ger.r")

# homogenize data types (all zoo yearmon)
gdp.log.ger.z <- zoo(gdp.log.ger)
index(gdp.log.ger.z) <- as.yearmon(index(gdp.log.ger.z))
inflation.seasadj.ger.z <- as.zooreg(inflation.seasadj.ger, class="yearmon")
inflation.exp.ger.z <- as.zooreg(inflation.exp.ger, class="yearmon")
interest.ger.z <- as.zooreg(interest.ger, class="yearmon")

# quick and dirty merge, brings everything to monthly
mrg <- merge(gdp.log.ger.z, inflation.seasadj.ger.z, 
             inflation.exp.ger.z, interest.ger.z)
mrg <- na.approx(mrg) 
colnames(mrg) <- c("gdp.log", "inflation", "inflation.expectations", "interest")

# aggregate to quarterly
mrg.q <- aggregate(mrg, by=yearqtr, FUN=mean)
rownames(mrg.q) <- NULL

# crop all NA at beginning and end
be <- max(apply(mrg.q, 2, function(x) min(which(!is.na(x)))))
en <- min(apply(mrg.q, 2, function(x) max(which(!is.na(x)))))
mrg.q <- mrg.q[be:en,]

# write csv
write.csv(ll.z, file="data.csv", quote=FALSE, na=".", row.names=FALSE)

# plot
e <- local({ 
   mtext <- function(...) graphics::mtext(..., cex = 0.8) 
   environment(plot.zoo) <-  environment() 
}) 
with(e, plot.zoo)(mrg.q, oma=c(2, 0, 2, 0), cex.axis=0.8) 

enter image description here

...