Part Finance Part R question
I have been trying to replicate the below formula in R using the Quantmod package and xts also using the diff function. The code gives me a plot for credit impulse but it doesn't seem to be replicating what I am trying to get at. See link
https://www.gam.com/media/1434580/biggs.pdf -
page 2 give the formula for Credit Impulse - where C is the stock of credit at time t
Credit impulset = (Ct – Ct-1)/GDPt – (Ct-1-Ct-2)/GDPt-1
page 3 take a look at the graph( This is the graph I am trying to replicate for Credit Impulse
Am I using the diff function the right way also could I be doing this more efficiently in R?
below is my code
#US DEBT [BN][USD][Q]
usd_debt <- getSymbols("CRDQUSAPABIS", src = "FRED", auto.assign=FALSE)
##US GDP [BN][USD][Q]
usd_gdp <- getSymbols("GDP", src = "FRED", auto.assign=FALSE)
#USD Credit Impulse
usd_debt <- usd_debt["2000/2016"]
usd_gdp <- usd_gdp["2000/2016"]
usd_ratio <- usd_debt/usd_gdp
usd_ci <- diff(usd_ratio)
plot(usd_ci)
Looks like you may actually want to use:
z <- diff(diff(usd_debt) / coredata(usd_gdp))
plot(z)
assuming Ct
can be modeling using your usd_debt
series?
Yes you're using diff
the right way. diff
will call diff.xts
when you apply it to an xts object, and in your example usd_ratio
is indeed an xts object, so it will be fast (efficient).
Here, coredata
is optional but good practice when dividing by xts objects, as it returns the underlying matrix instead. Division for xts
objects can be problematic.