2017-12-18 116 views
2

我有数据:错误 - 长度不同当试图整合(但长度相同)

Date     Value 
17/12/17 8:39:45 1144.5783 
17/12/17 8:40:02 1646.5863 
17/12/17 8:40:15 1104.4177 
17/12/17 8:40:30 1244.9799 
17/12/17 8:40:45 1084.3373 
17/12/17 8:41:00 1285.1406 
17/12/17 8:41:15 1144.5783 
17/12/17 8:41:30 1124498 
17/12/17 8:41:45 1265.0602 
17/12/17 8:42:00 1124498 
17/12/17 8:42:15 1144.5783 
17/12/17 8:42:30 1164.6586 
17/12/17 8:42:45 1084.3373 
17/12/17 8:43:00 1184739 
17/12/17 8:43:15 1064257 
17/12/17 8:43:30 1164.6586 
17/12/17 8:43:45 1184739 
17/12/17 8:44:00 1244.9799 

我想要的日期期间来计算积分。 我的真实数据由3124行组成。

library(lubridate) 
library(MESS) 

thedata <- read.csv('data.csv') 

datetime <- dmy_hms(as.character(thedata$Date)) 
time_length_data <- time_length(interval(datetime[1] , datetime[18]), "second") 
# data is gathered by almost every 15 sec 
divide_data <- 1:(time_length_data/15) 
# I am ommiting a few rows in order to have the same length as "sec" matrix below. 
divide_data <- divide_data[1:18] 

# this contains the values and has length 18 
sec <- as.numeric(as.matrix(thedata[2])) 

res <- auc(divide_data, sec, from = min(divide_data), to = max(divide_data), type = 'spline', absolutearea = TRUE) 

当我尝试执行资源,它给了我:

Error in xy.coords(x, y, setLab= FALSE): 'x' and 'y' lengths differ 

但长度相同。

+0

您能否根据小的子样本更新您的问题?忘记你的真实数据3124行。 –

+1

你的'auc'函数从哪里来? – kath

+0

我假设你的'auc'来自'MESS',但你应该在你的例子中包含它。 – Eumenedies

回答

2

我相信这是MESS程序包中的一个错误,absolutearea标志设置为TRUE

如果你看一下代码为auc:这里出现

if (absolutearea) 
    myfunction <- function(x) { abs(splinefun(x, y, method="natural")) } 
else 
    myfunction <- splinefun(x, y, method="natural") 

res <- integrate(myfunction, lower=from, upper=to)$value 

两个问题,x指定了两次,splinefun返回一个函数,而不是一个值。

如果absolutearea是假,则myfunction是训练有素的auc参数xy样条函数。

如果absolutearea为真,则myfunction是样条函数的绝对值(注意函数,而不是返回值)的培训上的匿名函数的参数xauc功能的y说法。

integrate函数将一系列值传递给myfunction。如果absolutearea为false,则myfunction将返回每个值处的样条曲线值。如果absolutearea为真,则myfunction返回函数的无意义绝对值。它不会抛出错误(非数字参数...),因为x(由integrate传递给匿名函数的值)与y(传递给auc函数的y值)的长度不同会导致第一次错误。

+0

完美答案和问题解释。用户(您?)提交了修复程序,新版本应该启动并运行。谢谢! – ekstroem