2016年7月19日火曜日

how to calculate moving average

when data is a vector of numeric like below.

> tail(data,n=10)
 [1] 199 158 165 137 170 142 127 174 158 156​


moving average is able to calculate with filter function. This is the case for 7 days simple moving average and omitting N/A results.

> filter(data,rep(1,7))[!is.na(filter(inc_daily,rep(1,7)))]/7


GDP forecast by VAR

Thus we should be able to see updated GDP forecast. Most of the figures are almost not changed at all.

> predict(VAR(v_XCPG_1992_2016),lag=6)$fcst$GDP
          fcst    lower    upper        CI
 [1,] 16633.79 16582.69 16684.89  51.09898
 [2,] 16653.44 16581.13 16725.76  72.31579
 [3,] 16672.98 16584.34 16761.61  88.63489
 [4,] 16692.39 16589.96 16794.82 102.42740
 [5,] 16711.69 16597.08 16826.30 114.61098
 [6,] 16730.88 16605.22 16856.53 125.65568
 [7,] 16749.97 16614.13 16885.81 135.84034
 [8,] 16768.95 16623.61 16914.30 145.34629
 [9,] 16787.85 16633.55 16942.15 154.29927
[10,] 16806.65 16643.86 16969.44 162.79063

2016年7月18日月曜日

revisit correlation between GDP adn S&P500. summary(lm(to.quarterly(SP5["2010-04-01::2016-03-01"])[,4] ~ GDPC96["2010-04-01::2016-01-01"]))



revisit correlation between GDP adn S&P500.

> summary(lm(to.quarterly(SP5["2010-04-01::2016-03-01"])[,4] ~ GDPC96["2010-04-01::2016-01-01"]))

Call:
lm(formula = to.quarterly(SP5["2010-04-01::2016-03-01"])[, 4] ~ 
    GDPC96["2010-04-01::2016-01-01"])

Residuals:
    Min      1Q  Median      3Q     Max 
-194.42  -86.31   13.70   73.06  172.41 

Coefficients:
                                   Estimate Std. Error t value Pr(>|t|)    
(Intercept)                      -7.760e+03  6.187e+02  -12.54 1.69e-11 ***
GDPC96["2010-04-01::2016-01-01"]  6.016e-01  3.967e-02   15.17 3.93e-13 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 106.3 on 22 degrees of freedom
Multiple R-squared:  0.9127, Adjusted R-squared:  0.9087 
F-statistic:   230 on 1 and 22 DF,  p-value: 3.93e-13


2016年7月17日日曜日

Why does the 2016 S&P breakout look so much like 2009?


Need to read later.



http://www.marketwatch.com/story/why-does-the-2016-sp-breakout-look-so-much-like-2009-2016-07-15?link=sfmw_tw

2016年7月16日土曜日

Updated dataframe for VAR analysis



use GDP,RSXFS, UNDCONTSA and PAYEMS.

v_XCPG_1992_2016 <- merge(RSXFS["1992::2016-04-01"],UNDCONTSA["1992::2016-04-01"])
v_XCPG_1992_2016 <- merge(v_XCPG_1992_2016,PAYEMS["1992::2016-04-01"])
v_XCPG_1992_2016 <- merge(v_XCPG_1992_2016,GDP_XTS["1992::2016-04-01"])

change column name.

> tail(v_XCPG_1992_2016)
            RSXFS UNDCONTSA PAYEMS GDP_XTS..1992..2016.04.01..
2015-11-01 395480       964 142875                    16485.24
2015-12-01 396723       976 143146                    16499.92
2016-01-01 394878       976 143314                    16514.59
2016-02-01 395126       987 143547                    16547.33
2016-03-01 393775       991 143733                    16580.67
2016-04-01 399134      1001 143877                    16614.00
> names(v_XCPG_1992_2016)[4]
[1] "GDP_XTS..1992..2016.04.01.."
> names(v_XCPG_1992_2016)[4] <- "GDP"

plot past 60 months and next 10 months.

>plot(c(as.numeric(last(GDP_XTS,n=60)),predict(VAR(v_XCPG_1992_2016),lag=6)$fcst$GDP[,1]),type="l")

2016年7月7日木曜日

how to convert GDP to monthly basis.

GDP data is available only for quarterly basis, while other economical data such as payroll, retail and house constructions are monthly. Use approx to convert quaterly data to monthly. this is quite unorthodox, though. the command below will add 2 iterations  between the quarter and the next one, thus convert to monthly basis. Vector length is automatically calculated and adjusted.

last(GDP_XTS);GDP_XTS <- as.xts(read.zoo(data.frame(date=seq(index(first(GDPC96)),index(last(GDPC96)),by="months"),GDP=approx(GDPC96,n=(length(GDPC96)*3-2))$y)))

start/stop apache automatically on mac os x



Recent Mac OS X is no longer able to start Apache from system configuration GUI and have to use terminals instead. Furthermore, init, inted and other legacy daemons are obsoleted. all those things are switched to launchd. The command below upload apache property file(plist) to launchd. Apache is started at boot or on demand or any other designated method written in the property.

  $ sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

"unload" the property will disable all related set up which are done at "load".

  $ sudo launchctl unload /System/Library/LaunchDaemons/org.apache.httpd.plist

2016年7月6日水曜日

