Part Finance Part R question
部分财务部分R问题
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
我一直在尝试使用Quantmod包复制下面的公式,xts也使用diff函数。代码给了我一个信贷冲动的情节,但它似乎并没有复制我想要的东西。见链接
https://www.gam.com/media/1434580/biggs.pdf -
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
第2页给出了信用冲动的公式 - 其中C是时刻t的信用存量
Credit impulset = (Ct – Ct-1)/GDPt – (Ct-1-Ct-2)/GDPt-1
信用冲击=(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
第3页看一下图表(这是我试图为Credit Impulse复制的图表
Am I using the diff function the right way also could I be doing this more efficiently in R?
我是否以正确的方式使用diff功能也可以在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)
1 个解决方案
#1
5
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?
假设Ct可以使用你的usd_debt系列进行建模?
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).
是的,你正在以正确的方式使用差异。当你将它应用到xts对象时,diff会调用diff.xts,在你的例子中,usd_ratio确实是一个xts对象,所以它会很快(高效)。
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.
在这里,coredata是可选的,但在划分xts对象时是很好的做法,因为它返回底层矩阵。 xts对象的划分可能有问题。
#1
5
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?
假设Ct可以使用你的usd_debt系列进行建模?
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).
是的,你正在以正确的方式使用差异。当你将它应用到xts对象时,diff会调用diff.xts,在你的例子中,usd_ratio确实是一个xts对象,所以它会很快(高效)。
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.
在这里,coredata是可选的,但在划分xts对象时是很好的做法,因为它返回底层矩阵。 xts对象的划分可能有问题。