[英]How to create a regular time series in R, and how to interpretate unit time in CCF function in R
首先,一些背景。 該代碼的主要目標是在可變通量與其預測變量(sensor1,…,sensor)之間建立滯后關聯。 可變通量在過程的最后,因此每個傳感器的測量值必須在一定的時移(滯后)下與可變通量相關。 為了檢測這種滯后,計算了CCF和預白化,在可變磁通和傳感器1,磁通和傳感器2之間,等等。
可變通量和傳感器每5m采樣一次,但是由於某種原因,有時這種采樣不規則,因此時間序列不規則。
我的代碼有兩個主要問題。
** 1。 第一個問題:我必須將我的不規則數據幀轉換為每5m采樣的常規數據幀,這意味着重復最后一個值(我不想進行插值),例如,如果傳感器是每60m采樣一次,我想擴展該值12為了每5m測量一次。
我的數據大約是一年的測量值,因此我采樣了2天,然后計算了所有的計算結果。 當我采樣2天並用預增白計算CCF時,最大滯后並不總是相同,因此我重復了大約1000次此過程,然后我可以估計平均最大滯后及其分散。
這是我的數據示例(僅40個樣本)。
row.names fecha flujo sensor1 sensor2
2 38465 2012-07-10 03:45:00 0.9617 0.0000 0
3 38466 2012-07-10 03:50:00 0.9587 0.0000 0
4 38467 2012-07-10 03:55:00 0.9638 0.0000 0
5 38468 2012-07-10 04:00:00 0.9653 0.0000 0
6 38469 2012-07-10 04:05:00 0.9678 0.0000 0
7 38470 2012-07-10 04:10:00 1.0209 0.0000 0
8 38471 2012-07-10 04:15:00 0.9573 0.0000 0
9 38472 2012-07-10 04:20:00 0.9740 0.0000 0
10 38473 2012-07-10 04:25:00 0.9678 0.0000 0
11 38474 2012-07-10 04:30:00 0.9743 0.0000 0
12 38479 2012-07-10 04:55:00 0.9647 0.0000 0
13 38480 2012-07-10 05:00:00 1.0342 0.0000 0
14 38485 2012-07-10 05:25:00 1.1007 0.0000 0
15 38486 2012-07-10 05:30:00 1.0962 0.0000 0
16 38489 2012-07-10 05:45:00 1.0918 0.0000 0
17 38490 2012-07-10 05:50:00 1.0918 0.0000 0
18 38491 2012-07-10 05:55:00 1.1018 0.0000 0
19 38493 2012-07-10 06:05:00 0.8414 0.0000 0
20 38494 2012-07-10 06:10:00 1.0406 0.7862 0
21 38495 2012-07-10 06:15:00 1.0614 0.8123 0
22 38496 2012-07-10 06:20:00 1.0619 0.7966 0
23 38497 2012-07-10 06:25:00 1.0557 0.7713 0
24 38500 2012-07-10 06:40:00 0.8487 0.8541 0
25 38506 2012-07-10 07:10:00 0.8478 0.9849 0
26 38515 2012-07-10 07:55:00 1.0776 1.0056 0
27 38516 2012-07-10 08:00:00 1.0807 0.9735 0
28 38517 2012-07-10 08:05:00 1.0801 1.0088 0
29 38518 2012-07-10 08:10:00 1.0737 1.0576 0
30 38519 2012-07-10 08:15:00 1.01.
%函數可生成2個連續的連續天樣本
matriz = function(datos){
n <- nrow(datos)
dia.aleatorio <- sample(1:(n/2), 1, replace=F)
dia.aleatorio
dia1 <- datos$fecha[dia.aleatorio]
dia1
t1 <- dia1$mda
t1
dia2 <- dia1 + 86400
dia2 <- as.POSIXlt(as.character(dia2),format="%Y-%m-%d %H:%M:%S")
t2 <- dia2$mday
t1
t2
mes.aleatorio <- sample(1:12, 1, replace=F)
mes.aleatorio
ifelse(t1>9, p1<-t1, p1 <- paste("0",t1,sep="") )
ifelse(t2>9, p2<-t2, p2 <- paste("0",t2,sep="") )
ifelse(mes.aleatorio>9, m2<-mes.aleatorio, m2 <- paste("0",mes.aleatorio,sep="") )
fecha.elejida1 <- paste(m2,p1, sep ="-")
fecha.elejida1
fecha.elejida2 <- paste(m2,p2, sep ="-")
fecha.elejida2
molienda.dia1 <- subset(datos, format(datos$fecha,'%m-%d')== fecha.elejida1)
molienda.dia2 <- subset(datos, format(datos$fecha,'%m-%d')== fecha.elejida2)
molienda2dias <- rbind(molienda.dia1,molienda.dia2)
molienda2dias$fecha <- as.POSIXlt(molienda2dias$fecha,format="%Y-%m-%d %H:%M:%S")
return(molienda2dias)
}
#此功能不太好,但現在可以使用
generar.matriz <- function(matriz.datos) {
repeat {
matriz.generada <- matriz(matriz.datos)
numero.filas <- nrow(matriz.generada)
# exit if the condition is met
if (numero.filas > 290) break
}
return(matriz.generada)
}
Find_Max_CCF<- function(a,b){
d <- ccf(a, b, plot = FALSE)
cor = d$acf[,,1]
lag = d$lag[,,1]
cor.abs = abs(cor)
res = data.frame(cor,cor.abs,lag)
res_max = res[which.max(res$cor.abs),]
return(res_max)
}
gen.lag2 = function(datos,columna){
repeat {
molienda.dia <- generar.matriz(datos)
y <- xts(molienda.dia[2],molienda.dia$fecha)
x <- xts(molienda.dia[columna],molienda.dia$fecha)
m2a=auto.arima(x,lambda =TRUE, ic ="bic", stepwise=FALSE,approx=FALSE)
bestOrder <- c(m2a$arma[1],m2a$arma[5],m2a$arma[2])
m2b=arima(y,order=bestOrder,xreg=1:length(y),method="ML", optim.control = list(maxit = 1000))
maximo.lag <- Find_Max_CCF(residuals(m2b), residuals(m2a))
if (maximo.lag$lag < 0) break
}
return(maximo.lag$lag)
}
I only use negative lags because my proceses must be causal
有時預催乳的感覺會導致奇異矩陣,函數會停止,因此我使用trycatch創建了另一個函數來避免此問題,重復過程直到得到解決方案
lag.filter = function(datos,col,p,q){
repeat{
er <- tryCatch(desf <- gen.lag1(datos,col,p,q), error = function(e) "este es un error")
if (desf < 0 | class(er) == "numeric")
return(desf)
}
}
#函數為每個時間序列創建一個滯后矩陣
matriz.desfase = function(data,nrep)
# se debe ingresar la data normal
desfases <- numeric()
for (i 3:ncol(data)){
desfase[i-2] <- replicate(nrep, lag.filter2(data,i))
}
所有這些功能都可以使用,但是我的矩陣不是固定的時間序列,因此我必須找到從我的矩陣創建5m固定時間序列的方法。 如果我從ccf得出的滯后與我的時間序列步長之間沒有關系(在我的情況下為5分鍾),那么我得到的滯后也是無法解釋的
任何建議都將受到歡迎
提前致謝,
我找到了一種使用此功能將數據轉換為常規5m時間序列的方法
regular = function(x){
index <- molienda.dia$fecha
delta.5m <- as.POSIXct(index[1])+(0:576)*60*5
aux.5m <- xts(rep(NA,length(delta.5m)), order.by=delta.5m)
#x1 <-na.locf(merge(aux.5m, x)[,2])
x2 <- merge(aux.5m, na.locf(merge(aux.5m, x)[,2]), join="inner")[,2]
return(x2)
}
x <- regular(x)
y <- regular(y)
關於時間單位,我想以秒為單位,因為我的時間單位是
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.