Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

quantmod: buildData(,na.rm=FALSE) drops head of time series

Tags:

r

quantmod

I want to create a dataset from FRED series and I use the quantmod package like so:

library(quantmod)
getSymbols(c('FEDFUNDS', 'GDPPOT', 'DGS10'), src='FRED')
dat <- buildData(FEDFUNDS ~ DGS10 + GDPPOT, na.rm=FALSE)

What I need is an xts object with observations for all dates in the longest time series, and missing values to fill-in the shorter time series. In the example above, I get:

> head(dat, 2)
           FEDFUNDS DGS10 GDPPOT
1962-10-01     2.90  3.93 3141.6
1963-01-01     2.92    NA 3173.9
> head(FEDFUNDS, 2)
           FEDFUNDS
1954-07-01     0.80
1954-08-01     1.22
> head(DGS10, 2)
           DGS10
1962-01-02  4.06
1962-01-03  4.03
> head(GDPPOT, 2)
           GDPPOT
1949-01-01 1864.8
1949-04-01 1885.2

The FEDFUNDS series was truncated to match the minimum date value of the DGS10 series. I like the convenience of the buildData() function, and would love to use it for this task, but I'm wondering how I can keep missing observations.

Thanks a lot for your time!

EDIT: The reason I don't want to use merge is that some of the data series have different periodicity and that buildData() takes care of that automatically.

like image 405
Vincent Avatar asked May 03 '11 21:05

Vincent


2 Answers

buildData is not giving you what you want, in particular because of DGS10 being NA on holidays (including the first day of the year) and not having any entry for Sundays. So if you try what you had in the question, then you get the following oddity

> tail(dat,16)
           FEDFUNDS DGS10  GDPPOT
2005-07-01     3.26  4.06 12611.7
2007-01-01     5.25    NA 13072.4
2007-10-01     4.76  4.56 13314.1
2008-01-01     3.94    NA 13393.0
2008-04-01     2.28  3.57 13471.2
2008-07-01     2.01  4.01 13547.1
2008-10-01     0.97  3.77 13619.9
2009-01-01     0.15    NA 13689.2
2009-04-01     0.15  2.68 13753.1
2009-07-01     0.16  3.55 13813.7
2009-10-01     0.12  3.21 13872.0
2010-01-01     0.11    NA 13928.2
2010-04-01     0.20  3.89 13985.8
2010-07-01     0.18  2.96 14044.7
2010-10-01     0.19  2.54 14109.8
2011-04-01     0.10  3.46 14247.2

with several missing lines, including the whole of 2006. dat is a zoo object rather than xts.

If you are using GDPPOT then presumably you want to use quarterly data. Try this:

FEDFUNDSq <- aggregate(na.omit(FEDFUNDS), as.yearqtr, first)
DGS10q    <- aggregate(na.omit(DGS10),    as.yearqtr, first)
GDPPOTq   <- aggregate(na.omit(GDPPOT),   as.yearqtr, first)
dat2 <- as.xts(merge(FEDFUNDSq, DGS10q, GDPPOTq))

The as.xts() is only there because you requested it. This should give you NA where you want it and not where you do not. For example, the following looks better than the earlier tail.

> head(tail(dat2,66),25)
        FEDFUNDSq DGS10q GDPPOTq
2005 Q3      3.26   4.06 12611.7
2005 Q4      3.78   4.39 12684.6
2006 Q1      4.29   4.37 12758.9
2006 Q2      4.79   4.88 12835.2
2006 Q3      5.24   5.15 12913.0
2006 Q4      5.25   4.62 12992.1
2007 Q1      5.25   4.68 13072.4
2007 Q2      5.25   4.65 13153.1
2007 Q3      5.26   5.00 13233.9
2007 Q4      4.76   4.56 13314.1
2008 Q1      3.94   3.91 13393.0
2008 Q2      2.28   3.57 13471.2
2008 Q3      2.01   4.01 13547.1
2008 Q4      0.97   3.77 13619.9
2009 Q1      0.15   2.46 13689.2
2009 Q2      0.15   2.68 13753.1
2009 Q3      0.16   3.55 13813.7
2009 Q4      0.12   3.21 13872.0
2010 Q1      0.11   3.85 13928.2
2010 Q2      0.20   3.89 13985.8
2010 Q3      0.18   2.96 14044.7
2010 Q4      0.19   2.54 14109.8
2011 Q1      0.17   3.36 14178.3
2011 Q2      0.10   3.46 14247.2
2011 Q3        NA     NA 14316.8
like image 168
Henry Avatar answered Oct 19 '22 21:10

Henry


You could use merge.xts, since it pads with NA automatically:

library(quantmod)
getSymbols('FEDFUNDS;DGS10'head(, src='FRED')
dat <- merge(FEDFUNDS, DGS10)
head(dat)
#            FEDFUNDS DGS10
# 1954-07-01     0.80    NA
# 1954-08-01     1.22    NA
# 1954-09-01     1.06    NA
# 1954-10-01     0.85    NA
# 1954-11-01     0.83    NA
# 1954-12-01     1.28    NA
like image 21
Joshua Ulrich Avatar answered Oct 19 '22 21:10

Joshua Ulrich