I have several variables at the annual frequency in R that I would like to include in the regression analysis with other variables available at the quarterly frequency. In addition, I would like to be able to convert quarterly data back to annual frequency so as to reproduce the original annual data.
My current approach when converting from low-frequency and high-frequency time series data is to use the na.spline function in the zoo package. However, I do not see how to limit the quarterly data to the corresponding annual average. As a result, when I convert data from quarterly to annual frequency, I get annual values that differ from the original series.
Playable example:
library(zoo)
a <- as.numeric(c("100", "110", "111"))
b <- as.Date(c("2000-01-01", "2001-01-01", "2002-01-01"))
z_a <- zoo(a, b); z_a
end_z <- as.Date(as.yearqtr(end(z_a))+ 3/4)
z_q <- na.spline(z_a, xout = seq(start(z_a), end_z, by = "quarter"), method = "hyman")
c <- merge(z_a, z_q); c
d <- aggregate(c, as.integer(format(index(c),"%Y")), mean, na.rm=TRUE); d
Storing the original annual data is one solution, or I could convert back by taking the value of the first quarter as annual values. But any approach adds complexity, because I will need to track which of my quarterly series was originally converted from annual data.
I would prefer a solution in zoo or xts packages, but alternative suggestions are also welcome.
, № 1, .
yr <- format(time(c), "%Y")
c$z_q_adj <- ave(coredata(c$z_q), yr, FUN = function(x) x - mean(x) + x[1]); c
dat <- c%>%
data.frame(date=time(.), .) %>%
gather(variable, value, -date)
ggplot(data=dat, aes(x=date, y=value, group=variable, color=variable)) +
geom_line() +
geom_point() +
theme(legend.position=c(.7, .4)) +
geom_point(data = subset(dat,variable == "z_a"), colour="red", shape=1, size=7)
, . , 1, , Q4 Q1 (, 2001Q1 , ). . , , 1, . , .
:
- r. , na.approx na.spline, ?
- cobs ( "CONstrained B-Splines" ). , , , , . , , , .
- :
- Eviews, , " " .