Skip to content

Instantly share code, notes, and snippets.

@mbusigin
Created September 7, 2017 00:42
Show Gist options
  • Save mbusigin/b2f552c6b5f70a6c415adf837bc7e37e to your computer and use it in GitHub Desktop.
Save mbusigin/b2f552c6b5f70a6c415adf837bc7e37e to your computer and use it in GitHub Desktop.
R function to return cyclically-adjusted series using labour & GDP output gaps
labour_output_gap = function()
{
if ( exists("UNRATE") == FALSE ) { fred("UNRATE", environment()) }
if ( exists("NROU") == FALSE ) { fred("NROU", environment()) }
a = na.trim(na.locf(merge(UNRATE, NROU)))
a = a[endpoints(a)]
return(UNRATE - NROU)
}
cyclical_adjust = function(p, endpoints.periodicity="months")
{
LOG = labour_output_gap()
fred(c("GDPC96", "GDPPOT"), environment())
og = GDPPOT/GDPC96
a = na.omit( na.locf(merge(p, LOG, og)) )
a = a[endpoints(a), on=endpoints.periodicity]
m = lm(a[,1] ~ a[,2] + a[,3])
print(summary(m))
a$model = predict(m)
return(a[,1] - a$model)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment