错误 - 尝试积分时长度不同(但长度相同)

error - lengths differ when trying to integrate (but lengths are the same)

我有数据:

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)

当我尝试执行 res 时,它给我:

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

但是长度是一样的

我认为这是 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 returns 是一个函数,而不是一个值。

如果 absolutearea 为假,则 myfunction 是在 auc 参数 xy.

上训练的样条函数

如果 absolutearea 为真,则 myfunction 是在匿名函数的 x 参数和 auc 函数的 y 参数。

integrate 函数正在将一系列值传递给 myfunction。如果 absolutearea 为假,则 myfunction returns 每个值处的样条值。如果 absolutearea 为真,则 myfunction returns 函数的无意义绝对值。它永远不会抛出该错误(非数字参数...),因为它第一次出错时 xintegrate 传递给匿名函数的值)与 [=21= 的长度不同](传递给 auc 函数的 y 值)。