Economic Data In R – Nonfarms Payroll & Other FED Data

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

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

non-farms payroll plot

non-farms payroll vs s&p 500 regression

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
#To see what the datasets are available from the FED goto the link below
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)
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){
  regression <- lm(y~x)
  plot(y~x,main=title, xlab=xlabel,ylab=ylabel)
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")

6 thoughts on “Economic Data In R – Nonfarms Payroll & Other FED Data

  1. (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.

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

  3. 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))

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

Leave a Reply

Your email address will not be published. Required fields are marked *