ラベル ヒストグラムhist() の投稿を表示しています。 すべての投稿を表示
ラベル ヒストグラムhist() の投稿を表示しています。 すべての投稿を表示

2020年5月21日木曜日

All Region Daily New Infection stack histgram curl(),matrix(), cbind(),transform(),png(),melt()




  • データは"https://raw.githubusercontent.com/kazogiwara/covid19/master/data/prefectures.csv"から取得
  • データは各県のデータが日付と県名をキーにシーケンシャルに格納されている。必要なデータだけを抽出、ソートの上使用する。 
  • nod は平滑化のための期間パラメータ(日数)である。ここでは 7 とする。
  • Kは、(十進LOG(過去nod日間の新規感染確認者数計)ー 十進LOG(さらにその前のnod日間の新規感染確認者数計))/ nod
  • L = 平均潜伏期間 = 4.76 (7 in china)
  • from https://www.fukuishimbun.co.jp/articles/-/1071227
  • 福井県内で新型コロナウイルスの感染が確認された人のうち、ほかの感染者と接触し発症するまでの潜伏期間が推定できるケースの平均値は「4.76日」だった。
  • D = 平均潜伏感染期間 = 9 あるいはL+2。2は中国での実測値。
  • R = K^2*(L*D)+K*(L+D)+1
  • library(reshape2) を事前に実行しておくこと。melt()に必要。

library(reshape2)
length_graph <- 60 # グラフは過去length_graph日間が対象
l <- 4.76
d <- l+2
nod <- 7

w <- c()
curl <- "https://raw.githubusercontent.com/kaz-ogiwara/covid19/master/data/prefectures.csv"
# curl <- "https://github.com/kaz-ogiwara/covid19/blob/master/data/summary.csv"
cdestfile <- "~/R/R2/covid/pref.csv"
download.file(curl,cdestfile)
w <- read.csv("~/R/R2/covid/pref.csv")
# w <- as.xts(w[,c(5,6)],as.Date(paste(w[,1],w[,2],w[,3],sep='-')))
# とりあえずdata.frameを作って必要なデータを抜きす。
df <- c()
df <- data.frame(t=as.Date(paste(w[,1],w[,2],w[,3],sep='-')),
                r=w[,5],
                p=w[,6])

# データフレームから県名を抜き出し、unique(w[,5])で県名一覧を作る。
# 行列を作成し、各県のデータを抜き出し、順次行列にbindしていく。
i <- 1
mtx <- matrix(diff(df$p[df$r == unique(w[,5])[i]]))
for( i in seq(2,length(unique(w[,5])),1)){
  mtx <- cbind(mtx,diff(df$p[df$r == unique(w[,5])[i]]))
}
# 行列をデータフレームに変換し、そののち日付データを先のデータフレームから抜き出し付加する。
mdf <-as.data.frame(mtx)
mdf <-transform(mdf,t=unique(df$t)[-1])
# データフレームの列名を県名一覧で変更する。
colnames(mdf)[1:length(unique(w[,5]))] <- as.character(unique(w[,5]))

df.melt <- melt(data=mdf, id.vars="t", measure.vars=as.character(unique(w[,5])))
head(df.melt)
df <- df.melt
g <- ggplot(df, aes(x = t, y = value, fill = variable))
g <- g + geom_bar(stat = "identity")


png("04em.png", width = 1400, height = 600)
plot(g)
dev.off()


2019年6月3日月曜日

histogram performance comparison between cli 1 month delta positive and negative.







when "func()" is "cli_delta_vs_period_return_rate.r"

> hist(as.vector(func("minus","1970-01-01")[,1])-1,col=rgb(0.5,1,0),breaks=20,xlim=c(-0.6,0.5),ylim=c(0,7))
0111 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[1] 11.34615
[1] 0.9858096
> par(new=T)
> hist(as.vector(func("plus","1970-01-01")[,1])-1,col=rgb(0.5,0,1,alpha=0.4),breaks=10,xlim=c(-0.6,0.5),ylim=c(0,7))

source code is as below.

#
# 1)pick up months whose clie_xts$oecd  is up from the previous
# 2)create the stream of flags, in which up is 1 and down is 0
# 3)compare SPX close price between the end month of cli_xts delta is plus(or minus) and its start.
# 4)return xts objects which contains. start month of period, updown ration, length of months and monthly average return during period.

func <- function(pm="plus",s="1970-01-01",l=1){
  w <- c()
  cat("0")
  cat(length(s))
  if(nchar(s) == 10){ # use nchar() to measure strings length, not length()
    cat("1")
    last_date <- last(index(cli_xts$oecd))
    start_date <- s
    period <- paste(start_date,last_date,sep='::')
  }else{
     # last_date <- l
     period <- s
  }
  # last_date <- last(index(cli_xts$oecd))
  # start_date <- s
  # period <- paste(start_date,last_date,sep='::')
  start_index <- 1
  iteration <- 0
  performance_val <- c()
  period_length <- c()
  lag_month <- l
  result <- c()
  rate <- c()
  plus_or_minus <- pm
  open_p <- c()
  close_p <- c()

# put flag on the months accoring to the parameter. for "minus" cli delta is less than ZERO, for plus the opposite.
  for(i in seq(1,length(diff(cli_xts$oecd,lag=lag_month)[period]),1,)){
    if(plus_or_minus == "minus"){
      if(as.vector(diff(cli_xts$oecd,lag=lag_month)[period])[i] < 0){  # up is "> 0"
        w <- append(w,1)
      }else{
        w <- append(w,0)
      }
    }else if(plus_or_minus == "plus"){
      if(as.vector(diff(cli_xts$oecd,lag=lag_month)[period])[i] > 0){  # up is "> 0"
        w <- append(w,1)
      }else{
        w <- append(w,0)
      }
    }else{
      stop("please use plus or minus as 1st parameter")
    }
  }
  month_flag <- 0 # status flag
# check stream and when flag is changes 0 to 1. it is the start of period.
  for(i in seq(1,length(diff(cli_xts$oecd,lag=lag_month)[period]),1,)){
    if(w[i] == 1){
      if(month_flag == 0){ # when w is 1 and month_flag is 0, the period starts
        month_flag <- 1
        start_price <- as.vector(to.monthly(SP5[period])[,1][i]) #dc 0531
        # print(index(to.monthly(SP5[period])[,4][i]))
        # cat("from ")
        # cat(as.character(as.Date(index(to.monthly(SP5[period])[,4][i]))))
        start_index <- i
      }
      # dc 0602 add output at the end of loop
      if(i == length(diff(cli_xts$oecd,lag=lag_month)[period])){
        # print("end of the loop")
        result <- append(result,as.xts(as.vector(to.monthly(SP5[period])[,4][i]) / start_price,index(to.monthly(SP5[period])[,4][i])))
                period_length <- append(period_length,i-start_index)
        performance_val <- append(performance_val,as.vector(to.monthly(SP5[period])[,4][i-1]) / start_price)
        open_p <- append(open_p,start_price)
        close_p <- append(close_p,as.vector(to.monthly(SP5[period])[,4][i-1]))
        rate <- append(rate,last(performance_val)**(1/last(period_length))-1)
      }
# check stream and when flag is changes 1 to 0. it is the start of period.
    }else if(w[i] ==0){
      if(month_flag == 1){ # when w is 0 and month_flag is 1, the period ends
        # cat(" ")
        # cat(i - start_index)
        # cat(" month(s)")
        # cat("\n")
        iteration <- iteration +1
        cat(iteration)
        cat(" ")
        result <- append(result,as.xts(as.vector(to.monthly(SP5[period])[,4][i-1]) / start_price,index(to.monthly(SP5[period])[,4][i-1])))
        # print(as.xts(as.vector(to.monthly(SP5[period])[,4][i]) / start_price,index(to.monthly(SP5[period])[,4][i])))
        # print(i - start_index)
        month_flag <- 0 # when period ends, intialize the flag.
        period_length <- append(period_length,i-start_index)
        performance_val <- append(performance_val,as.vector(to.monthly(SP5[period])[,4][i-1]) / start_price)
        open_p <- append(open_p,start_price)
        close_p <- append(close_p,as.vector(to.monthly(SP5[period])[,4][i-1]))
        rate <- append(rate,last(performance_val)**(1/last(period_length))-1)
      }
    }
  }
  cat("\n")
  print(mean(period_length))
  print(mean(performance_val))
  return(merge(result,period_length,rate,open_p,close_p))

}
# t_minus <- performance_val
# t_plus <- performance_val
func("minus","1970-01-01")




2019年3月12日火曜日

VIX vs. CLI 6 month delta. translucent histgram hist setdiff


# downloads ^VIX historical data from Yahoo Finance.
#
VIX <- as.xts(read.zoo(read.csv("~/VIX.csv")))

# select dates when CLI moves negative during 6 months.
mnt <- index(cli_xts$oecd["2000::2018"][cli_xts$oecd["2000::2018"]/as.vector(cli_xts$oecd["1999-07-01::2018-06-01"]) < 1])

  [1] "2000-06-01" "2000-07-01" "2000-08-01" "2000-09-01" "2000-10-01" "2000-11-01" "2000-12-01" "2001-01-01"
  [9] "2001-02-01" "2001-03-01" "2001-04-01" "2001-05-01" "2001-06-01" "2001-07-01" "2001-08-01" "2001-09-01"
<skip>
[105] "2018-05-01" "2018-06-01" "2018-07-01" "2018-08-01" "2018-09-01" "2018-10-01" "2018-11-01" "2018-12-01"

# select dates of positve movement.
> as.Date(setdiff(seq(as.Date("2000-01-01"),as.Date("2018-12-01"),by='months'),mnt))
  [1] "2000-01-01" "2000-02-01" "2000-03-01" "2000-04-01" "2000-05-01" "2001-12-01" "2002-01-01" "2002-02-01"
  [9] "2002-03-01" "2002-04-01" "2002-05-01" "2002-06-01" "2002-07-01" "2003-06-01" "2003-07-01" "2003-08-01"
<skip>
[105] "2017-03-01" "2017-04-01" "2017-05-01" "2017-06-01" "2017-07-01" "2017-08-01" "2017-09-01" "2017-10-01"
[113] "2017-11-01" "2017-12-01" "2018-01-01" "2018-02-01"


as.vector(VIX[,2][as.Date(setdiff(seq(as.Date("2000-01-01"),as.Date("2018-12-01"),by='months'),mnt))])
#   [1] 29.00 28.12 25.87 34.31 32.89 26.38 26.88 27.32 21.12 24.50 22.71 30.98 48.46 22.81 20.80 23.89 23.26 22.82 19.61
#  [20] 18.86 18.68 18.06 22.67 17.98 20.45 17.04 14.39 17.19 15.66 12.44 14.56 13.73 13.34 13.09 19.87 23.81 19.58 16.15
#  [39] 14.49 12.91 12.55 12.68 12.83 19.01 21.25 15.46 14.60 18.98 24.17 37.50 32.77 33.05 28.39 29.57 31.59 31.84 24.51
#  [58] 28.01 29.22 19.94 23.20 48.20 37.38 37.58 28.92 25.13 24.34 23.84 21.43 20.08 23.22 31.28 19.07 21.24 21.06 25.46
#  [77] 15.93 19.28 16.82 18.20 16.35 21.91 17.32 17.81 17.49 21.34 14.14 16.75 18.99 21.48 18.22 17.85 14.49 12.89 25.20
#  [96] 23.43 22.81 14.93 20.51 17.95 23.01 14.72 14.07 12.96 15.11 16.28 16.30 15.16 13.05 17.28 14.06 13.20 14.51 14.58
# [115] 15.42 50.30
# > as.vector(VIX[,2][mnt])
#   [1] 25.01 21.65 20.84 22.66 30.80 31.11 32.32 30.80 30.62 35.45 35.20 26.49 24.42 25.61 25.84 49.35 36.95 34.57 45.21
#  [20] 41.86 43.44 32.60 31.20 35.33 35.66 34.40 30.04 22.33 17.93 19.97 15.98 16.87 16.76 13.74 14.75 13.20 14.89 18.59
#  [39] 17.70 13.34 13.92 14.41 28.82 24.15 31.09 24.86 37.57 29.70 35.60 25.61 20.95 24.56 30.81 23.86 48.40 89.53 81.48
#  [58] 68.60 57.36 53.16 53.25 45.60 36.88 20.03 24.65 25.94 48.00 43.87 46.88 37.53 30.91 23.73 21.98 27.73 21.00 19.25
#  [77] 18.96 19.65 19.40 23.23 17.11 17.57 17.08 31.06 15.93 17.19 16.66 16.36 19.80 20.05 53.29 33.82 25.23 20.67 26.81
#  [96] 32.09 30.90 20.17 17.09 17.65 26.72 17.04 26.22 25.72 18.78 19.61 18.08 16.86 15.63 28.84 23.81 36.20

# compare monthly high between negative vs. positve
t.test(as.vector(VIX[,2][mnt]),as.vector(VIX[,2][as.Date(setdiff(seq(as.Date("2000-01-01"),as.Date("2018-12-01"),by='months'),mnt))]))

# Welch Two Sample t-test
#
# data:  as.vector(VIX[, 2][mnt]) and as.vector(VIX[, 2][as.Date(setdiff(seq(as.Date("2000-01-01"), as.vector(VIX[, 2][mnt]) and     as.Date("2018-12-01"), by = "months"), mnt))])
# t = 4.8495, df = 174.7, p-value = 2.725e-06
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
#  4.164086 9.879529
# sample estimates:
# mean of x mean of y
#  28.55741  21.53560

# compare monthly close.
t.test(as.vector(VIX[,4][mnt]),as.vector(VIX[,4][as.Date(setdiff(seq(as.Date("2000-01-01"),as.Date("2018-12-01"),by='months'),mnt))]))

# Welch Two Sample t-test
#
# data:  as.vector(VIX[, 4][mnt]) and as.vector(VIX[, 4][as.Date(setdiff(seq(as.Date("2000-01-01"), as.vector(VIX[, 4][mnt]) and     as.Date("2018-12-01"), by = "months"), mnt))])
# t = 4.7722, df = 178.21, p-value = 3.781e-06
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
#  2.860369 6.893837
# sample estimates:
# mean of x mean of y
#  22.19598  17.31888

par(mfrow=c(2,1))

hist(as.vector(VIX[,4][mnt]),ylim=c(0,40),xlim=c(10,60),breaks=20)
hist(as.vector(VIX[,4][as.Date(setdiff(seq(as.Date("2000-01-01"),as.Date("2018-12-01"),by='months'),mnt))]),ylim=c(0,40),xlim=c(10,60),breaks=10)

# 半透明色ヒストグラム

