2018年8月14日火曜日

Use approx() to convert quarterly to monthly.


result <- lm(apply.quarterly(SP5[k2k],mean)[,1] ~ PAq[k2k] * UCq[k2k] * G[k2k]*CSq[k2k] - UCq[k2k] -G[k2k] - PAq[k2k]*G[k2k] - UCq[k2k]*G[k2k]*CSq[k2k])
SP5.result <- merge(residuals(result),predict(result),suffixes = c("resi","pred"))

or

tmp.result <- merge(residuals(result),predict(result),suffixes = c("resi","pred"))

the parameter "suffixes" improves readability.

> colnames(tmp.result)
[1] "resi" "pred"
> head(tmp.result)
                 resi     pred
2000-03-01   6.572655 1403.471
2000-06-01 138.085416 1319.118

> length(as.vector(SP5.result[,2]))
[1] 74

74 is equal to length(as.vector(SP5.result[,2])), which is quarterly basis as GDP is available only on that term.
The number of months between those 74 qtr. is 73 * 3 +1 = 220.
Thus it is able to convert quarterly basis data into monthly.


approx(seq(1,74,1),as.vector(SP5.result[,2]),n=220,method="linear")

Below calculates the gap between actual monthly high and monthly converted theoretical.

The last entry of SP5.result is 2018-06.

> last(SP5.result)
           residuals.result. predict.result.
2018-06-01         -186.2956        2851.332

The length between the start and 2018-06 is 138.

> length(to.monthly(GSPC)[,2]["::2018-06-30"])
[1]138


to.monthly(GSPC)[,2]["::2018-06-30"] - last(approx(seq(1,74,1),as.vector(SP5.result[,2]),n=220,method="linear")$y,n=138)

Below will draw the graph of the ratio actual price versus residuals.

plot(100*(to.monthly(GSPC)[,2]["::2018-06-30"] - last(approx(seq(1,74,1),as.vector(SP5.result[,2]),n=220,method="linear")$y,n=138))/to.monthly(GSPC)[,2]["::2018-06-30"])



> GSPC.predict <- merge(to.monthly(GSPC)["::2018-06-30"],last(approx(seq(1,74,1),as.vector(SP5.result[,2]),n=220,method="linear")$y,n=138))
> colnames(GSPC.predict)
[1] "GSPC.Open"                                                                                   
[2] "GSPC.High"                                                                                   
[3] "GSPC.Low"                                                                                   
[4] "GSPC.Close"                                                                                 
[5] "GSPC.Volume"                                                                                 
[6] "GSPC.Adjusted"                                                                               
[7] "last.approx.seq.1..74..1...as.vector.SP5.result...2....n...220..method....linear...y..n...138."
> colnames(GSPC.predict)[7] <- "theory"
> colnames(GSPC.predict)
[1] "GSPC.Open"     "GSPC.High"     "GSPC.Low"      "GSPC.Close"    "GSPC.Volume"   "GSPC.Adjusted"
[7] "theory"
> GSPC.predict
        GSPC.Open GSPC.High GSPC.Low GSPC.Close  GSPC.Volume GSPC.Adjusted    theory
 1 2007   1418.03   1441.61  1403.97    1438.24  56686200000       1438.24 1388.9681
 2 2007   1437.90   1461.57  1389.42    1406.82  51844990000       1406.82 1409.6371
 3 2007   1406.80   1438.89  1363.98    1420.86  67622250000       1420.86 1430.3061
    <skip>
 5 2018   2642.96   2742.24  2594.62    2705.27  75617280000       2705.27 2811.0006
 6 2018   2718.70   2791.47  2691.99    2718.37  77439710000       2718.37 2851.8821

> plot(GSPC.predict[,4] / GSPC.predict[,7])

The option "suffixes" to change column names on the fly, but seems to need 1st arg for no purpose.

> colnames(merge(to.monthly(GSPC)["::2018-06-30"],last(approx(seq(1,74,1),as.vector(SP5.result[,2]),n=220,method="linear")$y,n=138),suffixes=c("a","theory")))
[1] "GSPC.Open"     "GSPC.High"     "GSPC.Low"      "GSPC.Close"    "GSPC.Volume"   "GSPC.Adjusted" "theory"  

0 件のコメント: