ラベル R の投稿を表示しています。 すべての投稿を表示
ラベル R の投稿を表示しています。 すべての投稿を表示

2021年3月14日日曜日

金利 NASDAQ 相関 cor.test() intersect()


choose date both TNX and NDX delta is available.


 d <- intersect(index(diff(TNX[,4])[!is.na(diff(TNX[,4]))]), index(diff(NDX[,4])[!is.na(diff(NDX[,4]))]))

check outputs.


head(as.Date(d))

[1] "2007-01-04" "2007-01-05" "2007-01-09" "2007-01-10" "2007-01-11" "2007-01-12"

Check correlation in 2021.


cor.test(diff(TNX[,4])["2021"][as.Date(d)],diff(NDX[,4])["2021"][as.Date(d)])

Pearson's product-moment correlation
data:  diff(TNX[, 4])["2021"][as.Date(d)] and diff(NDX[, 4])["2021"][as.Date(d)]
t = -2.3358, df = 36, p-value = 0.02519
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.61153962 -0.04874962
sample estimates:
       cor 
-0.3627793 
 警告メッセージ: 
 z + c(-1, 1) * sigma * qnorm((1 + conf.level)/2) で: 
  Recycling array of length 1 in array-vector arithmetic is deprecated.
  Use c() or as.vector() instead.

Check correlation all time.


cor.test(diff(TNX[,4])[as.Date(d)],diff(NDX[,4])[as.Date(d)])

Pearson's product-moment correlation
data:  diff(TNX[, 4])[as.Date(d)] and diff(NDX[, 4])[as.Date(d)]
t = 9.819, df = 2787, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.1467388 0.2184915
sample estimates:
      cor 
0.1828587 
 警告メッセージ: 
 z + c(-1, 1) * sigma * qnorm((1 + conf.level)/2) で: 
  Recycling array of length 1 in array-vector arithmetic is deprecated.
  Use c() or as.vector() instead.


> plot.default(diff(TNX[,4])[as.Date(d)],diff(NDX[,4])[as.Date(d)])
> plot.default(diff(TNX[,4])["2021"][as.Date(d)],diff(NDX[,4])["2021"][as.Date(d)])

2021年2月19日金曜日

ゼロサプレス zero supress bash awk split 文字列分割

 bash

$ seq -f %03g 1 10

001
002
003
004
005
006
007
008
009
010


ループ  loop

for i in `seq -f %03g 1 90`; do wget https://blog-imgs-77-origin.fc2.com/u/r/u/urutoraerogazou/matsushima-kaede-166-$i.jpg ; done


for i in `seq 414 453`; do wget  https://blog-imgs-76-origin.fc2.com/d/e/n/densetsuav/1543tachibanarikopin-$i.jpg ; done


二重ループ double loop サンプル

for j in `seq 1 34`
    do for i in `seq 1 12`
      # do wget -r https://javtube.com/javpic/ameri-ichinose/$j/ameri-ichinose-$i.jpg
      do wget -r https://jjgirls.com/japanese/rin-aikawa/$j/rin-aikawa-$i.jpg
  done
done




R


 for(i in seq(1,47,1)){ colnames(mdf)[i] <-  (paste(sprintf("%02d",i),colnames(mdf)[i],sep=""))}


sprintf("%.2f",100.0111)

[1] "100.01"

sprintf("%.3f",100.0111)

[1] "100.011"

sprintf("%.5f",100.0111)

[1] "100.01110"

sprintf("%.6f",100.0111)

[1] "100.011100"



awk


awk '{$3=sprintf("%02d", $3);


find . -print | awk -F\. '{print $2}' | grep k | awk -F\/ '{print $3"-"$2}' | awk '{split($0,a,"-"); print "cp \.\/"a[4]"\/"a[1]"-"a[2]"-"a[3]".jpg  ~\/tmpimage\/kaede\/jjgirls.com\/"a[1]a[2]a[4]"-"a[3]".jpg"}' > kaedecp.txt


split($0,a,"-")で行全体を-で区切って分割する。分割した各要素はa[i]でアクセスできる。

2020年9月1日火曜日

行列の計算 matrix()

 

> 1:15
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

> matrix(1:15,ncol=3)
     [,1] [,2] [,3]
[1,]    1    6   11
[2,]    2    7   12
[3,]    3    8   13
[4,]    4    9   14
[5,]    5   10   15

> diff(matrix(1:15,ncol=3))
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    1    1    1
[3,]    1    1    1
[4,]    1    1    1


> c(1:7,NA,9:15)
 [1]  1  2  3  4  5  6  7 NA  9 10 11 12 13 14 15
> w <- matrix(c(1:7,NA,9:15), ncol=5)
> w
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    4    7   10   13
[2,]    2    5   NA   11   14
[3,]    3    6    9   12   15

> v <- as.vector(w)
> v[is.na(v)]
[1] NA
> v[is.na(v)] <- 8
> matrix(v,ncol=dim(w)[2])
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    4    7   10   13
[2,]    2    5    8   11   14
[3,]    3    6    9   12   15
> w <- matrix(v,ncol=dim(w)[2])
> w
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    4    7   10   13
[2,]    2    5    8   11   14
[3,]    3    6    9   12   15

2020年4月29日水曜日

SIR model for R



## Load deSolve package
library(deSolve)

## Create an SIR function
sir <- function(time, state, parameters) {

  with(as.list(c(state, parameters)), {

    dS <- -beta * S * I
    dI <-  beta * S * I - gamma * I
    dR <-                 gamma * I

    return(list(c(dS, dI, dR)))
  })
}

### Set parameters
## Proportion in each compartment: Susceptible 0.999999, Infected 0.000001, Recovered 0
init       <- c(S = 1-1e-6, I = 1e-6, R = 0.0)
## beta: infection parameter; gamma: recovery parameter
parameters <- c(beta = 2.5, gamma = 0.14286)
## Time frame
times      <- seq(0, 70, by = 1)

## Solve using ode (General Solver for Ordinary Differential Equations)
out <- ode(y = init, times = times, func = sir, parms = parameters)
## change to data frame
out <- as.data.frame(out)
## Delete time variable
out$time <- NULL
## Show data