hist(as.vector(VIX[,4][as.Date(setdiff(seq(as.Date("2000-01-01"),as.Date("2018-12-01"),by='months'),mnt))]),ylim=c(0,40),xlim=c(10,60),breaks=10,col=2)
par(new=T)
hist(as.vector(VIX[,4][mnt]),ylim=c(0,40),xlim=c(10,60),breaks=20,col=rgb(0, 1, 0, alpha=0.1))

                              OR

hist(as.vector(VIX[,4][as.Date(setdiff(seq(as.Date("2000-01-01"),as.Date("2018-12-01"),by='months'),mnt))]),ylim=c(0,30),xlim=c(10,60),breaks=10,col=rgb(1, 0, 0, alpha=0.5))
par(new=T)
hist(as.vector(VIX[,4][mnt]),ylim=c(0,30),xlim=c(10,60),breaks=20,col=rgb(0, 0, 1, alpha=0.1))

Red for positive and Blue for negative.



change color scheme and transparency. 





2018年8月5日日曜日

TIPS 20180805



警告の抑止(未検証) 

options(warn=-1)
library(・・・)
# 読み込んだよーというメッセージがでなくなる

警告の抑止(検証済)  その2

        sink(file="/tmp/r.log") # console output is redirected to "/tmp/r.log"
     
        sink()  # reset to the console.

警告の抑止(未検証) その3

        options("getSymbols.yahoo.warning"=FALSE).

ヒストグラムの出力


  # kikan like kikan or "2018::06-21::2018-07-13"
  # len is a number of samples.
  # loc_x is x-axis position kikan 0 to 1.
  # loc_y is y-axis.
  # br is breaks for hist()
  # ymax is max value of y-axis.
  # xmin and xmax are for x-axis
  # color is from 1 to 9?
  hist(as.vector(last(par_xts[kikan][,highlow],n=len)),breaks=br,xlim=c(xmin,xmax),ylim=c(0,ymax),col=color)
  axis(side=2, pos=round(mean(par_xts[kikan][,highlow])),labels=F)
 #
 # par + text to output overlay messages on the graph.
 # graph's dimension data to graph_dim
 # "graph_dim[1] + graph_dim[2] *  loc_x " is 50% of horizontal loc
 #
  graph_dim <- par('usr')
  text( graph_dim[1] + graph_dim[2] *  loc_x   ,(graph_dim[4] - graph_dim[3]) * (loc_y+0) + graph_dim[3] ,paste("#",len,sep="="),adj=c(0,0))

垂直線をxtsグラフに引く


events <- xts(c("natrix","weight"),as.Date(c("2018-06-20", "2018-07-14")))
addEventLines(events, srt=90, pos=2,col=10)

   未検証だが以下もOKなはず。

addEventLines(as.xts(c("natrix","weight"),as.Date(c("2018-06-20", "2018-07-14"))), srt=90, pos=2,col=10)

多面プロットのやり方(plot when multi.panel=T and yaxis.same=F)

plot(merge(to.monthly(N225["2007-01::"])[,4],as.vector(residuals(result)/to.monthly(N225)[,4])),multi.panel=TRUE,yaxis.same=FALSE)

季節調整 case shiller 10 city composite.


CS.stl <- stl(ts(as.numeric(CS),frequency=12), s.window="periodic")
last(merge(CS,as.numeric(CS.stl$time.series[,2]),suffixes = c("","season")),n=24)
plot(last(merge(CS,as.numeric(CS.stl$time.series[,2])),n=48)[,1] - last(merge(CS,as.numeric(CS.stl$time.series[,2])),n=48)[,2])

chartSeries(
  weekly_pf,
  show_grid=TRUE,
  type="candlesticks"
)

ffmpegの使い方 再生できないファイルがある場合


再生できないファイルがある場合、mp4に変換してやるとうまくvlcで再生できることが多い。以下はcodecを変更しないでコンテナだけをmp4に変えてやる使い方。以下はaviファイルでオーディオストリームに問題があるらしい場合のエラーメッセージ。

[avi @ 0x7ffaf7800000] Could not find codec parameters for stream 1 (Audio: mp3 (mp3float) (U[0][0][0] / 0x0055), 48000 Hz, 2 channels, fltp, 128 kb/s): unspecified frame size


で、オーディオストリームはそのままにしてコンテナだけをmp4に変更する。

# no conversion just switch container. highly reliable.

ffmpeg -i <INPUT>.avi -codec:v copy -codec:a copy <OUTPUT>.mp4

OR

ffmpeg -i <INPUT>.avi -codec:a copy out.mp4

こちらは ビデオストリームをコピーする例。やはり、コンテナはmp4に変更する。

ffmpeg -i <INPUT>.mkv -vcodec copy <OUTPUT>.mp4

OR

ffmpeg -i sample_input.mkv -vcodec copy sample_output.mp4

for文とカウンタ in bash


j=0;for i in  *.mkv; do echo $i;  let ++j ; echo $j ; ffmpeg -y -i $i -vcodec copy $j.mp4; done


  • ;を書く文の最後に
  • let をつかってカウンタを加算する
  • ffmpeg -yで既存ファイルがあっても強制上書き
  • forはdo とdoneで受ける
  • 変数は初期化すること

以下と等価となる。

j=0;for i in  *.mkv; 
> do echo $i
> let ++j 
> echo $j 
> ffmpeg -y -i $i -vcodec copy $j.mp4
> done

j=0;for i in *.mp4; do let ++j; mv $j.mp4 hibike_euphonuim_S2E$j.mp4; done


  • -e 大事。これがないと\1 が使えない。(間違い。なくてもOK)
  • さいごの「2」で出現順序を指定して置換する。
  • \(<patter>\)で指定して\1で受けてる。

Zero Padding by sed

ただし、awkのgensub関数を使ったほうが良いと思う

Hibike Euphonium 1 [BD 720p]$ ls *.mp4 | awk '{ print "mv "$1" "$1}' | gsed -e 's/E\([1-9]\)\./E0\1./2'
mv hibike_euphonuim_S1E1.mp4 hibike_euphonuim_S1E01.mp4
mv hibike_euphonuim_S1E10.mp4 hibike_euphonuim_S1E10.mp4
mv hibike_euphonuim_S1E11.mp4 hibike_euphonuim_S1E11.mp4

awk のgensub関数を使えば、sedは使わなくて良いはず。第二引数の"g"はグローバル?例えば2を指定すると2番めにヒットした正規表現だけを処理してくれるらしい。(要研究)

ls *.mp4 | awk '{ print "ls "$1" "gensub("0([1-9])","1\\1","g",$1)}' 

gensub関数 in awk

gensub(regexp, replacement, how [, target])
gensubは汎用的な置換関数である。subやgsubのように 対象文字列targetから正規表現regexpに マッチする部分を検索する。subやgsubと違うのは、 関数の戻り値として置換が行われた文字列を返し、 元の文字列を変更しないという点である。

gensub を使うとこうなる。

K-On!$ ls | awk '{print "ffmpeg -i \""$0"\" K-OnS1E" gensub(/(\y.\y)/,"0\\1",1,$3)".mp4"}'

ffmpeg -i "K-ON! Ep 01 - Dissolution!.mkv" K-OnS1E01.mp4
ffmpeg -i "K-ON! Ep 02 - Instruments!.mkv" K-OnS1E02.mp4
ffmpeg -i "K-ON! Ep 03 - Special Lessons!.mkv" K-OnS1E03.mp4
ffmpeg -i "K-ON! Ep 04 - Training Camp!.mkv" K-OnS1E04.mp4
ffmpeg -i "K-ON! Ep 05 - Advisor!.mkv" K-OnS1E05.mp4
ffmpeg -i "K-ON! Ep 06 - School Festival!.mkv" K-OnS1E06.mp4
ffmpeg -i "K-ON! Ep 09 - New Club Member!.mkv" K-OnS1E09.mp4
ffmpeg -i "K-ON! Ep 10 - Another Training Camp!.mkv" K-OnS1E10.mp4
ffmpeg -i "K-ON! Ep 11 - Crisis!.mkv" K-OnS1E11.mp4
ffmpeg -i "K-ON! Ep 12 (Season Finale) - Light Music!.mkv" K-OnS1E12.mp4
ffmpeg -i "K-ON! Ep 13 (Extra) - Winter Days!.mkv" K-OnS1E13.mp4
ffmpeg -i "K-ON! Ep 14 (OVA) - Live House!.mkv" K-OnS1E14.mp4
ffmpeg -i "K-ON! Ep 7 - Christmas!.mkv" K-OnS1E07.mp4
ffmpeg -i "K-ON! Ep 8 - Freshman Reception!.mkv" K-OnS1E08.mp4

gawk 正規表現

\w
これは単語を構成する任意のキャラクタ、つまり 文字、数字、それとアンダースコアにマッチする演算子である。 これは [[:alnum:]_] の簡潔な表現とみなして良い。
\W
これは単語を構成する要素にならない任意のキャラクタにマッチする 演算子である。これは [^[:alnum:]_] の簡潔な表現とみなして良い。
\<
これは単語の先頭にある空文字列にマッチする演算子である。 例えば、/\<away/`away'にマッチするが、 `stowaway'にはマッチしない。
\>
これは単語の末尾にある空文字列にマッチする演算子である。 例えば、/stow\>/`stow'にマッチするが、 `stowaway'にはマッチしない。
\y
これは単語の先頭、あるいは末尾の空文字列とマッチする演算子である (つまり語の区切りとマッチするということである)。 例えば、`\yballs?\y'は独立した単語として `ball' にも `balls'にもマッチする。注意! 他のGNU softwareは単語の区切りに\bを使うらしい。
\B
この演算子は単語中の空文字列にマッチする。言い換えると、`\B'は二つ の単語の構成要素文字の間にある空文字列にマッチするということである。例え ば、/\Brat\B/ は`crate'にマッチする。しかし、`dirty rat' にはマッチしない。`B'は簡単にいうと`\y'の反対語である。
\( .... \) () でマッチの塊を扱う
\n  \n は n 番目の () に対応 ([a-z]*) \1*




