2019年8月28日水曜日

ggplot() data.frame() geom="bar" ggplot2を使用した棒グラフ その2


w <- to.monthly(SP5)[,4][2:836]/as.vector(to.monthly(SP5)[,4][1:835])
w <- w-1
# will use "c(last(seq(1,12,1),month(index(last(apply.monthly(SP5[,4],sd)))) ),head(seq(1,12,1),12-month(index(last(apply.monthly(SP5[,4],sd)))) ))" in the future
#
ggplot(data.frame(data=apply(matrix(last(w,floor(length(w)/12)*12),nrow=12),1,mean)[c(5,6,7,8,9,10,11,12,1,2,3,4)],mon=c("01jan","02feb","03mar","04apr","05may","06jun","07jul","08aug","09sep","10oct","11nov","12dec")),aes(x=mon,y=data,fill =mon)) +
scale_x_discrete(label=c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")) + #x-axis label
stat_summary(fun.y=NULL,geom="bar") + # speciy "bar" graph
theme(legend.position = 'none')  # erase legend


# the final version

use "c(last(seq(1,12,1),month(index(last(apply.monthly(SP5[,4],sd)))) ),head(seq(1,12,1),12-month(index(last(apply.monthly(SP5[,4],sd)))) ))"
# to calculate the order of months like "c(5,6,7,8,9,10,11,12,1,2,3,4)" for August.
#
len <- length(index(to.monthly(SP5)))
w <- to.monthly(SP5)[,4][2:len]/as.vector(to.monthly(SP5)[,4][1:(len-1)])
w <- w-1
# need factor() to set descrete index, other wise continus/gradation will be used as color scheme.
#
df <- data.frame(data=apply(matrix(last(w,floor(length(w)/12)*12),nrow=12),1,mean)[  c(last(seq(1,12,1),month(index(last(apply.monthly(SP5[,4],sd)))) ),head(seq(1,12,1),12-month(index(last(apply.monthly(SP5[,4],sd)))) )) ],mon=factor(seq(1,12,1)))
p <- ggplot(df, aes(x=mon,y=data,fill =mon)) 
p <- p + scale_x_discrete(label=c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))  #x-axis label
p <- p + geom_bar(stat = "identity") # need identity to draw value itself. 
p <- p + theme(legend.position = 'none')  # erase legend
plot(p)





ggplot() data.frame() geom="bar" ggplot2を使用した棒グラフ



library(ggplot2)
w <- c();for(i in seq(1,12,1)){w <- append(w,apply.monthly(SP5[,4],sd)[seq(i,length(index(apply.monthly(SP5[,4],sd))),12)])}
# when > month(index(last(apply.monthly(SP5[,4],sd)))) = 8. numeric sequence for months is 
# c(5,6,7,8,9,10,11,12,1,2,3,4)
ggplot(data.frame(data=apply(matrix(last(w,floor(length(w)/12)*12),nrow=12),1,mean)[c(5,6,7,8,9,10,11,12,1,2,3,4)],mon=c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")),aes(x=mon,y=data)) +
 stat_summary(fun.y=NULL,geom="bar")


STILL WORK IN PROGRESS



  w <- c();for(i in seq(1,12,1)){w <- append(w,apply.monthly(SP5[,4],sd)[seq(i,length(index(apply.monthly(SP5[,4],sd))),12)])}
  # when > month(index(last(apply.monthly(SP5[,4],sd)))) = 8. numeric sequence for months is 
  # c(5,6,7,8,9,10,11,12,1,2,3,4)

still unable to fix x axis label order

ggplot(data.frame(data=apply(matrix(last(w,floor(length(w)/12)*12),nrow=12),1,mean)[c(5,6,7,8,9,10,11,12,1,2,3,4)],mon=c("01jan","02feb","03mar","04apr","05may","06jun","07jul","08aug","09sep","10oct","11nov","12dec")),aes(x=mon,y=data,fill =mon))+
# levels = c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))) +
    # order = c(5,6,7,8,9,10,11,12,1,2,3,4))) +
   stat_summary(fun.y=NULL,geom="bar")



library(ggplot2)
# ignore a next line complete nonsense.
# w <- c();for(i in seq(1,12,1)){w <-append(w,apply.monthly(SP5[,4],sd)[seq(i,length(index(apply.monthly(SP5[,4],sd))),12)])}
w <- apply.monthly(SP5[,4],sd)
  # when > month(index(last(apply.monthly(SP5[,4],sd)))) = 8. numeric sequence for months is   
  # c(5,6,7,8,9,10,11,12,1,2,3,4)
  # or execute c(last(seq(1,12,1),month(index(last(apply.monthly(SP5[,4],sd)))) ),head(seq(1,12,1),12-month(index(last(apply.monthly(SP5[,4],sd)))) ))

ggplot(data.frame(data=apply(matrix(last(w,floor(length(w)/12)*12),nrow=12),1,mean)[c(5,6,7,8,9,10,11,12,1,2,3,4)],mon=c("01jan","02feb","03mar","04apr","05may","06jun","07jul","08aug","09sep","10oct","11nov","12dec")),aes(x=mon,y=data,fill =mon))+
# levels = c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))) +
    # order = c(5,6,7,8,9,10,11,12,1,2,3,4))) +
scale_x_discrete(label=c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))+
stat_summary(fun.y=NULL,geom="bar")+
theme(legend.position = 'none')  # erase legend


# the final version
w <- apply.monthly(SP5[,4],sd)
# when > month(index(last(apply.monthly(SP5[,4],sd)))) = 8. numeric sequence for months is   
# c(5,6,7,8,9,10,11,12,1,2,3,4)
# or execute c(last(seq(1,12,1),month(index(last(apply.monthly(SP5[,4],sd)))) ),head(seq(1,12,1),12-month(index(last(apply.monthly(SP5[,4],sd)))) ))
#
df <- data.frame(data=apply(matrix(last(w,floor(length(w)/12)*12),nrow=12),1,mean)[     c(last(seq(1,12,1),month(index(last(apply.monthly(SP5[,4],sd)))) ),head(seq(1,12,1),12-month(index(last(apply.monthly(SP5[,4],sd)))) ))],mon=factor(seq(1,12,1)))
p <- ggplot(df, aes(x=mon,y=data,fill =mon)) 
p <- p + scale_x_discrete(label=c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))  #x-axis label
p <- p + geom_bar(stat = "identity") # need identity to draw value itself. 
p <- p + theme(legend.position = 'none')  # erase legend

plot(p)



なお、

>month(index(last(apply.monthly(SP5[,4],sd))))
[1] 8

c(last(seq(1,12,1),month(index(last(apply.monthly(SP5[,4],sd)))) ),head(seq(1,12,1),12-month(index(last(apply.monthly(SP5[,4],sd)))) ))

で、「5  6  7  8  9 10 11 12  1  2  3  4」を生成できる。

2019年8月19日月曜日

行列 matrix() 変換 その2


please also check this post.

目的

 指数日次終値データを格納する配列が存在し、その各日付データについて当日を含む過去100営業日分の範囲内で最高値を求めるものとする。通常の言語であればfor文をつかうところだが、Rの場合for文を使用すると処理が大変遅くなるので行列を使用する。

処理内容

各日付における過去100営業日以内の最高値の算出


  •  配列のサイズが17519のとき、切りが良いように20番目から17519番目の要素だけを抜き出す。
  •  行列「1」を作成する。列数100、byrow=t。
    • 1行目は21,22,23,,,,,,120。
    • 2行目は121,122,123,,,,,,220。
    • 最終174行目は17321,17322,17333,,,,,17420。
  • 行単位でapply()関数を使用して、max()それをapply(matrix,1,max)を使用し各行の最高値を出す。これで、120日目、220日目、、17420日目時点での最高値が記録される。
  •  インデックスを一つ進め同様に処理する。つまり、配列「2」を作成する。
    • 1行目は22,23,24,,,,,121。
    • 2行目は122,123,124,,,,,221。
    • 最終174行目は17322,17323,17324,,,,17421。
  • 同じくapply(matrix,1,max)を使用し各行の最高値を出す。これで、121日目、221日目、、17421日目時点での最高値が記録される。
  • これを最後まで繰り返す。
  • 各行ごとの出力結果をすべてappend()すると、120,220,,17420,121,221,,17421,,,,,219,319,,,17519日目の結果が格納された配列「w」ができる。


出力結果の並び替え

 配列「w」をmatrix(w,ncol=174,byrow=T)つかって行列に変換する。すると日付で言えば、

   120日目の結果,220日目の結果,,17420日目の結果
   121日目の結果,221日目の結果,,17421日目の結果
   ,,,,,
   219日目の結果,319日目の結果,,,17519日目の結果

となる行数=100x列数=174の配列ができる。
これをさらに、matrix(matrix(w,ncol=174,byrow=T),ncol=1,byrow = T)で列数=1の行列に変換後、as.vector()すると。「120,121,219,220,221,,,319,,,,17420,17421,,,,,17519日目」の結果を日付順に並べた配列が完成する。

行列 matrix() 変換


matrix(1:9, nrow=3, ncol=3)
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

数列(1:9)があるとき[1,1][2,1][3,1][1,2][1,3].....[3,3]の順に要素が充填される。これを列数が1の行列に変換すると。

matrix(matrix(1:9, nrow=3, ncol=3),ncol=1,byrow=T)
      [,1]
 [1,]    1
 [2,]    2
 [3,]    3
 [4,]    4
 [5,]    5
 [6,]    6
 [7,]    7
 [8,]    8
 [9,]    9

順序は保たれる。しかし、もとの数列が順序を保ってない場合、つまり、c(1,4,7,2,5,8,3,6,9)のときは、以下の行列ができる。

matrix(c(1,4,7,2,5,8,3,6,9), nrow=3, ncol=3,byrow=F) # byrow=F is default.
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

その場合、"byrow=T" を利用して要素の充填順序を変えてやらなくてはならない。

matrix(c(1,4,7,2,5,8,3,6,9), nrow=3, ncol=3,byrow=T)
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

そこで再び列数が1の行列に再変換してやる。再変換のときにはbyrow=Tはいらない。

matrix(matrix(c(1,4,7,2,5,8,3,6,9), nrow=3, ncol=3,byrow=T),ncol=1)
      [,1]
 [1,]    1
 [2,]    2
 [3,]    3
 [4,]    4
 [5,]    5
 [6,]    6
 [7,]    7
 [8,]    8
 [9,]    9

please refer to this post as well.

2019年8月18日日曜日

matrix




length(index(SP5))
# [1] 17519


  1. $SPX daily close pirce up to the date length is 17519. 
  2. In order to render the array into the matrix whose colmn size =100.
  3. pick up the data from 21th element to 17420th. use matrix(data,ncol=100,byrow=T)
  4. use apply() to excute max() to each row respectively and put it to the w.
  5. pick up data from 22th to 1742th and do the same procedure.
  6. repeat 23th,24th,25,,,,,,until120th.
  7. the result is array w.
  8. run "matrix(w,ncol=174,byrow=T)" to create the matrix col=174 and row=100. with the parameter "byrow=T", the elemnet of w is filler by the order of row.
  9. run "as.vector(matrix(matrix(w,ncol=174,byrow=T),ncol=1,byrow = T))" to transform to the single dimention array. row and column are converted here.




 w <- c();system.time(for(i in seq(0,99,1)){w <- append(w,apply(matrix(as.vector(SP5[,4])[(21+i):(17420+i)],ncol=100,byrow=T),1,max))})
   # ユーザ   システム       経過
   #   3.223      0.345      3.586

matrix(w,ncol=174,byrow=T)[100,]
#   [1]   20.12   22.20   23.28   24.66   24.60   25.55   26.66   25.06   26.09   29.19
#  [11]   33.54   37.52   43.76   46.41   48.87   49.64   47.04   49.13   45.44   44.64
#  [21]   51.62   56.67   60.71   60.39   58.40   58.07   64.86   68.68   72.64   71.06
#  [31]   59.78   66.52   70.80   75.67   81.16   85.24   87.63   90.27   92.63   94.06
#  [41]   87.61   88.29   94.75   97.59   98.91  104.99  108.37  106.16   98.33   90.23
#  [51]   86.89  101.21  104.77  104.05  110.66  112.55  120.24  112.68  111.44   99.74
#  [61]   93.10   86.01   95.61   93.53  103.51  107.83  107.46  102.17   98.92   99.60
#  [71]  106.99  104.66  104.47  111.27  118.44  130.40  140.52  137.11  133.85  124.00
#  [81]  124.88  148.93  170.99  172.65  169.28  168.87  182.19  192.43  201.52  244.74
#  [91]  253.83  276.45  309.65  336.77  271.37  275.81  289.14  326.95  359.80  359.69
# [101]  368.95  331.75  390.45  396.64  420.77  425.09  441.28  456.33  469.50  482.00
# [111]  471.06  476.07  528.61  586.77  661.45  678.51  757.03  841.88  983.12 1052.02
# [121] 1186.75 1192.33 1362.80 1418.78 1469.25 1527.46 1520.77 1380.20 1312.83 1172.51
# [131] 1170.29  962.70  931.66 1039.58 1157.76 1156.86 1191.37 1225.31 1245.04 1294.18
# [141] 1325.76 1406.09 1495.42 1553.08 1565.15 1426.63 1305.32  934.70 1044.14 1150.23
# [151] 1217.28 1225.85 1343.01 1363.61 1326.06 1419.04 1465.77 1587.73 1709.67 1848.38
# [161] 1956.98 2031.92 2117.39 2130.82 2109.79 2119.12 2190.15 2395.96 2480.91 2786.24
# [171] 2872.87 2930.75 2854.88 3025.86
matrix(w,ncol=174,byrow=T)[,174]
 #  [1] 2854.88 2854.88 2854.88 2867.19 2867.24 2873.40 2879.39 2892.74 2895.77 2895.77
 # [11] 2895.77 2895.77 2907.41 2907.41 2907.41 2907.41 2907.41 2907.97 2933.68 2933.68
 # [21] 2933.68 2939.88 2943.03 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83
 # [31] 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83
 # [41] 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83
 # [51] 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2954.18
 # [61] 2954.18 2954.18 2954.18 2954.18 2954.18 2954.18 2964.33 2973.01 2995.82 2995.82
 # [71] 2995.82 2995.82 2995.82 2999.91 3013.77 3014.30 3014.30 3014.30 3014.30 3014.30
 # [81] 3014.30 3014.30 3019.56 3019.56 3025.86 3025.86 3025.86 3025.86 3025.86 3025.86
 # [91] 3025.86 3025.86 3025.86 3025.86 3025.86 3025.86 3025.86 3025.86 3025.86 3025.86
 as.vector(matrix(w,ncol=174,byrow=T))[17300:17400]
 #   [1] 2854.88 2854.88 2854.88 2854.88 2867.19 2867.24 2873.40 2879.39 2892.74 2895.77
 #  [11] 2895.77 2895.77 2895.77 2907.41 2907.41 2907.41 2907.41 2907.41 2907.97 2933.68
 #  [21] 2933.68 2933.68 2939.88 2943.03 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83
 #  [31] 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83
 #  [41] 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83
 #  [51] 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83 2945.83
 #  [61] 2954.18 2954.18 2954.18 2954.18 2954.18 2954.18 2954.18 2964.33 2973.01 2995.82
 #  [71] 2995.82 2995.82 2995.82 2995.82 2999.91 3013.77 3014.30 3014.30 3014.30 3014.30
 #  [81] 3014.30 3014.30 3014.30 3019.56 3019.56 3025.86 3025.86 3025.86 3025.86 3025.86
 #  [91] 3025.86 3025.86 3025.86 3025.86 3025.86 3025.86 3025.86 3025.86 3025.86 3025.86
 # [101] 3025.86
 matrix(w,ncol=174,byrow=T)[,173]
 #  [1] 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75
 # [11] 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75
 # [21] 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75
 # [31] 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75
 # [41] 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75
 # [51] 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75
 # [61] 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75
 # [71] 2930.75 2930.75 2929.67 2925.51 2925.51 2925.51 2925.51 2925.51 2925.51 2925.51
 # [81] 2925.51 2901.61 2885.57 2884.43 2880.34 2813.89 2813.89 2813.89 2813.89 2813.89
 # [91] 2813.89 2813.89 2822.48 2832.94 2832.94 2832.94 2854.88 2854.88 2854.88 2854.88
max(as.vector(SP5[,4])[(173*100+20):(173*100+119)])
# [1] 2854.88
as.vector(matrix(w,ncol=174,byrow=T))[17201:17300]
 #  [1] 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75
 # [11] 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75
 # [21] 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75
 # [31] 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75
 # [41] 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75
 # [51] 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75
 # [61] 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75 2930.75
 # [71] 2930.75 2930.75 2929.67 2925.51 2925.51 2925.51 2925.51 2925.51 2925.51 2925.51
 # [81] 2925.51 2901.61 2885.57 2884.43 2880.34 2813.89 2813.89 2813.89 2813.89 2813.89
 # [91] 2813.89 2813.89 2822.48 2832.94 2832.94 2832.94 2854.88 2854.88 2854.88 2854.88





> max(as.vector(SP5[,4])[(149*100+20):(149*100+119)])
[1] 1044.14
> as.vector(matrix(matrix(w,ncol=174,byrow=T),ncol=1,byrow = T))[(149*100)]
[1] 1044.14
> max(as.vector(SP5[,4])[(19*100+20):(19*100+119)])
[1] 45.44
> as.vector(matrix(matrix(w,ncol=174,byrow=T),ncol=1,byrow = T))[(19*100)]
[1] 45.44


2019年8月5日月曜日

EPS 2019AUG05



~$tac eps.txt | awk '{gsub("\\$","",$NF);print "eps_year_xts[\"2019::\"]["NR"] <- "$NF}'
eps_year_xts["2019::"][1] <- 134.39
eps_year_xts["2019::"][2] <- 135.40
eps_year_xts["2019::"][3] <- 137.01
eps_year_xts["2019::"][4] <- 147.41
eps_year_xts["2019::"][5] <- 150.65
eps_year_xts["2019::"][6] <- 156.72
eps_year_xts["2019::"][7] <- 161.90
eps_year_xts["2019::"][8] <- 167.23

update again at 2019AUG16.

$tac eps.txt | awk '{gsub("\\$","",$NF);print "eps_year_xts[\"2019::\"]["NR"] <- "$NF}'
eps_year_xts["2019::"][1] <- 134.39
eps_year_xts["2019::"][2] <- 135.89
eps_year_xts["2019::"][3] <- 137.11
eps_year_xts["2019::"][4] <- 147.45
eps_year_xts["2019::"][5] <- 150.86
eps_year_xts["2019::"][6] <- 156.20
eps_year_xts["2019::"][7] <- 161.78

eps_year_xts["2019::"][8] <- 167.39


> eps_year_xts["2019::"]
             [,1]
2019-01-01 134.39
2019-04-01 135.40
2019-07-01 137.01
2019-10-01 147.41
2020-01-01 150.65
2020-04-01 156.72
2020-07-01 161.90
2020-10-01 167.23
> eps_year_xts["2019::"][1] <- 134.39
> eps_year_xts["2019::"][2] <- 135.89
> eps_year_xts["2019::"][3] <- 137.11
> eps_year_xts["2019::"][4] <- 147.45
> eps_year_xts["2019::"][5] <- 150.86
> eps_year_xts["2019::"][6] <- 156.20
> eps_year_xts["2019::"][7] <- 161.78
> eps_year_xts["2019::"][8] <- 167.39
> eps_year_xts["2019::"]
             [,1]
2019-01-01 134.39
2019-04-01 135.89
2019-07-01 137.11
2019-10-01 147.45
2020-01-01 150.86
2020-04-01 156.20
2020-07-01 161.78

2020-10-01 167.39

2019年8月3日土曜日

cbind, append and merge



> last(GSPC.predict)
        SP5.Open SP5.High SP5.Low SP5.Close  SP5.Volume  spline      eps
 6 2019  2751.53  2964.15 2728.81   2941.76 70881390000 3073.12 2873.672
> as.xts(cbind(1,2,3,4,5,6,7),as.Date("2019-07-01"))
           [,1] [,2] [,3] [,4] [,5] [,6] [,7]
2019-07-01    1    2    3    4    5    6    7
> append(last(GSPC.predict,12),as.xts(cbind(1,2,3,4,5,6,7),as.Date("2019-07-01")))
        SP5.Open SP5.High SP5.Low SP5.Close  SP5.Volume   spline      eps
 7 2018  2704.95  2848.03 2698.95   2816.29 64542170000 2755.988 2655.353
 8 2018  2821.17  2916.50 2796.34   2901.52 69238220000 2782.479 2683.418
 9 2018  2896.96  2940.91 2864.12   2913.98 62492080000 2803.764 2707.010
10 2018  2926.29  2939.86 2603.54   2711.74 91327930000 2819.118 2723.880
11 2018  2717.58  2815.15 2631.09   2760.17 80080110000 2831.201 2737.248
12 2018  2790.50  2800.18 2346.58   2506.85 83519570000 2843.346 2751.415
 1 2019  2476.96  2708.95 2443.96   2704.10 80391630000 2858.716 2769.494
 2 2019  2702.32  2813.49 2681.83   2784.49 70183430000 2879.906 2790.537
 3 2019  2798.22  2860.31 2722.27   2834.40 78596280000 2909.390 2812.729
 4 2019  2848.63  2949.52 2848.63   2945.83 69604840000 2949.670 2834.471
 5 2019  2952.33  2954.13 2750.52   2752.06 76860120000 3003.365 2854.984
 6 2019  2751.53  2964.15 2728.81   2941.76 70881390000 3073.120 2873.672
 7 2019     1.00     2.00    3.00      4.00           5    6.000    7.000

> last(append(tmp.predict[,-236],cbind(last(SP5)[,-5],1,2)))
        SP5.Open SP5.High SP5.Low SP5.Close SP5.Volume spline eps
 8 2019   2943.9   2945.5 2914.11   2932.05 2210278484      1   2

なので、

> last(append(GSPC.predict[,-5],cbind(last(to.monthly(SP5),2)[,-5],2,1)),5)
        SP5.Open SP5.High SP5.Low SP5.Close   spline      eps
 4 2019  2848.63  2949.52 2848.63   2945.83 2950.103 2833.925
 5 2019  2952.33  2954.13 2750.52   2752.06 3002.397 2852.558
 6 2019  2751.53  2964.15 2728.81   2941.76 3070.108 2868.224
 7 2019  2971.41  3027.98 2952.22   2980.38    2.000    1.000
 8 2019  2980.32  3013.59 2914.11   2932.05    2.000    1.000

のように2ヶ月分いっぺんにappendできる。

ゆえに、以下の操作で2ヶ月分付加。

last(append(GSPC.predict,cbind(last(to.monthly(SP5),2),2,1)),5)
#
#
tmp.predict <- append(GSPC.predict,cbind(last(to.monthly(SP5),2),2,1))
length(index(tmp.predict))



tmp.predict <- append(GSPC.predict[,-5],cbind(last(to.monthly(SP5),2)[,-5],2,1))

が下の式のほうがよりベター。ヶ月分。

tmp.predict <- append(tmp.predict,cbind(last(to.monthly(SP5)),1,2))
length(index(tmp.predict))

1月,4月,7月,10月用。

tmp.predict <- append(GSPC.predict,cbind(last(to.monthly(SP5)),1,2))

tmp.predict <- append(GSPC.predict,cbind(last(to.monthly(SP5),2),c(1,3),c(2,4)))

tmp.predict <- append(GSPC.predict,cbind(last(to.monthly(SP5),3),c(1,3,5),c(2,4,6)))


以下、未検証。

2月,5月,8月,11月用


tmp.predict <-append(GSPC.predict,cbind(last(to.monthly(SP5),2),

c(as.vector(last(tmp.predict[,6],2)[1]),1),
c(as.vector(last(tmp.predict[,7],2)[1]),2)))

tmp.predict <-append(GSPC.predict,cbind(last(to.monthly(SP5),2),

c(as.vector(last(tmp.predict[,6],2)[1]), as.vector(last(tmp.predict[,6],2)[2]) ),

c(as.vector(last(tmp.predict[,7],2)[1]), as.vector(last(tmp.predict[,7],2)[2]) )))

3月,6月,9月,12月用。


tmp.predict <-

append(GSPC.predict,cbind(last(to.monthly(SP5),3),
c(as.vector(last(tmp.predict[,6],2)[1]),
as.vector(last(tmp.predict[,6],2)[2]),
1),
c(as.vector(last(tmp.predict[,7],2)[1]),
as.vector(last(tmp.predict[,7],2)[2]),
2)))