install mediawiki on mac os x



  1. download mediawiki source file tarball from https://www.mediawiki.org/wiki/Download/ja
  2. extend all files into /Library/WebServer/Documents
  3. make symbolic link between the directory created in step 2 to "wiki"(refered in 7.).
  4. check php version(should be equal or more than 5.5.9)
  5. edit conf to enable php on apache
    1. sudo vi /private/etc/apache2/httpd.conf
    2. uncomment the line "LoadModule php5_module libexec/apache2/libphp5.so"
  6. sudo apachectl restart
  7. access  http://localhost/wiki/mw-config/index.php
  8. start installation.

uninstall mysql from mac os x



Commands below will uninstall from your mac os x. please substitute version number and architecture designator accordingly.

  $  sudo rm -rf /usr/local/mysql-5.1.37-osx10.5-x86_64/
  $  sudo rm -rf /Library/StartupItems/MySQLCOM/
  $  sudo rm -rf /Library/PreferencePanes/MySQL*
  $ sudo rm -rf /Library/Receipts/mysql-5.1.37-osx10.5-x86_64.pkg/


2016年6月24日金曜日

Use list as associative array part 2

This one is better than the previous one.

> test2_list <- list(list("Mon","Tue","Wed","Thu","Fri","Sat","Sun"),list(1,2,3,4,5,6,7))
> for(i in test2_list[[2]]){
+   # if(match(weekdays(as.Date(ISOdate(year(Sys.Date()),1,1)),abbreviate = TRUE),test_list[[1]][i],nomatch = FALSE))
+   print(paste(">>",test2_list[[1]][i]))
+ }
[1] ">> Mon"
[1] ">> Tue"
[1] ">> Wed"
[1] ">> Thu"
[1] ">> Fri"
[1] ">> Sat"
[1] ">> Sun"

Use list as associative array.


plase also see part2.

utilize a list as associative array.

1)create a list which each element contains a pair of information.

> test_list <- list(list("Mon",1),list("Tue",2),list("Wed",3),list("Thu",4),list("Fri",5),list("Sat",6),list("Sun",7))

2)when weekday returns day of the first day of the year, numeric seq number which is associated with weekday's return will be printed.

> for(i in 1:length(test_list)){
   if(match(weekdays(as.Date(ISOdate(year(Sys.Date()),1,1)),abbreviate = TRUE),test_list[[i]][1],nomatch = FALSE))
     {print(test_list[[i]][2])}
 }
[[1]]
[1] 5

2016年6月15日水曜日

Calculate day dependent data with less workload - 2 - test 2 strings are equal or not

Use match function to test 2 strings are same or not.

Below is a normal case and easy.

> weekdays(as.Date("2016-01-01"),abbreviate = TRUE)
[1] "Fri"
match(weekdays(as.Date("2016-01-01"),abbreviate = TRUE),"Fri")
[1] 1

In other cases, match return NA not 0 by default and this causes a problem in if function. I see this is very strange, however, it is just one more option to go.

> match(weekdays(as.Date("2016-01-01"),abbreviate = TRUE),"Mon")
[1] NA

The parameter nomatch will give an option to set a value for the case. with "nomatch=0" you can booleanize the return value

> match(weekdays(as.Date("2016-01-01"),abbreviate = TRUE),"Fri", nomatch=0)
[1] 1
> match(weekdays(as.Date("2016-01-01"),abbreviate = TRUE),"Thur", nomatch=0)
[1] 0

Then now it's possible to use if-then-else clause without problem.

> if(match(weekdays(as.Date("2016-01-01"),abbreviate = TRUE),"Mon",nomatch=0)){print("金曜日")}else{print("other days...")}
[1] "other days..."
> if(match(weekdays(as.Date("2016-01-01"),abbreviate = TRUE),"Fri",nomatch=0)){print("金曜日")}else{print("other days...")}
[1] "金曜日"

how to eliminate blank lines by atom regex.

​use "^\s" instead of traditonal "^$". seems that atom's regex works only ​​when at least one character is being consumed.

When given data are below,

[2016-06-01,167],[2016-06-02,155],[2016-06-03,140],[2016-06-04,138],[2016-06-05,144],[2016-06-06,176],[2016-06-07,157],[2016-06-08,146],[2016-06-09,134],[2016-06-10,130],[2016-06-11,119],[2016-06-12,107],[2016-06-13,141]]

regex below will remove all brackets and leave data and just one comma before them. DO NOT FORGET BRACES!

\[+[0-9-]+,([0-9]+)\][,\]]  →  $1,

when original data is as below

423 258 314 175 255 211 134 162 205 168 156 273 258 288 291 243 128

(\b[0-9]*\b) → $1,

wil add comma at the end of each numeric.

423, 258, 314, 175, 255, 211, 134, 162, 205, 168, 156, 273, 258, 288, 291, 243, 128,

77.9 73.5 74.5 72.2 73.1 73.4 72.9 72.7 76.9 73.1 71.8 79 79.9 78.4 77.4 79.6 74.6

(\s) → ,   # actually ", + space"

77.9, 73.5, 74.5, 72.2, 73.1, 73.4, 72.9, 72.7, 76.9, 73.1, 71.8, 79, 79.9, 78.4, 77.4, 79.6, 74.6


2016年6月14日火曜日

Calculate # of months from the beginning of the year.

change language setting causes in other parts of system. the example is below.

> m <- as.numeric(months(Sys.Date(),abbreviate = T)) 

in JA environment, this operation returns 6 in June but ,in EN , months returns "Jun" and it is unable to process in as.numeirc. This breaks operations in the latter parts.

The fix is to use MonthsBetween and mondate in mondate library. They should be independent from language setting.

m <- ceiling(MonthsBetween(mondate("2016-01-01"),mondate(Sys.Date()))) ;
> m
[1] 6
attr(,"timeunits")
[1] "months"