2018年8月3日金曜日

Draw histgrams to compare high and low


Write function to draw the histgram being optimized for recording blood pressure.

my_bp_hist_x <- function(par_xts,kikan,highlow,len,loc_x,loc_y,br,ymax,xmin,xmax,color){
  # kikan like kikan or "2018::06-21::2018-07-13"
  # highlow 1 or 2. 1 means high, 2 for low.(loc_y+0.04)
  # len is a number of samples.
  # loc_x is x-axis position kikan 0 to 1.
  # loc_y is y-axis.
  # br is breaks for hist()
  # ymax is max value of y-axis.
  # xmin and xmax are for x-axis
  # color is from 1 to 9?
  # function(bp.bangkok,"::2016-06-21,2",length(as.vector(bp.bangkok["2018-06-21::"][,2]),0.3,0.5,20,55,120,2)
  hist(as.vector(last(par_xts[kikan][,highlow],n=len)),breaks=br,xlim=c(xmin,xmax),ylim=c(0,ymax),col=color)
  # axis(side=2, pos=84,labels=F)
  axis(side=2, pos=round(mean(par_xts[kikan][,highlow])),labels=F)
  graph_dim <- par('usr')
  text( graph_dim[1] + graph_dim[2] *  loc_x   ,(graph_dim[4] - graph_dim[3]) * (loc_y+0) + graph_dim[3] ,paste("#",len,sep="="),adj=c(0,0))
  text( graph_dim[1] + graph_dim[2] * loc_x   ,(graph_dim[4] - graph_dim[3]) * (loc_y+0.1) + graph_dim[3] ,paste("mean",round(mean(par_xts[kikan][,highlow]),2),sep="="),adj=c(0,0))
  text( graph_dim[1] + graph_dim[2] * loc_x   ,(graph_dim[4] - graph_dim[3]) * (loc_y+0.2) + graph_dim[3] ,paste("sd",round(sd(par_xts[kikan][,highlow]),2),sep="="),adj=c(0,0))
  text( graph_dim[1] + graph_dim[2] * loc_x   ,(graph_dim[4] - graph_dim[3]) * (loc_y+0.3) + graph_dim[3] ,paste("period ",kikan,sep="="),adj=c(0,0))
}


Use as below which replaces Aug 2 entry.


par(mfrow=c(3,1))
my_bp_hist_x(bp.bangkok,"::2018-06-19",2,70,0.1,0.6,20,15,55,100,4)
my_bp_hist_x(bp.bangkok,"2018-06-21::2018-07-13",2,length(bp.bangkok["2018-06-21::2018-07-13"][,2]),0.1,0.6,20,15,55,100,5)
my_bp_hist_x(bp.bangkok,"2018-07-15::",2,length(bp.bangkok["2018-07-15::"][,2]),0.1,0.6,10,15,55,100,6)
par(mfrow=c(1,1))


par(mfrow=c(3,1))
my_bp_hist_x(bp.bangkok,"::2018-06-19",1,70,0.1,0.6,30,15,90,150,4)
my_bp_hist_x(bp.bangkok,"2018-06-21::2018-07-13",1,length(bp.bangkok["2018-06-21::2018-07-13"][,2]),0.1,0.6,20,15,90,150,5)
my_bp_hist_x(bp.bangkok,"2018-07-15::",1,length(bp.bangkok["2018-07-15::"][,2]),0.1,0.6,20,15,90,150,6)
par(mfrow=c(1,1))

2018年8月2日木曜日

Draw histgram in multiple panes.



  1. Draw histgrams in 3 panes. Each pane represents
    1. before 2018/06/19
    2. between 06/21 and 07/13
    3. after 07/15
  2. Set the mean value mark by axis command.
  3. Output overlay text for Standard Deviation, Mean value and the number of samples.

par(mfrow=c(3,1))
hist(as.vector(last(bp.bangkok["::2018-06-19"][,2],n=length(as.vector(bp.bangkok["2018-06-21::"][,2])))),breaks=20,xlim=c(55,100),ylim=c(0,15),col=3)
# axis(side=2, pos=84,labels=F) 
axis(side=2, pos=round(mean(bp.bangkok["::2018-06-19"][,2])),labels=F) 
graph_dim <- par('usr') 
text( (graph_dim[1] + graph_dim[2]) / 2.2   ,(graph_dim[4] - graph_dim[3]) * 0.64 + graph_dim[3] ,paste("#",round(length(bp.bangkok["2018-06-21::"][,2]),2),sep="="),adj=c(0,0))
text( (graph_dim[1] + graph_dim[2]) / 2.2   ,(graph_dim[4] - graph_dim[3]) * 0.70 + graph_dim[3] ,paste("mean",round(mean(bp.bangkok["::2018-06-19"][,2]),2),sep="="),adj=c(0,0))
text( (graph_dim[1] + graph_dim[2]) / 2.2   ,(graph_dim[4] - graph_dim[3]) * 0.76 + graph_dim[3] ,paste("sd",round(sd(bp.bangkok["::2018-06-19"][,2]),2),sep="="),adj=c(0,0))
hist(as.vector(bp.bangkok["2018-06-21::2018-07-13"][,2]),breaks=20,xlim=c(55,100),ylim=c(0,15),col=2)
axis(side=2, pos=round(mean(bp.bangkok["2018-06-21::2018-07-13"][,2])),labels=F) 
graph_dim <- par('usr') 
text( (graph_dim[1] + graph_dim[2]) / 2.2   ,(graph_dim[4] - graph_dim[3]) * 0.64 + graph_dim[3] ,paste("#",round(length(bp.bangkok["2018-06-21::2018-07-13"][,2]),2),sep="="),adj=c(0,0))
text( (graph_dim[1] + graph_dim[2]) / 2.2   ,(graph_dim[4] - graph_dim[3]) * 0.70 + graph_dim[3] ,paste("mean",round(mean(bp.bangkok["2018-06-21::2018-07-13"][,2]),2),sep="="),adj=c(0,0))
text( (graph_dim[1] + graph_dim[2]) / 2.2   ,(graph_dim[4] - graph_dim[3]) * 0.76 + graph_dim[3] ,paste("sd",round(sd(bp.bangkok["2018-06-21::2018-07-13"][,2]),2),sep="="),adj=c(0,0))
hist(as.vector(bp.bangkok["2018-07-15::"][,2]),breaks=10,xlim=c(55,100),ylim=c(0,15),col=4)
axis(side=2, pos=round(mean(bp.bangkok["2018-07-15::"][,2])),labels=F) 
graph_dim <- par('usr') 
text( (graph_dim[1] + graph_dim[2]) / 1.8   ,(graph_dim[4] - graph_dim[3]) * 0.64 + graph_dim[3] ,paste("#",round(length(bp.bangkok["2018-07-15::"][,2]),2),sep="="),adj=c(0,0))
text( (graph_dim[1] + graph_dim[2]) / 1.8   ,(graph_dim[4] - graph_dim[3]) * 0.70 + graph_dim[3] ,paste("mean",round(mean(bp.bangkok["2018-07-15::"][,2]),2),sep="="),adj=c(0,0))
text( (graph_dim[1] + graph_dim[2]) / 1.8   ,(graph_dim[4] - graph_dim[3]) * 0.76 + graph_dim[3] ,paste("sd",round(sd(bp.bangkok["2018-07-15::"][,2]),2),sep="="),adj=c(0,0))
par(mfrow=c(1,1))

2018年7月26日木曜日

Manage Blood Pressure data



# 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)
#
#  draw the graph
#
plot(bp.xts[,c(1,2)][bp.xts$High > 95],col = c("red", "blue"),lwd=c(3,3,2,2),major.ticks='days',grid.ticks.on='days',type='p',ylim=c(60,160))
# draw a horizontal line at 130 as the benchmark
addSeries(xts(rep(130,length(index(bp.xts[bp.xts$High > 95]))),index(bp.xts[bp.xts$High > 95])),ylim=c(60,160),on=1,col=5,lwd=1)
# draw another line at 85.
addSeries(xts(rep(85,length(index(bp.xts[bp.xts$High > 95]))),index(bp.xts[bp.xts$High > 95])),ylim=c(60,160),on=1,col=5,lwd=1)
# add aux axis
axis(2,at=c(135,130,125,120,115,85))
axis(4,at=c(135,130,125,120,115,85))
# draw the horizontal line at the average
addSeries(xts(rep(mean(bp.xts[,1][bp.xts$High > 95]),length(index(bp.xts[bp.xts$High > 95]))),index(bp.xts[bp.xts$High > 95])),ylim=c(60,160),on=1,col=6,lwd=1)
addSeries(xts(rep(mean(bp.xts[,2][bp.xts$High > 95]),length(index(bp.xts[bp.xts$High > 95]))),index(bp.xts[bp.xts$High > 95])),ylim=c(60,160),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)

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


#
# t test between before 2018-06-20 and after 2018-06-21
#  2018-06-20 is to add natrix into prescription.
#  2018-07-14 is to start weight training.
#
t.test(as.vector(last(bp.bangkok["::2018-06-20"][,2],n=length(as.vector(bp.bangkok["2018-06-21::"][,2])))),as.vector(bp.bangkok["2018-06-21::"][,2]))
#
# t test no.2
#
t.test(as.vector(bp.bangkok["2018-06-21::2018-07-13"][,2]),as.vector(bp.bangkok["2018-07-15::"][,2]))
#
# t test no.3
#
t.test(as.vector(last(bp.bangkok["::2018-07-13"][,2],n=length(as.vector(bp.bangkok["2018-07-15::"][,2])))),as.vector(bp.bangkok["2018-07-15::"][,2]))
#
# t test no.4
#
t.test(as.vector(last(bp.bangkok["::2018-06-19"][,2],n=length(as.vector(bp.bangkok["2018-08-16::"][,2])))),as.vector(bp.bangkok["2018-08-16::"][,2]))



#
# use anonymous function in line to calculate the average of the data sorted by the time to take samples of higher pressure.
#
mapply(function(x,y){return(mean(na.omit(bp.xts[strptime(format(index(bp.xts),"%H:%M:%S"),"%H:%M:%S") > strptime(x,"%H:%M:%S") & strptime(format(index(bp.xts),"%H:%M:%S"),"%H:%M:%S") < strptime(y,"%H:%M:%S")])
[,1]))},c("05:00:00","06:00:00","07:00:00","08:00:00","09:00:00","10:00:00","11:00:00","23:00:00","00:00:00"),c("05:00:00","06:59:00","07:59:00","08:59:00","09:59:00","10:59:00","11:59:00","23:59:00","00:59:00"))
#
# mean value of the lower prssure.
#
mapply(function(x,y){return(mean(na.omit(bp.xts[strptime(format(index(bp.xts),"%H:%M:%S"),"%H:%M:%S") > strptime(x,"%H:%M:%S") & strptime(format(index(bp.xts),"%H:%M:%S"),"%H:%M:%S") < strptime(y,"%H:%M:%S")])
[,2]))},c("05:00:00","06:00:00","07:00:00","08:00:00","09:00:00","10:00:00","11:00:00","23:00:00","00:00:00"),c("05:00:00","06:59:00","07:59:00","08:59:00","09:59:00","10:59:00","11:59:00","23:59:00","00:59:00"))
#
#  count the number of samples
#
mapply(function(x,y){return(length(na.omit(bp.xts[strptime(format(index(bp.xts),"%H:%M:%S"),"%H:%M:%S") > strptime(x,"%H:%M:%S") & strptime(format(index(bp.xts),"%H:%M:%S"),"%H:%M:%S") < strptime(y,"%H:%M:%S")])
[,1]))},c("05:00:00","06:00:00","07:00:00","08:00:00","09:00:00","10:00:00","11:00:00","23:00:00","00:00:00"),c("05:00:00","06:59:00","07:59:00","08:59:00","09:59:00","10:59:00","11:59:00","23:59:00","00:59:00"))


par(mfrow=c(3,1))
hist(as.vector(last(bp.bangkok["::2018-06-20"][,2],n=length(as.vector(bp.bangkok["2018-06-21::"][,2])))),breaks=20,xlim=c(55,100),ylim=c(0,15),col=3)
# axis(side=2, pos=84,labels=F) 
axis(side=2, pos=round(mean(bp.bangkok["::2018-06-20"][,2])),labels=F) 
hist(as.vector(bp.bangkok["2018-06-21::2018-07-13"][,2]),breaks=20,xlim=c(55,100),ylim=c(0,15),col=2)
axis(side=2, pos=round(mean(bp.bangkok["2018-06-21::2018-07-13"][,2])),labels=F) 
hist(as.vector(bp.bangkok["2018-07-15::"][,2]),breaks=10,xlim=c(55,100),ylim=c(0,15),col=4)
axis(side=2, pos=round(mean(bp.bangkok["2018-07-15::"][,2])),labels=F) 
graph_dim <- par('usr') 
text( (graph_dim[1] + graph_dim[2]) / 2   ,(graph_dim[4] - graph_dim[3]) * 0.70 + graph_dim[3] ,paste("mean",mean(bp.bangkok["2018-07-15::"][,2]),sep="="),adj=c(0,0))
text( (graph_dim[1] + graph_dim[2]) / 2   ,(graph_dim[4] - graph_dim[3]) * 0.75 + graph_dim[3] ,paste("sd",sd(bp.bangkok["2018-07-15::"][,2]),sep="="),adj=c(0,0))
par(mfrow=c(1,1))

2018年7月17日火曜日

T test to compare before and after the new drug prescribed.


t.test(as.vector(last(bp.bangkok["::2018-06-20"][,2],n=length(as.vector(bp.bangkok["2018-06-21::"][,2])))),as.vector(bp.bangkok["2018-06-21::"][,2]))
par(mfrow=c(2,1))
hist(as.vector(last(bp.bangkok["::2018-06-20"][,2],n=length(as.vector(bp.bangkok["2018-06-21::"][,2])))),breaks=20,xlim=c(55,100),ylim=c(0,10),col=3)
hist(as.vector(bp.bangkok["2018-06-21::"][,2]),breaks=20,xlim=c(55,100),ylim=c(0,10),col=2)
par(mfrow=c(1,1))
#
# mean value before
#
mean(last(bp.bangkok["::2018-06-20"][,2],n=length(as.vector(bp.bangkok["2018-06-21::"][,2]))))
#
# mean value after
#
mean(as.vector(bp.bangkok["2018-06-21::"][,2]))

  • 2018-06-20 is the day to start to add the new drug for me.
  • Comparing low blood pressure before and after the date.
  • The number of sample is standardized.
  • Drawing histograms to compare graphically.