## Plot
matplot(x = times, y = out, type = "l",
        xlab = "Time", ylab = "Susceptible and Recovered", main = "SIR Model",
        lwd = 1, lty = 1, bty = "l", col = 2:4)

## Add legend
legend(40, 0.7, c("Susceptible", "Infected", "Recovered"), pch = 1, col = 2:4, bty = "n")

2019年11月14日木曜日

Download composite leading indicator full data and load them into the R environment CLI

# go to OECD, and download 'Full Indicator data(.csv)".
# change the filename to CLI3.csv upon download.
# go to the directory where the downloaded csv exists.
# run the script in git repo. for example as the below.

$~/R/R2/index/cli_download.sed

# there will be

$ls -l *.csv
-rw-r--r--@ 1 honomoto  staff  1283086 11 14 09:50 CLI3.csv
-rw-r--r--  1 honomoto  staff     7105 11 14 09:50 chn.csv
-rw-r--r--  1 honomoto  staff    12848 11 14 09:50 ea19.csv
-rw-r--r--  1 honomoto  staff    13923 11 14 09:50 oecd.csv
-rw-r--r--  1 honomoto  staff    15465 11 14 09:50 usa.csv

# run cli_download.


cli_xts <- merge(as.xts(read.zoo(read.csv("~/Downloads/oecd.csv"))),
as.xts(read.zoo(read.csv("~/Downloads/usa.csv"))),
as.xts(read.zoo(read.csv("~/Downloads/chn.csv"))),
as.xts(read.zoo(read.csv("~/Downloads/ea19.csv"))),
suffixes = c("oecd","usa","china","ea19"))

# draw graphs

par(mfrow=c(4,1))
plot(diff(cli_xts$oecd)["2011::"],type='p',pwd=1,pch='+')
plot(diff(cli_xts$usa)["2011::"],type='p',pwd=1,pch='+')
plot(diff(cli_xts$ea19)["2011::"],type='p',pwd=1,pch='+')
plot(diff(cli_xts$china)["2011::"],type='p',pwd=1,pch='+')

par(mfrow=c(1,1))

2019年11月13日水曜日

Download Composite Leading Indicator from OECD site. Early Bird Version


# go to OECD site and download CSV from Export menu.
# assume download file name is  "MEI_CLI.csv".
# pick up amplitute justfied OECD total data from CSV

$sed -n '/OECD\ -\ Total/p' MEI_CLI_13112019035737453.csv |grep LOLITOAA | grep Ampli | awk -F, 'BEGIN{ORS = ""}{print $(NF-2)","}'

the improved version is below to integrate all query string into one criteria.

sed -n '/LOLITOAA.*Ampli.*OECD\ -\ Total/p' MEI_CLI.csv | awk -F, 'BEGIN{ORS = "";print "c("}{print $(NF-2)","}END{print $(NF-2)")\n"}'


OR do as below.



awk -F, 'BEGIN{ORS = "";print "w <- c("}/LOLITOAA.*Ampli.*OECD\ -\ Total/{print $(NF-2)","}END{print $(NF-2)")\n length(w)\n"}' MEI_CLI.csv 

output sample is like below. now output is in the from of R statement. Construct vector in c() and substitute to w().


100.7565,100.7652,100.7551,100.7274,100.6736,100.5974,100.512,100.4191,100.3163,100.2006,100.0703,99.93036,99.79047,99.6553,99.53469,99.43623,99.36298,99.30513,99.25108,99.20237,99.15969,99.12648,99.10692

w <- c(100.7565,100.7652,100.7551,100.7274,100.6736,100.5974,100.512,100.4191,100.3163,100.2006,100.0703,99.93036,99.79047,99.6553,99.53469,99.43623,99.36298,99.30513,99.25108,99.20237,99.15969,99.12648,99.10692)

length(w)
[1] 23

# The last month will be found in this way.

sed -n '/LOLITOAA.*Ampli.*OECD\ -\ Total/p' MEI_CLI.csv  | awk -F, 'END{print $7}'
"2019-09"

# as the last entry is "2019-09-01" do as below

