Thursday, August 24, 2017

Fred on R

When you are trying to handle the data fed from public data source like Google Finance and Fred on R, the library quantmod is absolutely useful. Here, their manual says Yahoo Finance is also available, but now it's not because of protocol change. Also, tickets only in the US seems available, not London nor Tokyo.

It’s just one line command:
> getSymbols(‘GDPC1’, src='FRED')

Then, the data is saved in the time series object named same as the symbol name, so you can draw a graph like:
> chartSeries(GDPC1)

NA field is filled with the following command:
> na.locf(GDPC1, fromLast = FALSE)
If you want to fill the data with the last observation, put the flag fromLast = TRUE.

The time frame can be specified intuitively:
> GDPC1[‘1999/2010-10’]
With this sample, the data is trimmed from the beginning of 1999 to the end of Oct, 2010.

Sampling with defined frequency period end is:
> endpoints(GDPC1, ‘years’)
This returns the index of the year end data, so in order to pick up the yearend data:
> GDPC1[endpoints(GDPC1, ‘years’)]

No comments: