This post is a simple demo to show how to get economic data into R.The data comes from the Federal Reserve Bank of St Louis http://research.stlouisfed.org/fred2/.

I will show how to download Nonfarms Payroll numbers, although it is very easy to modify the code below to download GDP, CPI etc…

The top chart shows non-farms plotted with the S&P 500. It is interesting to note that in the % change charts there is a crash in the market around mid 08 this is then followed by a crash in the non-farms numbers. Although not a very rigorous analysis it looks like non-farms numbers LAG the market.

The second chart regress the % change in payrolls with the % change in the S&P for the month. It is seen in the scatter plot that there is no clear relationship between payroll change and S&P change.

The second regression on the right takes this months payroll change and regress it against next months S&P return, ie try and see if the numbers from this month can tell us anything about the return in the S&P for the coming month. Payrolls don’t look very predictive at the 1month time horizon. I think a more interesting analysis would look at payrolls on the market over 10,20,30min horizons intraday.

Onto the code:

^{?}View Code RSPLUS

library("quantmod") #To see what the datasets are available from the FED goto the link below #http://research.stlouisfed.org/fred2/ economicData <- new.env() #Make a new environment for quantmod to store data in startDate = as.Date("2000-01-01") #Specify what date to get the prices from getSymbols("PAYEMS",src="FRED",env=economicData,from=startDate) #Payems is non-farms payrolls getSymbols("^GSPC",env=economicData,from=startDate) #S&P 500 economicData$PAYEMS <- window(economicData$PAYEMS,start=startDate) #Window our data (FRED ignores the from parameter above) :@ economicData$GSPC <- window(economicData$GSPC,start=startDate) #Window our data mergedData <- merge(economicData$PAYEMS,Cl(economicData$GSPC),all=FALSE) #join the two datasets based on their SHARED dates #Calculate the % diff mergedPercDiff<- mergedData mergedPercDiff$PAYEMS <- diff(mergedData$PAYEMS)/Lag(mergedData$PAYEMS) mergedPercDiff$GSPC.Close <- diff(mergedData$GSPC.Close)/Lag(mergedData$GSPC.Close) dev.new() par(mfrow=c(2,2)) plot(mergedData$PAYEMS, main="Non-Farm Payrolls",ylab="Thousands of Persons") plot(mergedPercDiff$PAYEMS, main="Non-Farm Payrolls", ylab="% Change") plot(mergedData$GSPC.Close, main="S&P 500 Close",ylab="Close Price") plot(mergedPercDiff$GSPC.Close, main="&P 500 Close",ylab="% Change") #Function to plot data and add regression line doPlot <- function(x,y,title,xlabel,ylabel){ x<-as.vector(x) y<-as.vector(y) regression <- lm(y~x) print(regression) plot(y~x,main=title, xlab=xlabel,ylab=ylabel) abline(regression,col="red",lwd=1.5) legend("bottomleft",paste("y=",regression$coefficients[2],"x+",regression$coefficients[1],sep=""),bg="lightblue") } dev.new() par(mfrow=c(1,2)) doPlot(mergedPercDiff$PAYEMS,mergedPercDiff$GSPC.Close,"Regress Non-Farms Payroll with S&P Monthly Returns","Non-Farms Monthly % Change","S&P 500 Monthly % Change") doPlot(Lag(mergedPercDiff$PAYEMS),mergedPercDiff$GSPC.Close,"Regress Non-Farms Payroll with NEXT MONTH'S S&P Monthly Return","Non-Farms Monthly % Change","S&P 500 Monthly % Change") |

(1) Useful as usual.

(2) NFP numbers are not lagging. What is pulled is the revised series, and revisions are substantial. The actual ‘market-mover’ numbers are different.

Great comment on (2).

Certainly in the short term NFP can be ‘predictive’, but thats more trading the numbers relative to consensus. Although these days the moves tend to happen before the numbers even come out http://www.zerohedge.com/news/2013-12-06/gold-gets-jobs-leak-early-again and http://www.zerohedge.com/news/2013-08-02/presenting-todays-blatant-bond-market-manipulation-or-bls-leak

E do you trade NFP?

Regulation does not allow this as I am considered an insider.

Pingback: Daily Wrap for 5/5/2014 | The Whole Street

Hey!

Thanks for another awesome post.

Just one little problem I thought I’d point out. When merging the data, the dates where the exchange is closed and the NFP date coincides, the NFP will not be added. Thus, looking at mergedData, there are some gaps.

Fix (add before merging data):

dates <- range(time(economicData$GSPC))

emp <- zoo(,seq(from = dates[1], to = dates[2], by = "day"))

economicData$GSPC <- na.locf(merge(economicData$GSPC, emp))

Pingback: Economic Data In R – Nonfarms Payroll & Other FED Data | Gekko Quant – Quantitative Trading | Investment Times Daily