w <- as.xts(w,last(seq(as.Date("2010-01-01"),as.Date("2019-09-01"),by='months'),length(w))

2019年10月30日水曜日

calculate S&P 500 n month return.

w <- c()
for(i in seq(1,13,1)){
 m <- matrix(as.vector(to.monthly(SP5)[,4])[i:(i+818)],nrow=13,ncol=63)
  w <- append(
    w,m[13,] / m[1,]

  )
}


gap <- 61 # when month gap is 61.
a<-Sys.time()
w <- c()
j <- (floor(length(index(to.monthly(SP5)))/gap)-1)*gap-1
for(i in seq(1,gap,1)){
#  m <- matrix(as.vector(to.monthly(SP5)[,4])[i:(i+j)],nrow=gap,ncol=(floor(length(index(to.monthly(SP5)))/gap)-1))
m <- matrix(as.vector(to.monthly(SP5)[,4])[i:(i+j)],nrow=gap,ncol=((j+1)/gap))
  w <- append(
    w,m[gap,] / m[1,]
  )
}

s <- as.vector(matrix(t(matrix(w,ncol=gap)),ncol=1))

length(s[s<1])
mean(s[s<1])
mean(s[s>1])
length(s[s>1])
b<-Sys.time()
b -a

# テスト用サンプル gap = 61のとき有効。
# > s[1:60]
#  [1] 2.148387 2.134727 2.115674 2.113586 2.018637 2.319389 2.439462 2.344191 2.245244 2.167947
# [11] 2.332650 2.226138 2.023084 2.079817 2.256983 2.156933 2.100372 2.240935 2.204911 2.040807
# [21] 1.949699 1.986922 1.970280 1.963399 1.852527 1.859845 1.810012 1.961407 1.987846 1.897837
# [31] 1.886220 1.806632 1.728606 1.674551 1.625877 1.505081 1.580743 1.576834 1.664689 1.764419
# [41] 1.796658 1.874068 1.906667 2.047599 2.143897 2.091687 2.119548 2.225312 2.126150 2.118929
# [51] 2.057906 2.037863 2.010277 2.001712 1.959521 1.997989 1.760446 1.815657 1.702103 1.664536
# > as.vector(to.monthly(SP5)[,4])[61:120]/as.vector(to.monthly(SP5)[,4])[1:60]
#  [1] 2.148387 2.134727 2.115674 2.113586 2.018637 2.319389 2.439462 2.344191 2.245244 2.167947
# [11] 2.332650 2.226138 2.023084 2.079817 2.256983 2.156933 2.100372 2.240935 2.204911 2.040807
# [21] 1.949699 1.986922 1.970280 1.963399 1.852527 1.859845 1.810012 1.961407 1.987846 1.897837
# [31] 1.886220 1.806632 1.728606 1.674551 1.625877 1.505081 1.580743 1.576834 1.664689 1.764419
# [41] 1.796658 1.874068 1.906667 2.047599 2.143897 2.091687 2.119548 2.225312 2.126150 2.118929
# [51] 2.057906 2.037863 2.010277 2.001712 1.959521 1.997989 1.760446 1.815657 1.702103 1.664536

# > s[101:160]
#  [1] 1.605806 1.533378 1.464929 1.518325 1.432281 1.441847 1.395389 1.358812 1.389360 1.404079
# [11] 1.424603 1.379753 1.369632 1.397127 1.374649 1.372987 1.479958 1.475313 1.448524 1.415094
# [21] 1.574537 1.557912 1.556921 1.638955 1.583736 1.477864 1.535759 1.530372 1.680867 1.731036
# [31] 1.649442 1.590604 1.503399 1.437894 1.371503 1.394273 1.294020 1.310953 1.252247 1.132658
# [41] 1.147310 1.168755 1.128015 1.122711 1.258135 1.240423 1.296909 1.440987 1.493879 1.655525
# [51] 1.627168 1.583897 1.718678 1.650743 1.509798 1.528843 1.393354 1.389952 1.354965 1.396275
# > as.vector(to.monthly(SP5)[,4])[161:220]/as.vector(to.monthly(SP5)[,4])[101:160]
#  [1] 1.605806 1.533378 1.464929 1.518325 1.432281 1.441847 1.395389 1.358812 1.389360 1.404079
# [11] 1.424603 1.379753 1.369632 1.397127 1.374649 1.372987 1.479958 1.475313 1.448524 1.415094
# [21] 1.574537 1.557912 1.556921 1.638955 1.583736 1.477864 1.535759 1.530372 1.680867 1.731036
# [31] 1.649442 1.590604 1.503399 1.437894 1.371503 1.394273 1.294020 1.310953 1.252247 1.132658
# [41] 1.147310 1.168755 1.128015 1.122711 1.258135 1.240423 1.296909 1.440987 1.493879 1.655525
# [51] 1.627168 1.583897 1.718678 1.650743 1.509798 1.528843 1.393354 1.389952 1.354965 1.396275

when gap = 61,

> length(s[s<1])
[1] 137
> mean(s[s<1])
[1] 0.8869793
> mean(s[s>1])
[1] 1.629065
> length(s[s>1])
[1] 595

when gap = 14,

> length(s[s<1])
[1] 216
> mean(s[s<1])
[1] 0.8863247
> mean(s[s>1])
[1] 1.160806
> length(s[s>1])
[1] 602
> b<-Sys.time()

2019年2月17日日曜日

day to day comparison 2018 vs. 2019 intersect merge



 use intersect() to calculate AND between index(bp.day["2019"]) and 2019 calendar.

> intersect(index(bp.day["2019"]),seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days'))
 [1] 17899 17900 17901 17902 17903 17904 17905 17906 17907 17908 17909 17910 17911 17912 17913 17914 17915 17916 17917 17918
<skip>
[41] 17939 17940 17941 17942 17943 17944 17945
> length(seq(as.Date("1970-01-01"),as.Date("2019-01-01"),by='days'))
[1] 17898
> length(seq(as.Date("1970-01-01"),as.Date("1970-01-01"),by='days'))
[1] 1

resutls are given as the number of the days since "1970-01-01". therefore sbstract 17896 and 17896-365 will calculate the case for 2019 and 2018 respectively. intersecting between 2019 and 2018 will give back the date which exit both 2018 and 2019 in the daily sequential data.

>intersect(intersect(index(bp.day["2019"]),seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days'))-17896,intersect(index(bp.day["2018"]),seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days'))-(17896-365))
[1]  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
[39] 41 43 44 45 46 47

below will creates the day-to-day comparison graph between 2018 and 2019.


bp.day <- apply.daily(bp.bangkok,mean)
d <- intersect(intersect(index(bp.day["2019"]),seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days'))-17896,intersect(index(bp.day["2018"]),seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days'))-(17896-365))

bp.day[seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days')[d]]
bp.day[seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]

merge(bp.day[seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days')[d]],as.vector(bp.day[,1][seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]),as.vector(bp.day[,2][seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]),suffixes = c("","h18","h18"))


plot(merge(bp.day[seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days')[d]],as.vector(bp.day[,1][seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]),as.vector(bp.day[,2][seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]),suffixes = c("","h18","h18")))





2019年2月16日土曜日

eps update at 2019/2/16


before

2018-10-01 139.50000
2019-01-01 141.16000
2019-04-01 144.81000
2019-07-01 148.56000
2019-10-01 153.33000

after

> eps_year_xts["2019-10-01"] <- 154.37
> eps_year_xts["2019-07-01"] <- 148.75
> eps_year_xts["2019-04-01"] <- 144.92
> eps_year_xts["2019-01-01"] <- 140.74
> eps_year_xts["2018-10-01"] <- 138.87

2018-10-01 138.87000
2019-01-01 140.74000
2019-04-01 144.92000
2019-07-01 148.75000
2019-10-01 154.37000

Upgrading R from 340 to 352


necessary to install packages below manually.

install.packages("xts")
install.packages("quantmod")
install.packages("vars")
install.packages("mondate")
install.packages("RMySQL")
install.packages("forecast")
install.packages("beepr")

2019年2月6日水曜日

index mondate,merge



>  index(bp.day["2019"])
 [1] "2019-01-03 UTC" "2019-01-04 UTC" "2019-01-05 UTC" "2019-01-06 UTC" "2019-01-07 UTC" "2019-01-08 UTC"
 [7] "2019-01-09 UTC" "2019-01-10 UTC" "2019-01-11 UTC" "2019-01-12 UTC" "2019-01-13 UTC" "2019-01-14 UTC"
<skip>
[31] "2019-02-02 UTC" "2019-02-03 UTC" "2019-02-04 UTC" "2019-02-05 UTC"

>  mondate(index(bp.day["2019"]))-12
mondate: timeunits="months"
 [1] 2018-01-03 2018-01-04 2018-01-05 2018-01-06 2018-01-07 2018-01-08 2018-01-09 2018-01-10 2018-01-11 2018-01-12
<skip>
[31] 2018-02-02 2018-02-03 2018-02-04 2018-02-05

> as.character(mondate(index(bp.day["2019"]))-12)
 [1] "2018-01-03" "2018-01-04" "2018-01-05" "2018-01-06" "2018-01-07" "2018-01-08" "2018-01-09" "2018-01-10"
<skip>
[25] "2018-01-27" "2018-01-28" "2018-01-29" "2018-01-30" "2018-01-31" "2018-02-01" "2018-02-02" "2018-02-03"
[33] "2018-02-04" "2018-02-05"

>  bp.day[as.character(mondate(index(bp.day["2019"]))-12)]
               high       low
2018-01-03 147.0000 100.00000
2018-01-04 150.0000 104.00000
2018-01-05 131.0000  87.50000
2018-01-06 143.0000 108.00000
<skip>
2018-02-03 148.0000  91.00000
2018-02-04 124.3333  77.00000

> bp.day["2019"]
            high  low
2019-01-03 130.0 78.0
2019-01-04 136.0 80.0
2019-01-05 129.0 83.0
2019-01-06 127.0 77.5
<skip>
2019-02-03 117.5 75.5
2019-02-04 119.0 76.5
2019-02-05 116.5 71.0

merge them and draw graph.
the code below doesn't work when the data exits on the particular date which exits in 2019 but not in 2018.

> bp.day["2019-02-11"]
           high  low
2019-02-11  126 77.5
> bp.day["2018-02-11"]
     high low

updated code is available at 2019-02-17. please refer the new code.

bp.day <- apply.daily(bp.bangkok,mean)
merge(bp.day["2019"], as.vector(bp.day[as.character(mondate(index(bp.day["2019"]))-12)][,1]), as.vector(bp.day[as.character(mondate(index(bp.day["2019"]))-12)][,2]),suffixes=c("","h2018","l2018"))

2019年2月5日火曜日

merge, suffixes, index and plot



THIS IS OBSOLETE. PLEASE REFER TO 2019-02-17

  1. This is the sample for when the number of entries is 34.
  2. Please see "suffixes" parameter. the number of parameters should be equal to the number of xts objects to merged. Not the number of columns.

>bp.day <- apply.daily(bp.bangkok,mean)
> length(bp.day["2019"][,1])
[1] 34

bp.day <- apply.daily(bp.bangkok,mean)

len <- length(bp.day["2019"][,1])
plot(merge(bp.day["2019"],as.xts(as.vector(bp.day[,1]["2018"][1:len]),index(bp.day["2019"])),as.xts(as.vector(bp.day[,2]["2018"][1:len]),index(bp.day["2019"])),suffixes = c("","h2018","l2018")))










2019年1月28日月曜日

read file and normalize data to adjust TZ. for 2019

THIS ENTRY SUPERCEDES 2018-10-06.

# read data as csv format and convert to xts to plot
#
Sys.setenv(TZ=Sys.timezone())
# 
# Update below to check file.exists.
#
if(file.exists("~/~/Downloads/bp2018.csv")){
  bp2018 <- read.csv("~/Downloads/bp2018.csv")
  system("rm \"$HOME/Downloads/bp2018.csv\"")
}else{
  print("!!!FILE DOESN'T EXIST!!!!!")
}
#
# Update ends.
#
# bp2018.xts <- xts(bp2018[,c(-1,-2,-6)],as.POSIXct(paste(bp2018$Date,bp2018$Time,sep=" "),tz=Sys.timezone()),tz=Sys.timezone())
bp2018.xts <- xts(bp2018[,c(3,4,5)],as.POSIXct(paste(bp2018$Date,bp2018$Time,sep=" "),tz=Sys.timezone()),tz=Sys.timezone())
bp2019 <- read.csv("~/Downloads/bp - シート1.csv")
system("rm \"$HOME/Downloads/bp - シート1.csv\"")
bp2019.xts <- xts(bp2019[,c(3,4,5)],as.POSIXct(paste(bp2019$Date,bp2019$Time,sep=" "),tz=Sys.timezone()),tz=Sys.timezone())
bp.xts <- append(bp2018.xts,bp2019.xts)
# weekly average
apply.weekly(bp.xts[bp.xts$High > 95],mean)
#
#
# prepare data according to system timezone. "Asia/Tokyo" in most cases.
#
bp.day <- merge(as.xts(as.vector(bp.xts[,1]),as.Date(index(bp.xts),tz=tzone(bp.xts))),as.vector(bp.xts[,2]))
colnames(bp.day)[1] <- "high"
colnames(bp.day)[2] <- "low"
#
# prepare timezone 2 hours behind "Asia/Tokyo".
#
bp.bangkok <- merge(as.xts(as.vector(bp.xts[,1]),as.Date(index(bp.xts),tz="Asia/Bangkok")),as.vector(bp.xts[,2]))
colnames(bp.bangkok)[1] <- "high"
colnames(bp.bangkok)[2] <- "low"
apply.weekly(bp.bangkok,mean)


# read data as csv format and convert to xts to plot
#
Sys.setenv(TZ=Sys.timezone())
bp2018 <- read.csv("~/Downloads/bp2018.csv")
system("rm \"$HOME/Downloads/bp2018.csv\"")
# bp2018.xts <- xts(bp2018[,c(-1,-2,-6)],as.POSIXct(paste(bp2018$Date,bp2018$Time,sep=" "),tz=Sys.timezone()),tz=Sys.timezone())
bp2018.xts <- as.xts(bp2018[,c(3,4,5)],as.POSIXct(paste(bp2018$Date,bp2018$Time,sep=" "),tz=Sys.timezone()),tz=Sys.timezone())
bp2019 <- read.csv("~/Downloads/bp - シート1.csv")
system("rm \"$HOME/Downloads/bp - シート1.csv\"")
bp2019.xts <- xts(bp2019[,c(3,4,5)],as.POSIXct(paste(bp2019$Date,bp2019$Time,sep=" "),tz=Sys.timezone()),tz=Sys.timezone())
bp.xts <- append(bp2018.xts,bp2019.xts)
# weekly average
apply.weekly(bp.xts[bp.xts$High > 95],mean)
#
#
# prepare data according to system timezone. "Asia/Tokyo" in most cases.
#
bp.day <- merge(as.xts(as.vector(bp.xts[,1]),as.Date(index(bp.xts),tz=tzone(bp.xts))),as.vector(bp.xts[,2]))
colnames(bp.day)[1] <- "high"
colnames(bp.day)[2] <- "low"
#
# prepare timezone 2 hours behind "Asia/Tokyo".
#
bp.bangkok <- merge(as.xts(as.vector(bp.xts[,1]),as.Date(index(bp.xts),tz="Asia/Bangkok")),as.vector(bp.xts[,2]))
colnames(bp.bangkok)[1] <- "high"
colnames(bp.bangkok)[2] <- "low"
apply.weekly(bp.bangkok,mean)

2018年11月19日月曜日

CLI - Composite Leading Indicator - OECD


REFER THIS ENTRY , INSTEAD.

# download csv from oecd(https://data.oecd.org/leadind/composite-leading-indicator-cli.htm) to "~/Downloads"
# use file name "CLI3.csv".
# this file contains multiple regions data. you have to specify its name.
# extract USA only entries
# execute commands below at "~/Download".
#
sed -n '/USA/p' CLI3.csv |awk -F, '{print $6"-01,"$7}'  |sed 's/\"//g' |awk 'BEGIN{print "DATE,DATA"}{print $0}' > usa.csv
# extract OECD entries and exclude OECDE
sed -n '/OECD[^E]/p' CLI3.csv |awk -F, '{print $6"-01,"$7}'  |sed 's/\"//g' |awk 'BEGIN{print "DATE,DATA"}{print $0}' > oecd.csv
#
#sample #1 for usa
#
cli_xts <- merge(as.xts(read.zoo(read.csv("~/Downloads/oecd.csv"))),as.xts(read.zoo(read.csv("~/Downloads/usa.csv"))),suffixes = c("oecd","usa"))

plot.default((cli_xts$usa["2014-07::2018-09"]   / as.vector(cli_xts$usa["2014-01::2018-03"])-1)*100,cli_xts$usa["2014-07::2018-09"])
tmp <- par('usr')
# plot.default((cli_xts$usa["2012-07::2018-09"] / as.vector(cli_xts$usa["2012-01::2018-02"])-1)*100,cli_xts$usa["2012-07::2018-09"] ,xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]))
plot.default((cli_xts$usa["2014-07::2018-09"] / as.vector(cli_xts$usa["2014-01::2018-03"])-1)*100,cli_xts$usa["2014-07::2018-09"] ,xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]))
par(new=T)
plot.default((cli_xts$usa["2018-01::2018-09"] / as.vector(cli_xts$usa["2017-07::2018-03"])-1)*100,cli_xts$usa["2018-01::2018-09"] ,xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=2,lwd=2)
par(new=T)
plot.default((cli_xts$usa["2017-01::2017-12"] / as.vector(cli_xts$usa["2016-07::2017-06"])-1)*100,cli_xts$usa["2017-01::2017-12"], xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=3)
par(new=T)
plot.default((cli_xts$usa["2016-01::2016-12"] / as.vector(cli_xts$usa["2015-07::2016-06"])-1)*100,cli_xts$usa["2016-01::2016-12"], xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=4)

#sample #2 for oecd all
plot.default((cli_xts$oecd["2014-07::2018-09"]   / as.vector(cli_xts$oecd["2014-01::2018-03"])-1)*100,cli_xts$oecd["2014-07::2018-09"])
tmp <- par('usr')
# plot.default((cli_xts$oecd["2012-07::2018-09"] / as.vector(cli_xts$oecd["2012-01::2018-02"])-1)*100,cli_xts$oecd["2012-07::2018-09"] ,xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]))
plot.default((cli_xts$oecd["2014-07::2018-09"] / as.vector(cli_xts$oecd["2014-01::2018-03"])-1)*100,cli_xts$oecd["2014-07::2018-09"] ,xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]))
par(new=T)
plot.default((cli_xts$oecd["2018-01::2018-09"] / as.vector(cli_xts$oecd["2017-07::2018-03"])-1)*100,cli_xts$oecd["2018-01::2018-09"] ,xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=2,lwd=2)
par(new=T)
plot.default((cli_xts$oecd["2017-01::2017-12"] / as.vector(cli_xts$oecd["2016-07::2017-06"])-1)*100,cli_xts$oecd["2017-01::2017-12"], xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=3)
par(new=T)
plot.default((cli_xts$oecd["2016-01::2016-12"] / as.vector(cli_xts$oecd["2015-07::2016-06"])-1)*100,cli_xts$oecd["2016-01::2016-12"], xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=4)
#
# sample #3 for oecd with the extended period.
#
#
#
plot.default((cli_xts$oecd["2014-07::2018-09"]   / as.vector(cli_xts$oecd["2014-01::2018-03"])-1)*100,cli_xts$oecd["2014-07::2018-09"])
tmp <- par('usr')
# plot.default((cli_xts$oecd["2012-07::2018-09"] / as.vector(cli_xts$oecd["2012-01::2018-02"])-1)*100,cli_xts$oecd["2012-07::2018-09"] ,xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]))
plot.default((cli_xts$oecd["2014-07::2018-09"] / as.vector(cli_xts$oecd["2014-01::2018-03"])-1)*100,cli_xts$oecd["2014-07::2018-09"] ,xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),type='b')
par(new=T)
plot.default((cli_xts$oecd["2018-01::2018-09"] / as.vector(cli_xts$oecd["2017-07::2018-03"])-1)*100,cli_xts$oecd["2018-01::2018-09"] ,xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=2,lwd=2)
par(new=T)
plot.default((cli_xts$oecd["2017-01::2017-12"] / as.vector(cli_xts$oecd["2016-07::2017-06"])-1)*100,cli_xts$oecd["2017-01::2017-12"], xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=3)
par(new=T)
plot.default((cli_xts$oecd["2016-01::2016-12"] / as.vector(cli_xts$oecd["2015-07::2016-06"])-1)*100,cli_xts$oecd["2016-01::2016-12"], xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=4)
par(new=T)
plot.default((cli_xts$oecd["2015-01::2015-12"] / as.vector(cli_xts$oecd["2014-07::2015-06"])-1)*100,cli_xts$oecd["2015-01::2015-12"], xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=
5)
abli
ne(h=100)

abline(v=0)

The below is OECD.




2018年10月27日土曜日

merge all data eps spline gpuc


merge all data

result.gpuc <- 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])
result.eps <- lm(apply.quarterly(SP5[,4][k2k],mean) ~ eps_year_xts[k2k]+apply.quarterly(PA[k2k],mean)+apply.quarterly(CS[k2k],mean)+apply.quarterly(UC[k2k],mean))
SP5.result <- merge(residuals(result.gpuc),predict(result.gpuc),residuals(result.eps),predict(result.eps))

GSPC.predict <- merge(to.monthly(GSPC)[substr(k2k,11,23)],last(spline(seq(1,length(SP5.result[,1]),1),as.vector(SP5.result[,2]),n=length(SP5.result[,1])*3+1)$y,n=length(to.monthly(GSPC)[,1][substr(k2k,11,23)])),last(spline(seq(1,length(SP5.result[,1]),1),as.vector(SP5.result[,4]),n=length(SP5.result[,1])*3+1)$y,n=length(to.monthly(GSPC)[,1][substr(k2k,11,23)])),suffixes=c('','spline','eps'))


plot(merge(GSPC.predict[,4],GSPC.predict[,7],GSPC.predict[,8],GSPC.predict[,4]-GSPC.predict[,7],GSPC.predict[,4]-GSPC.predict[,8]),main="GSPC.predict[,4] vs. GSPC.predict[,7]",grid.ticks.on='months')
tmp.legend <- "Black: actual \nRed: spline\nGreen: eps"
addLegend(legend.loc = "topleft", legend.names = tmp.legend,col=3)
tmp.addTA <- as.xts(rep(2800,length(index(GSPC.predict))),index(GSPC.predict))

addSeries(tmp.addTA,on=1,col=6,lwd=1)

2018年10月22日月曜日

SP5,eps,PAYEMS,UC,CS


> summary(lm(apply.quarterly(SP5[,4][k2k],mean) ~ eps_year_xts[k2k]+apply.quarterly(PA[k2k],mean)+apply.quarterly(CS[k2k],mean)+apply.quarterly(UC[k2k],mean)))

Call:
lm(formula = apply.quarterly(SP5[, 4][k2k], mean) ~ eps_year_xts[k2k] +
    apply.quarterly(PA[k2k], mean) + apply.quarterly(CS[k2k],
    mean) + apply.quarterly(UC[k2k], mean))

Residuals:
    Min      1Q  Median      3Q     Max
-178.63  -65.62   14.98   57.55  320.86

Coefficients:
                                 Estimate Std. Error t value Pr(>|t|) 
(Intercept)                    -8.791e+03  4.347e+02 -20.222  < 2e-16 ***
eps_year_xts[k2k]               7.243e+00  6.983e-01  10.373 1.01e-15 ***
apply.quarterly(PA[k2k], mean)  7.740e-02  3.649e-03  21.210  < 2e-16 ***
apply.quarterly(CS[k2k], mean) -4.994e+00  5.692e-01  -8.774 7.71e-13 ***
apply.quarterly(UC[k2k], mean)  1.304e-01  5.309e-02   2.456   0.0166 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 97.83 on 69 degrees of freedom
Multiple R-squared:   0.96, Adjusted R-squared:  0.9577
F-statistic: 413.8 on 4 and 69 DF,  p-value: < 2.2e-16

tmp <- predict(lm(apply.quarterly(SP5[,4][k2k],mean) ~ eps_year_xts[k2k]+apply.quarterly(PA[k2k],mean)+apply.quarterly(CS[k2k],mean)+apply.quarterly(UC[k2k],mean)))
GSPC.predict <- merge(GSPC.predict[,-8],last(spline(seq(1,74,1),tmp,n=220)$y,n=138),suffixes = c("","eps"))

2018年10月6日土曜日

read file and normalize data to adjust TZ.


THIS ENTRY IS OBSOLETE. PLEASE GO TO THIS PAGE

# read data as csv format and convert to xts to plot
#
Sys.setenv(TZ=Sys.timezone())
bp <- read.csv("~/Downloads/bp - シート1.csv")
system("rm \"$HOME/Downloads/bp - シート1.csv\"")
bp.xts <- xts(bp[,c(-1,-2,-6)],as.POSIXct(paste(bp$Date,bp$Time,sep=" "),tz=Sys.timezone()),tz=Sys.timezone())
# weekly average
apply.weekly(bp.xts[bp.xts$High > 95],mean)
#
#
# prepare data according to system timezone. "Asia/Tokyo" in most cases.
#
bp.day <- merge(as.xts(as.vector(bp.xts[,1]),as.Date(index(bp.xts),tz=tzone(bp.xts))),as.vector(bp.xts[,2]))
colnames(bp.day)[1] <- "high"
colnames(bp.day)[2] <- "low"
#
# prepare timezone 2 hours behind "Asia/Tokyo".
#
bp.bangkok <- merge(as.xts(as.vector(bp.xts[,1]),as.Date(index(bp.xts),tz="Asia/Bangkok")),as.vector(bp.xts[,2]))
colnames(bp.bangkok)[1] <- "high"
colnames(bp.bangkok)[2] <- "low"
apply.weekly(bp.bangkok,mean)

