2018年2月8日木曜日

Calculate NIKKEI225 vol.3



k3 <-  "2007-01-01::2018-02-07"

# or

getSymbols("^GSPC")
k3 <- paste("2007-01-01", index(last(GSPC)),sep="::")
k3

# download other data
getSymbols("NIKKEI225",src="FRED") # download nikkei 225
# getSymbols("DEXJPUS", src = "FRED")
# getSymbols("YJUSDJPY",src="yahooj")
getSymbols('YJUSDJPY', src="yahooj",auto.assign=TRUE)
N225 <- NIKKEI225
# result <summary(lm(to.monthly(N225[k3])[,4] ~  to.monthly(GSPC[k3])[,4] + to.monthly(DEXJPUS[k3])[,4]) 
result_nikkei <- lm(to.monthly(N225[k3])[,4] ~  to.monthly(GSPC[k3])[,4] + to.monthly(YJUSDJPY[k3])[,4])
result_nikkei$coefficients[2]*last(GSPC)[,4]+result_nikkei$coefficients[3]*as.vector(last(YJUSDJPY)[,4])+result_nikkei$coefficients[1]
# plot(merge(as.xts(predict(result_nikkei),index(residuals(result_nikkei))),to.monthly(N225[k3])[,4],residuals(result_nikkei)))
be
ep(2)

plot(merge(as.xts(predict(result_nikkei),index(residuals(result_nikkei))),to.monthly(N225[k3])[,4],residuals(result_nikkei)),main=paste(paste("NIKKEI225 =",round(result_nikkei$coefficients[2],4),"* GSPC +",round(result_nikkei$coefficients[3],2),"*USDJPY +",round(result_nikkei$coefficients[1],2))))

tmp.legend <- paste("R Squared is ",round(summary(result_nikkei)$r.squared,4)," \n","DF is ",round(summary(result_nikkei)$df[2],0),sep=' ')

addLegend(legend.loc = "topleft", legend.names = tmp.legend,col=3)

> result

Call:
lm(formula = to.monthly(N225[k3])[, 4] ~ to.monthly(GSPC[k3])[,
    4] + to.monthly(YJUSDJPY[k3])[, 4])

Residuals:
     Min       1Q   Median       3Q      Max
-1875.41  -473.74   -12.89   507.10  1855.32

Coefficients:
                                Estimate Std. Error t value Pr(>|t|) 
(Intercept)                   -9864.4555   499.7539  -19.74   <2e-16 ***
to.monthly(GSPC[k3])[, 4]         4.8991     0.1804   27.16   <2e-16 ***
to.monthly(YJUSDJPY[k3])[, 4]   158.2244     6.1444   25.75   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 792.4 on 131 degrees of freedom
Multiple R-squared:  0.9655, Adjusted R-squared:  0.965
F-statistic:  1832 on 2 and 131 DF,  p-value: < 2.2e-16


summary(lm(apply.weekly(N225[k3],mean) ~  apply.weekly(GSPC[k3][,4],mean) + apply.weekly(YJUSDJPY[k3][,4],mean)))   

> summary(lm(apply.weekly(N225[k3],mean) ~  apply.weekly(GSPC[k3][,4],mean) + apply.weekly(YJUSDJPY[k3][,4],mean)))   

Call:
lm(formula = apply.weekly(N225[k3], mean) ~ apply.weekly(GSPC[k3][,
    4], mean) + apply.weekly(YJUSDJPY[k3][, 4], mean))

Residuals:
     Min       1Q   Median       3Q      Max
-1992.66  -510.50    51.79   469.11  2524.40

Coefficients:
                                        Estimate Std. Error t value Pr(>|t|) 
(Intercept)                           -1.017e+04  2.593e+02  -39.24   <2e-16 ***
apply.weekly(GSPC[k3][, 4], mean)      4.800e+00  9.568e-02   50.17   <2e-16 ***
apply.weekly(YJUSDJPY[k3][, 4], mean)  1.626e+02  3.180e+00   51.11   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 754.9 on 445 degrees of freedom
  (132 observations deleted due to missingness)
Multiple R-squared:  0.9674, Adjusted R-squared:  0.9673
F-statistic:  6612 on 2 and 445 DF,  p-value: < 2.2e-16

# plot(merge(na.omit(apply.weekly(N225[k3],mean))[2:length(na.omit(apply.weekly(N225[k3],mean)))],predict(lm(apply.weekly(N225[k3],mean) ~  apply.weekly(GSPC[k3][,4],mean) + apply.weekly(YJUSDJPY[k3][,4],mean)))))

result <- lm(apply.weekly(N225[k3],mean) ~  apply.weekly(GSPC[k3][,4],mean) + apply.weekly(YJUSDJPY[k3][,4],mean)) 
tmp.main <- paste("NIKKEI225 =",round(result$coefficients[2],4),"* GSPC +",round(result$coefficients[3],2),"*USDJPY +",round(result$coefficients[1],2))
nikkei_week <- na.omit(apply.weekly(N225[k3],mean))
predict_nikkei_week <- predict(lm(apply.weekly(N225[k3],mean) ~  apply.weekly(GSPC[k3][,4],mean) + apply.weekly(YJUSDJPY[k3][,4],mean)))
if(length(nikkei_week) > length(predict_nikkei_week)){
  plot(merge(last(nikkei_week,n=length(predict_nikkei_week),predict_nikkei_week)),main=tmp.main)
}else{
  plot(merge(nikkei_week,last(predict_nikkei_week,n=length(nikkei_week))),main=tmp.main) 
}
# add legend at topleft
#
#
addLegend(legend.loc = "topleft", legend.names = paste("R Squared",round(summary(result)$r.squared,4),sep=':'),col=2)

or

tmp.legend <- paste("R Squared is ",round(summary(result)$r.squared,4)," \n","DF is ",round(summary(result)$df[2],0),sep=' ')
addLegend(legend.loc = "topleft", legend.names = tmp.legend,col=3)



In spite of the recent steep declines, Nikkei225 is still in the premium side.




use grid.ticks.on='months' to show grid on monthly basis.


0 件のコメント: