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

2019年10月28日月曜日

CLI delta plus period graph.


# draw wp graph starts.


wpx <- wp

wp <- wp[wp[,2] > 10]

par(mfrow=c(2,3))
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),1)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),2)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),3)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),4)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),5)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),6)[1]],main="")




plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),7)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),8)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),9)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),10)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),11)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),12)[1]],main="")




plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),13)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),14)[1]],main="")
# plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),15)[1]],main="")
# plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),16)[1]],main="")
# plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),17)[1]],main="")
# plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),18)[1]],main="")


par(mfrow=c(1,1))
wp <- wpx
remove(wpx)


# draw wp graph ends.






2019年7月5日金曜日

年と月の計算 yearmon mfrow




> wp
           result period_length         rate  open_p close_p
 1 1973 1.3763938            28  0.011474868   84.30  116.03
 5 1976 1.3013769            16  0.016600207   76.98  100.18
10 1978 0.9423369            15 -0.003951666   98.85   93.15
12 1980 1.1883753             6  0.029182211  114.24  135.76
 1 1984 1.3672189            17  0.018569047  119.52  163.41
 2 1985 1.1075926             3  0.034649717  163.58  181.18
 8 1987 1.3967474            13  0.026036742  236.12  329.80
12 1988 1.0727335             9  0.007831630  258.89  277.72
 2 1990 0.9751147             4 -0.006280247  340.36  331.89
 8 1991 1.1498066             7  0.020142134  343.91  395.43
 4 1992 0.9950124             4 -0.001249244  417.03  414.95
 9 1994 1.0727019            22  0.003195123  431.35  462.71
10 1995 1.0674621             4  0.016454915  544.75  581.50
 9 1997 1.4893871            20  0.020117927  636.02  947.28
 3 2000 1.3639946            17  0.018427587 1098.67 1498.58
 4 2002 1.0345650             7  0.004866239 1040.94 1076.92
 3 2004 1.3277960            12  0.023908021  848.18 1126.21
 5 2006 1.0659588            12  0.005337085 1191.50 1270.09
 6 2007 1.1836842            12  0.014151848 1270.06 1503.35
 5 2010 1.4932221            15  0.027089509  729.57 1089.41
 2 2011 1.1983603             7  0.026187620 1107.53 1327.22
 3 2012 1.1295682             4  0.030927485 1246.91 1408.47
 3 2014 1.2994239            18  0.014657552 1440.90 1872.34
12 2014 1.0273593             4  0.006770750 2004.07 2058.90
11 2017 1.2807753            19  0.013109691 2067.17 2647.58
>

のとき、その20番目から25番目までのエントリを対象に一括でグラフを書く。パラメータ中のデータ(月数)をyearmon で正確に計算するために12で割っている。1/12で1ヶ月、7/12で7ヶ月、19/12で1年と7ヶ月。

par(mfrow=c(3,2))
for(i in seq(1,6,1)){plot.default(to.monthly(SP5)[,4][paste(as.Date((yearmon(index(wp))[20:25] -(wp[20:25][,2]/12))[i]),as.Date(index(wp[20:25])[i]),sep="::")]) }


OR

あるいは、mondateを使って月数の計算を行う。またここでは最初の月を計算ミスするバグを修正するため’+1’を追加した。下の図はバグあり期間計算で作成したもので実際より一月早く始まっている。

for(i in seq(1,6,1)){plot.default(to.monthly(SP5)[,4][paste(as.Date(mondate(index(wp))[20:25][i] -    as.vector(wp[,2][20:25])[i]+1),as.Date(index(wp[20:25])[i]),sep="::")]) }




14番目から19番目までのエントリを対象に一括でグラフを書くときは以下の通り。序数を指定する場所が三箇所あることに注意。

for(i in seq(1,6,1)){plot.default(to.monthly(SP5)[,4][paste(as.Date((yearmon(index(wp))[14:19] -(wp[14:19][,2]/12))[i]),as.Date(index(wp[14:19])[i]),sep="::")]) }

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年1月28日月曜日

CLI - composite leading indicator - OECD


THIS ENTRY IS SUPERCEDED BY THIS PAGE.
THIS ENTRY SUPERCEDE THIS

1. preparation

# 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

2.drawing graph

# read data from csv.
#
cli_xts <- merge(as.xts(read.zoo(read.csv("~/Downloads/oecd.csv"))),as.xts(read.zoo(read.csv("~/Downloads/usa.c
sv"))),suffixes = c("oecd","usa"
))
#
#  set start date and end date
#
start_date <- as.Date("2014-07-01")
end_date <- as.Date("2018-11-01")
#
#
cli_xts$oecd[paste(start_date,end_date,sep="::")]
period_base <- paste(start_date,end_date,sep="::")
diff_mon <- 6
period_compare <- paste(as.Date(as.yearmon(mondate(as.Date(start_date))-diff_mon )),as.Date(as.yearmon(mondate(as.Date(end_date))-diff_mon )),sep="::")
paste("2018-01",end_date,sep="::")
paste("2017-07",as.Date(as.yearmon(mondate(as.Date(end_date))-diff_mon )),sep="::")

plot.default((cli_xts$oecd[period_base]   / as.vector(cli_xts$oecd[period_compare])-1)*100,cli_xts$oecd[period_base])
tmp <- par('usr')
plot.default((cli_xts$oecd[period_base] / as.vector(cli_xts$oecd[period_compare])-1)*100,cli_xts$oecd[period_base] ,xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),type='b')
par(new=T)
#
code for CY2019 and after.
#
#
if(as.Date("2018-12-31") < end_date){
  # add line to data beyond "2019-01-01" when time has come.
  plot.default((cli_xts$oecd[paste("2019-01",end_date,sep="::")] / as.vector(cli_xts$oecd[paste("2018-07",as.Date(as.yearmon(mondate(as.Date(end_date))-diff_mon )),sep="::")])-1)*100,cli_xts$oecd[paste("2019-01",end_date,sep="::")] ,xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=2,lwd=2)

  par(new=T)

  plot.default((cli_xts$oecd["2018-01::2018-12"] / as.vector(cli_xts$oecd["2017-07::2018-06"])-1)*100,cli_xts$oecd["2018-01::2018-12"], xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=6)

} else{

  plot.default((cli_xts$oecd[paste("2018-01",end_date,sep="::")] / as.vector(cli_xts$oecd[paste("2017-07",as.Date(as.yearmon(mondate(as.Date(end_date))-diff_mon )),sep="::")])-1)*100,cli_xts$oecd[paste("2018-01",end_date,sep="::")] ,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)
legend("topleft", legend = "Light Blue: 2015\nBlue: 2016\nLime: 2017\nRed: 2018",bty='n')




2018年11月1日木曜日

Date Format Conversion mondate() index() format()





October 2018 was quite wild. Let us see the historical data on the other occasion. There were only 9 cases in 11 years, when the lowest price of the month is more than 11% lower than the open.

> to.monthly(GSPC)[,3]/to.monthly(GSPC)[,1][to.monthly(GSPC)[,3]/to.monthly(GSPC)[,1] < 0.89]
         GSPC.Low
 1 2008 0.8651744
 9 2008 0.8591352
10 2008 0.7213723
11 2008 0.7649871
 5 2010 0.8756500
 8 2011 0.8521960
 8 2015 0.8871556
 1 2016 0.8891620
10 2018 0.8897068

Use "as.mondate" to calculate one month after the examples.

> as.mondate(index(to.monthly(GSPC)[,3]/to.monthly(GSPC)[,1][to.monthly(GSPC)[,3]/to.monthly(GSPC)[,1] < 0.89])[-8])+1
mondate: timeunits="months"
[1] 2008-02-01 2008-10-02 2008-11-01 2008-12-02 2010-06-01 2011-09-01 2015-09-01 2018-11-01

use "format()" to normalize. "%Y-%m" should be the option.

> format(as.mondate(index(to.monthly(GSPC)[,3]/to.monthly(GSPC)[,1][to.monthly(GSPC)[,3]/to.monthly(GSPC)[,1] < 0.89])[-9])+1,"%y-%m")
[1] "08-02" "08-10" "08-11" "08-12" "10-06" "11-09" "15-09" "16-02"


> format(as.mondate(index(to.monthly(GSPC)[,3]/to.monthly(GSPC)[,1][to.monthly(GSPC)[,3]/to.monthly(GSPC)[,1] < 0.89])[-9])+1,"%Y-%m")
[1] "2008-02" "2008-10" "2008-11" "2008-12" "2010-06" "2011-09" "2015-09" "2016-02"

But index() returns yearmon calss and the direct coversion from yearmon to mondate triggers the warning message. If you don't like it, insert "as.Date()" between them.

警告メッセージ:
Attempting to convert class 'yearmon' to 'mondate' via 'as.Date' then 'as.numeric'. Check results!

> format(as.mondate(as.Date(index(to.monthly(GSPC)[,3]/to.monthly(GSPC)[,1][to.monthly(GSPC)[,3]/to.monthly(GSPC)[,1] < 0.89]) ))+1,"%Y-%m")[-9]
[1] "2008-02" "2008-10" "2008-11" "2008-12" "2010-06" "2011-09" "2015-09" "2016-02"

The result was disappointing. Hope, this month is exceptional.

> monthlyReturn(GSPC)[format(as.mondate(index(to.monthly(GSPC)[,3]/to.monthly(GSPC)[,1][to.monthly(GSPC)[,3]/to.monthly(GSPC)[,1] < 0.89])[-9])+1,"%Y-%m")]
           monthly.returns
2008-02-29    -0.034761193
2008-10-31    -0.169424524
2008-11-28    -0.074849043
2008-12-31     0.007821577
2010-06-30    -0.053882442
2011-09-30    -0.071761988
2015-09-30    -0.026442832
2016-02-29    -0.004128360

you can do the similar calculation for monthlyReturn().

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'))


2018年9月10日月曜日

mondate,as.yearmon and the calculation to treat number of months. mondate as.yearmon as.yearqtr as.Date(date,frac=1) 該当月の初めの日付



> index(last(G))
[1] "2018-04-01"
> mondate(index(last(G)))+2
mondate: timeunits="months"
[1] 2018-06-01
> as.yearmon(as.character(mondate(index(last(G)))+2))
[1] " 6 2018"
> as.yearmon((mondate(index(last(G)))+2))
[1] " 6 2018"
> as.yearmon(mondate(index(last(G))+2))
[1] " 4 2018"
> as.yearmon((mondate(index(last(G)))+2))
[1] " 6 2018"
> as.Date(as.yearmon((mondate(index(last(G)))+2)),frac=1)
[1] "2018-06-30"
>
> paste("1992-01-01::",as.Date(as.yearmon((mondate(index(last(G)))+2)),frac=1),sep="")
[1] "1992-01-01::2018-06-30"
> kikan <- paste("1992-01-01::",as.Date(as.yearmon((mondate(index(last(G)))+2)),frac=1),sep="")
# calculate coming quarter beginning dates
> as.Date(as.yearqtr((mondate(index(last(G)))+seq(3,27,3))))
[1] "2018-07-01" "2018-10-01" "2019-01-01" "2019-04-01" "2019-07-01" "2019-10-01" "2020-01-01" "2020-04-01" "2020-07-01"
> as.Date(as.yearmon(mondate(index(last(CS)))+seq(1,48,1)),frac=0)
 [1] "2018-07-01" "2018-08-01" "2018-09-01" "2018-10-01" "2018-11-01" "2018-12-01" "2019-01-01" "2019-02-01" "2019-03-01"
<skip>
[46] "2022-04-01" "2022-05-01" "2022-06-01"
> as.Date(as.yearqtr(mondate(index(last(CS)))+seq(3,48,3)),frac=0)
 [1] "2018-07-01" "2018-10-01" "2019-01-01" "2019-04-01" "2019-07-01" "2019-10-01" "2020-01-01" "2020-04-01" "2020-07-01"
[10] "2020-10-01" "2021-01-01" "2021-04-01" "2021-07-01" "2021-10-01" "2022-01-01" "2022-04-01"


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="")

2018年6月18日月曜日

get UNDCONTSA data from www.census.gov


Download "the total number housing under construction" from here.

  1. choose "New Residential Construction"
  2. "1970 from 2018"
  3. choose "Housing Units under Construction"
  4. "Total Units" and "United States"

Open CSV file and chop unnecessary lines at header and footer. The result must be like below.


Period,Value
Jan-1970,889
Feb-1970,888
Mar-1970,890
<.........>
Mar-2018,1124
Apr-2018,1124


When file name is "~/iCloud/R/UC-1970-2018.csv" and contains data from 1970-04-01 to 2018-04-01.

as.xts(read.csv("~/iCloud/R/UC-1970-2018.csv")[,2],seq(as.Date("1970-01-01"),as.Date("2018-04-01"),by="months"))

The below calculate data length and the date of termination accordingly.

as.xts(read.csv("~/iCloud/R/UC-1970-2018.csv")[,2],as.Date(seq(mondate(as.Date("1970-01-01")),mondate(as.Date("1970-01-01"))-1+length(read.csv("~/iCloud/R/UC-1970-2018.csv")[,2]))))


           [,1]
1970-01-01  889
1970-02-01  888
1970-03-01  890
<snipet>
2018-03-01 1124
2018-04-01 1124

2017年3月24日金曜日

Calculate the first and the last day of any designated month.

Below will pick up the start and the end of next month where PA data ends.


as.Date(as.yearmon(mondate(index(last(PA)))+1))
[1] "2017-03-01"
as.Date(as.yearmon(mondate(index(last(PA)))+2))-1
[1] "2017-03-31"


thus the combination of them will output as below.


as.xts(forecast(auto.arima(PA),h=10)$mean[1:10],as.Date(seq(mondate(index(last(PA)))+1,by=1,length.out=10)))

               [,1]
2017-03-02 145989.9
2017-04-02 146182.0
      <SKIP>
2017-11-02 147361.8

2017-12-02 147513.5


pick up the first day and the last day of the month which is exactly 36 months after any given date.

> as.Date(as.yearmon((mondate(Sys.Date())+36)))
[1] "2020-03-01"


> as.Date(as.yearmon((mondate(Sys.Date())+36)))-1
[1] "2020-02-29"
>

do the same thing by ISOdate, however it's much more redundant.

> as.Date(ISOdate(yea(mondarte(.())SysDate+36),month(mondate(Sys.Date())+36),1))-1
[1] "2020-02-29"