2018年9月20日木曜日

addTA,approx


Draw the line between given 2 positions on the candle chart.

my_draw_line_on_candle <- function(par_xts,start_val,start_date,end_val,end_date){
  len <- length(seq(as.Date(start_date),as.Date(end_date),by='weeks'))
  plot_data <- approx(seq(1,2,1),c(start_val,end_val),n=len,method='linear')$y
  tmp_xts <- as.xts(plot_data,seq(as.Date(start_date),as.Date(end_date),by='weeks'))
  addTA(tmp_xts,on=1,legend="slope")
}


> my_draw_line_on_candle(weekly_pf,2273486,"2018-01-26",2189051,"2018-09-19")
> my_draw_line_on_candle(weekly_pf,as.vector(first(weekly_pf)[,1]),index(first(weekly_pf)),2188051,"2018-09-19")
> my_draw_line_on_candle(weekly_pf,1160008,"2017-01-06",2163476,"2018-09-18")



> last(weekly_pf)[,4]/as.vector(first(weekly_pf)[,1])
              close
2018-09-19 3.450367
> length(index(weekly_pf))
[1] 247
> 3.450367**(1/247)
[1] 1.005027
> seq(1,247,1)
  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27
<skip>
[244] 244 245 246 247
> 1.005027**seq(1,247,1)
  [1] 1.005027 1.010079 1.015157 1.020260 1.025389 1.030544 1.035724 1.040931 1.046163 1.051423 1.056708 1.062020
 <skip>
[241] 3.348365 3.365197 3.382114 3.399116 3.416203 3.433376 3.450636
> tmp <- as.xts(634440.2*1.005027**seq(1,247,1),index(weekly_pf))
> addTA(tmp,on=1,legend="powered")

my_draw_line_on_candle(weekly_pf,as.vector(first(weekly_pf)[,1]),index(first(weekly_pf)),last(weekly_pf)[,4],index(last(weekly_pf)[,4]))
tmp <- as.xts((as.vector((last(weekly_pf)[,4]/as.vector(first(weekly_pf)[,1]))**(1/length(index(weekly_pf))))**seq(1,length(index(weekly_pf)),1))*as.vector(first(weekly_pf)[,1]),index(weekly_pf))
addTA(tmp,on=1,legend="powered")
tmp <- as.xts((as.vector((1800000/as.vector(first(weekly_pf)[,1]))**(1/length(index(weekly_pf))))**seq(1,length(index(weekly_pf)),1))*as.vector(first(weekly_pf)[,1]),index(weekly_pf))

addTA(tmp,on=1,legend="")


2018年9月14日金曜日

bp graph with moving average. plot() suffixes,filter()





> length(as.vector(na.omit(filter(apply.daily(bp.bangkok,mean)[,1],rep(1,7))/7)))
[1] 231
> plot(merge(as.xts(as.vector(na.omit(filter(apply.daily(bp.bangkok,mean)[,1],rep(1,7))/7)),last(index(apply.daily(bp.bangkok,mean)[,2]),len)),as.vector(na.omit(filter(apply.daily(bp.bangkok,mean)[,2],rep(1,7))/7)),last(apply.daily(bp.bangkok,mean),n=231)),suffixes=c("mh","ml","high","low"),main="daily w/ 7 day moving average",grid.ticks.on='weeks')

> events
           [,1]       
2018-06-20 "natrix"   
2018-07-14 "weight"   
2018-08-09 "abort natrix"
> addEventLines(events, srt=90, pos=2,col=10)


 plot(merge(as.xts(as.vector(na.omit(filter(apply.daily(bp.bangkok,mean)[,1],rep(1,7))/7)),last(index(apply.daily(bp.bangkok,mean)[,2]),len)),as.vector(na.omit(filter(apply.daily(bp.bangkok,mean)[,2],rep(1,7))/7)),last(apply.daily(bp.bangkok,mean),n=length(as.vector(na.omit(filter(apply.daily(bp.bangkok,mean)[,1],rep(1,7))/7))))),suffixes=c("mh","ml","high","low"),main="daily w/ 7 day moving average",grid.ticks.on='weeks')
addEventLines(events, srt=90, pos=2,col=10)

len <- length(as.vector(na.omit(filter(apply.daily(bp.bangkok,mean)[,1],rep(1,7))/7)))
plot(merge(as.xts(as.vector(na.omit(filter(apply.daily(bp.bangkok,mean)[,1],rep(1,7))/7)),last(index(apply.daily(bp.bangkok,mean)[,2]),len)),
as.vector(na.omit(filter(apply.daily(bp.bangkok,mean)[,2],rep(1,7))/7)),last(apply.daily(bp.bangkok,mean),n=len)),
suffixes=c("mh","ml","high","low"),main="daily w/ 7 day moving average",grid.ticks.on='weeks')
addSeries(as.xts(rep(mean(bp.xts[,1][bp.xts$High > 95]),len),last(index(apply.daily(bp.bangkok,mean)[,2]),len)),on=1,col=6,lwd=1)
addSeries(as.xts(rep(mean(bp.xts[,2][bp.xts$High > 95]),len),last(index(apply.daily(bp.bangkok,mean)[,2]),len)),on=1,col=6,lwd=1)
addSeries(as.xts(rep(last(as.vector(na.omit(filter(apply.daily(bp.bangkok,mean)[,2],rep(1,7))/7))),len),last(index(apply.daily(bp.bangkok,mean)[,2]),len)),on=1,col=6,lwd=1)
addSeries(as.xts(rep(last(as.vector(na.omit(filter(apply.daily(bp.bangkok,mean)[,1],rep(1,7))/7))),len),last(index(apply.daily(bp.bangkok,mean)[,2]),len)),on=1,col=6,lwd=1)
# events <- xts(c("natrix","weight","abort natrix"),as.Date(c("2018-06-20", "2018-07-14","2018-08-09")))
# addEventLines(events, srt=90, pos=2,col=10)
events <- xts(c("natrix","weight","abort natrix","55kg"),as.Date(c("2018-06-20", "2018-07-14","2018-08-09","2018-10-13")))

addEventLines(events, srt=90, pos=2,col=10)




2018年9月13日木曜日

Prepare Data --- getsymbols(), auto.arima(), as.yearmon(), as.yearqtr()




> len_mon <- 48  # # of months to predict
> r <- 1.04 # pesumed GDP growth rate
> i <- seq(2,len_mon/3,1)  # seq of quarters to predict
> d <- as.Date(as.yearqtr(seq(Sys.Date(),as.Date("2100-12-31"),by="quarters")[i])) # pick up the first day of each quarters.
> as.xts(forecast(auto.arima(CS["2012::"]),h=len_mon)$mean[1:len_mon],as.Date(as.yearmon(mondate(index(last(CS)))+seq(1,len_mon,1)),frac=0))[as.Date(as.yearqtr(mondate(index(last(CS)))+seq(3,len_mon,3)),frac=0)]
               [,1]
2018-07-01 225.2251
2018-10-01 227.3579
<skip>
2022-04-01 266.0765
> last(CS)
           SPCS10RSA
2018-06-01  224.8054
> as.xts(forecast(auto.arima(PA),h=len_mon)$mean[1:len_mon],as.Date(as.yearmon(mondate(index(last(PA)))+seq(1,len_mon,1)),frac=0))[as.Date(as.yearqtr(mondate(index(last(PA)))+seq(3,len_mon,3)),frac=0)]
               [,1]
2018-10-01 149684.2
2019-01-01 150292.1
<skip>
2022-07-01 158802.2
>  last(PA)
           PAYEMS
2018-08-01 149279
> last(UC)
           UNDCONTSA
2018-07-01      1122
>
as.xts(forecast(auto.arima(UC),h=len_mon)$mean[1:len_mon],as.Date(as.yearmon(mondate(index(last(UC)))+seq(1,len_mon,1)),frac=0))[as.Date(as.yearqtr(mondate(index(last(UC)))+seq(3,len_mon,3)),frac=0)]
2018-10-01 1130.871
2019-01-01 1139.742
<skip>
2022-07-01 1263.939


len_mon <- 48  # # of months to predict
gdp_g_r <- 1.04 # pesumed GDP growth rate
i <- seq(2,len_mon/3,1)  # seq of quarters to predict
d <- as.Date(as.yearqtr(seq(Sys.Date(),as.Date("2100-12-31"),by="quarters")[i])) # pick up the first day of each quarters.

getSymbols("PAYEMS",src="FRED")
PA <- PAYEMS
m_PA <- as.xts(forecast(auto.arima(PA),h=len_mon)$mean[1:len_mon],as.Date(as.yearmon(mondate(index(last(PA)))+seq(1,len_mon,1)),frac=0))[as.Date(as.yearqtr(mondate(index(last(PA)))+seq(3,len_mon,3)),frac=0)]
PAq <- apply.quarterly(PA[k2k],mean)
length(PAq)

getSymbols("UNDCONTSA",src="FRED")
UC <- UNDCONTSA
m_UC <- as.xts(forecast(auto.arima(UC),h=len_mon)$mean[1:len_mon],as.Date(as.yearmon(mondate(index(last(UC)))+seq(1,len_mon,1)),frac=0))[as.Date(as.yearqtr(mondate(index(last(UC)))+seq(3,len_mon,3)),frac=0)]
UCq <- apply.quarterly(UC[k2k],mean)
length(UCq)

getSymbols('SPCS10RSA',src='FRED')
CS <- SPCS10RSA
m_CS_2012 <- as.xts(forecast(auto.arima(CS["2012::"]),h=len_mon)$mean[1:len_mon],as.Date(as.yearmon(mondate(index(last(CS)))+seq(1,len_mon,1)),frac=0))[as.Date(as.yearqtr(mondate(index(last(CS)))+seq(3,len_mon,3)),frac=0)]
CSq <- apply.quarterly(CS[k2k],mean)
length(CSq)

getSymbols("GDP",src="FRED")
G <- GDP
# m_GDP <- as.xts(as.vector(last(GDP)) * r**(i/4),d)
m_GDP <- as.xts(as.vector(last(G)) * gdp_g_r**(seq(1,len_mon/3,1)/4),as.Date(as.yearqtr(mondate(index(last(G)))+seq(3,len_mon,3)),frac=0))
kikan <- paste("1992-01-01::",as.Date(as.yearmon((mondate(index(last(G)))+2)),frac=1),sep="")
k2k <- paste("2000-01-01::",as.Date(as.yearmon((mondate(index(last(G)))+2)),frac=1),sep="")

SP5 <- as.xts(read.zoo(read.csv("~/SP5.csv")))

length(CSq)
length(UCq)
length(PAq)
summary(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]))
my_sp5cs(k2k,m_GDP[d[1:9]],m_PA[d[1:9]],m_UC[d[1:9]],m_CS_2012[d[1:9]])
result.eps <- lm(apply.quarterly(SP5[,4][k2k],mean) ~ eps_year_xts[k2k]+apply.quarterly(PA[k2k],mean)+apply.quarterly(CS[k2k],mean)+apply.quarterly(UC[k2k],mean))
result.gpuc <- 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])
summary(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.gpuc),predict(result.gpuc),residuals(result.eps),predict(result.eps))

GSPC.predict <- merge(to.monthly(GSPC)[substr(k2k,11,23)],last(spline(seq(1,length(SP5.result[,1]),1),as.vector(SP5.result[,2]),n=length(SP5.result[,1])*3+1)$y,n=length(to.monthly(GSPC)[,1][substr(k2k,11,23)])),last(spline(seq(1,length(SP5.result[,1]),1),as.vector(SP5.result[,4]),n=length(SP5.result[,1])*3+1)$y,n=length(to.monthly(GSPC)[,1][substr(k2k,11,23)])),suffixes=c('','spline','eps'))