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

0 件のコメント: