使用R从流量数据计算喇叭曲线

时间:2012-07-10 21:23:53

标签: r ggplot2

我正在寻找一种使用R(最好是ggplot2来计算静脉设定流量数据)来计算小号曲线的方法,尽管这并不重要。以下是流体加温泵的一些流量数据:http://pastebin.com/vJmGcJmn

到目前为止,我有这个:

flow<-read.table(file="flow.dat",header=T)
flow$diff<-c(0,diff(flow$Mass))
ggplot(data=flow, aes(x=Secs,y=diff)) + geom_line()

请注意,所需的小号图符合ISO 60601-2-24(如果这意味着什么!)

此流量设定为150毫升/分钟。

4 个答案:

答案 0 :(得分:3)

对不起,这花了很长时间,迫不及待。

您提供的数据似乎不符合您对小号曲线所代表的内容的描述,或者我错过了一些重要内容。如果你能简单地说明需要对数据做些什么,我将不胜感激。

当你设法为输出塑造数据时,你可以将它粘贴到下面的代码中,它应该产生一个图。我会根据您的需要定制它。

# generate some random data
trump <- data.frame(
   curve1 = rev(sort(rchisq(100, df = 2))) * rnorm(100, mean = 5, sd = 0.1) + 3,
   curve2 = -rev(sort(rchisq(100, df = 2))) * rnorm(100, mean = 5, sd = 0.1) - 3
)
trump <- trump[seq(from = 1, to = 100, by = 3), ]

ggplot(trump, aes(x = 1:nrow(trump), y = curve1)) + 
   geom_line() +
   geom_point() +
   geom_line(aes(y = curve2)) + 
   geom_point(aes(y = curve2)) +
   geom_hline(aes(yintercept = 0), linetype = "solid") + # overall percentage error
   geom_hline(aes(yintercept = 5), linetype = "dashed") + # set rate
   xlab("Observation windows (in minutes)") +
   ylab("% error") + 
   annotate("text", x = 8, y = -1.5, label = "overall percentage error") +
   annotate("text", x = 5, y = 3, label = "set rate") +
   annotate("text", x = 10, y = -24, label = "min. error") +
   annotate("text", x = 10, y = 24, label = "max. error") +
   theme_bw()

enter image description here

答案 1 :(得分:1)

非常感谢罗曼的帮助。这就是我们最终做到的方式。

flow<-read.table("iv.txt",comment.char="#",header=TRUE)
Q<-as.data.frame(c(0,(60*diff(flow$wt,1,1)/0.5)))  # Q is used for flow in  the standard
names(Q)<-"Q"
# Now combine them
l<-list(flow,Q)
dflow<-as.data.frame(l)
t1flow<-subset(dflow, dflow$secs>60*60 & dflow$secs<=120*60) # require the second hour of the data
t1flow$QE<-((t1flow$Q-setflow)/setflow)*100  # calculate the error
library(TTR) # use this for moving averages
# 
# Avert your eyes!  I know there must be a slicker way of doing this .....
#     
# Calculate the moving average using a sample rate of every 4 readings (2 mins of 30sec readings)
QE2<-SMA(t1flow$QE,4)
minQE2<-min(QE2,na.rm=TRUE)
maxQE2<-max(QE2,na.rm=TRUE)
# now for the 5 minute window
QE5<-SMA(t1flow$QE,10)
minQE5<-min(QE5,na.rm=TRUE)
maxQE5<-max(QE5,na.rm=TRUE)
# Set window to 11 mins
QE11<-SMA(t1flow$QE,22)
minQE11<-min(QE11,na.rm=TRUE)
maxQE11<-max(QE11,na.rm=TRUE)
# Set window to 19 mins
QE19<-SMA(t1flow$QE,38)
minQE19<-min(QE19,na.rm=TRUE)
maxQE19<-max(QE19,na.rm=TRUE)
# Set window to 31 mins
QE31<-SMA(t1flow$QE,62)
minQE31<-min(QE31,na.rm=TRUE)
maxQE31<-max(QE31,na.rm=TRUE)
# 
# OK - you can look again :-)
#
# create a data frame from this data
trump<-data.frame(c(2,5,11,19,31),c(minQE2,minQE5,minQE11,minQE19,minQE31),c(maxQE2,maxQE5,maxQE11,maxQE19,maxQE31))
names(trump)<-c("T","minE","maxE")
A<-mean(t1flow$QE) # calculate the overall mean percentage error
error_caption<-paste("overall percentage error = ",A,"%") # create the string to label the error line
# plot the graph
ggplot(trump, aes(x = T, y = minE)) +
    geom_line() +
    geom_point(color="red") +
    geom_line(aes(y = maxE)) +
    geom_point(aes(y = maxE),colour="red") +
    geom_hline(aes(yintercept = 0), linetype = "dashed") + # overall percentage error
    geom_hline(aes(yintercept = A), linetype = "solid") + # set rate
    xlab("Observation windows (in minutes)") +
    ylab("% error") +
    scale_x_continuous(breaks=c(0,2,5,11,19,31),limits=c(0,32)) +   # label the x axis only at the window values
    annotate("text", x = 10, y = A-0.5, label = error_caption) +  # add the error line label
    opts(title="Trumpet curve for Test Data")

Final Trumpet Graph

也可以使用你可以看到最终图[这里] [2]

答案 2 :(得分:0)

我目前正试图解决小号曲线计算错误。基于IEC 60601-2-24:1998 - 第八节 - 操作数据的准确性 和对危险输出的保护。这里是我的R代码:

    flow<-read.table("c:\\ciringe.txt",comment.char="#",header=TRUE)
    #parameters
    # setflow = 1 
    # P = 1,2,5,11,19,31

    P1<-1
    P2<-2
    P5<-5 
    P11<-11
    P19<- 19
    P31<-31 

    P1m = ((60 - P1) / 0.5 ) + 1
    P2m = ((60 - P2) / 0.5 ) + 1
    P5m = ((60 - P5) / 0.5 ) + 1
    P11m = ((60 - P11) / 0.5 ) + 1
    P19m = ((60 - P19) / 0.5 ) + 1
    P31m = ((60 - P31) / 0.5 ) + 1


    setflow<-1
    mQE1<-0.5
    mQE2<-0.25
    mQE5<-0.1
    mQE11<-0.045
    mQE19<-0.0263
    mQE31<-0.0161

    Q<-as.data.frame(c(0,(60*diff(flow$wt,1,1)/0.5*0.998)))  # Q is used for    flow in  the standard
   names(Q)<-"Q"
   # Now combine them
   l<-list(flow,Q)
   dflow<-as.data.frame(l)
   t1flow<-subset(dflow, dflow$secs>=3600 & dflow$secs<=7200) # require the   second hour of the data

   #overall 
   t1flow$QE<-(((t1flow$Q-setflow)/setflow)*100)   # calculate the error

   t1flow$QE1<-(((t1flow$Q-setflow)/setflow)*100)  * mQE1 # calculate the   error
   t1flow$QE2<-(((t1flow$Q-setflow)/setflow)*100) * mQE2  # calculate the error
   t1flow$QE5<-(((t1flow$Q-setflow)/setflow)*100) * mQE5 # calculate the error
   t1flow$QE11<-(((t1flow$Q-setflow)/setflow)*100) * mQE11  # calculate the error
   t1flow$QE19<-(((t1flow$Q-setflow)/setflow)*100) * mQE19  # calculate the error
   t1flow$QE31<-(((t1flow$Q-setflow)/setflow)*100) * mQE31  # calculate the error

   library(TTR) # use this for moving averages
   # 
   # Avert your eyes!  I know there must be a slicker way of doing this .....
   #     
   # Calculate the moving average using a sample rate of every
   # 4 readings (2 mins of 30sec readings)

   # now for the 1 minute window
   QE1<-SMA(t1flow$QE1,2)
   minQE1<-min(QE1,na.rm=TRUE)
   maxQE1<-max(QE1,na.rm=TRUE)

   # now for the 2 minute window


   QE2<-SMA(t1flow$QE2,4)
   minQE2<-min(QE2,na.rm=TRUE)
   maxQE2<-max(QE2,na.rm=TRUE)

   # now for the 5 minute window

   QE5<-SMA(t1flow$QE5,10)
   minQE5<-min(QE5,na.rm=TRUE)
   maxQE5<-max(QE5,na.rm=TRUE)

   # Set window to 11 mins

   QE11<-SMA(t1flow$QE11,22)
   minQE11<-min(QE11,na.rm=TRUE)
   maxQE11<-max(QE11,na.rm=TRUE)

   # Set window to 19 mins

   QE19<-SMA(t1flow$QE19,38)
   minQE19<-min(QE19,na.rm=TRUE)
   maxQE19<-max(QE19,na.rm=TRUE)

   # Set window to 31 mins


   QE31<-SMA(t1flow$QE31,62)
   minQE31<-min(QE31,na.rm=TRUE)
   maxQE31<-max(QE31,na.rm=TRUE)

   # 
   # OK - you can look again :-)
   #
   # create a data frame from this data
   trump<-    data.frame(c(1,2,5,11,19,31),c(minQE1,minQE2,minQE5,minQE11,minQE19,minQE31), c(maxQE1,maxQE2,maxQE5,maxQE11,maxQE19,maxQE31))
   names(trump)<-c("T","minE","maxE")
   A<-mean(t1flow$QE) # calculate the overall mean percentage error
   error_caption<-paste("overall percentage error = ",A,"%") # create the    string to label the error line
   # plot the graph
   library(ggplot2)
   ggplot(trump, aes(x = T, y = minE)) +
   geom_line() +
   geom_point(color="red") +
   geom_line(aes(y = maxE)) +
   geom_point(aes(y = maxE),colour="red") +
   geom_hline(aes(yintercept = 0), linetype = "dashed") + # overall percentage error
   geom_hline(aes(yintercept = A), linetype = "solid") + # set rate
    xlab("Observation windows (in minutes)") +
    ylab("% error") +
    scale_x_continuous(breaks=c(0,2,5,11,19,31),limits=c(0,32)) +   # label   the x axis only at the window values
    annotate("text", x = 10, y = A-0.5, label = error_caption)   # add the   error line label

实际上,我不理解ISO文档(百分比变化)中所述的总和系列

答案 3 :(得分:0)

您的公式与ISO 60601-2-24中的公式不匹配。该规范有点繁琐,但没有2分钟的窗口或移动平均线。首先,将您的数据格式化为1分钟样本(根据规范)。以1分钟的间隔和列:

执行数据数组
colTotalLiquid = 1  #total liquid  (precalculated from weight)
colProgRate = 2         #prog rate
colQi   = 3         # rate Q(i) from IEC60601-2-24


obsWindow = (2, 5, 11, 19, 31)                  # observation windows in minutes
analysisT1start = 60                            # start of analysis period T1
analysisT1end = 120                             # end of analysis period T1

t1err = dict.fromkeys(obsWindow, None)

progRate = data[analysisT1start][colProgRate]
A = 100 * (((data[analysisT1end][colTotalLiquid] - data[analysisT1start][colTotalLiquid]) * hours / (analysisT1end - analysisT1start)) - progRate) / progRate

t1TrumpetX = []     #mintes
t1TrumpetY1 = []    #Ep(max)
t1TrumpetY2 = []    #Ep(min)
t1TrumpetY3 = []    #mean err
t1TrumpetY4 = []    #prog Rate

for p in obsWindow:
    m = (analysisT1end - analysisT1start) - p + 1
    t1err[p] = {'m': m} 
    EpMax = 0
    EpMin = 0
    for j in range(1, m + 1):
        errSum = 0
        for i in range(j, j + p):
            errSum += (1.0/p) * 100 * ( data[analysisT1start + i][colQi] - progRate ) / progRate
        if errSum > EpMax:
            EpMax = errSum
        if errSum < EpMin:
            EpMin = errSum
    t1err[p]['EpMax'] = EpMax
    t1err[p]['EpMin'] = EpMin
    t1TrumpetX.append(p)
    t1TrumpetY1.append(EpMax)
    t1TrumpetY2.append(EpMin)
    t1TrumpetY3.append(A)
    t1TrumpetY4.append(0)

tplot = PdfPages('trumpet curve.pdf')
p1 = plt.figure()
plt.plot(t1TrumpetX, t1TrumpetY1)
plt.plot(t1TrumpetX, t1TrumpetY2)
plt.plot(t1TrumpetX, t1TrumpetY3)
plt.plot(t1TrumpetX, t1TrumpetY4, '--')
plt.legend(('Ep(max)','Ep(min)', 'overall error', 'set rate'), fontsize='xx-small', loc='best')
plt.title(baseName + ': IEC60601-2-24 Trumpet Curve for Second Hour', fontsize='x-small')
plt.xlabel('Observation window (min)', fontsize='x-small')
plt.ylabel('Percentage error of flow', fontsize='x-small')
plt.ylim((-15, 15))
ax = plt.axes()
ax.set_xticks(obsWindow)
tplot.savefig(p1)
plt.close(p1)
tplot.close(p1)

这应该可以让你获得标准的小号曲线。没有要求在整体错误A上放置数字标签,但您可以使用注释执行此操作。