Reputation: 587
I have the following equation for calculating ACF, I don`t want to use the function ACF in r, but calculate it manually for the following data and then plot the results using ggplot2
data<-rweibull(168,2,3)
time<-seq(from=as.POSIXct("2014-01-01 00:00"),to=as.POSIXct("2014-01-07 23:00"),by="hour")
df<-data.frame(time,data)
where k is the time lag and μ is the mean of time series ( , Yt=1,2,….N). The ACF is calculated here for time series considering up to time lag of 100 hours. In my final output I want to have an vector with 100 values. In the equation I don`t know how to set the Yt+k part of the equation in R for 100 time lags. Any help would be appreciated.
For now I have the code but I don`t know how to set the code to get 100 Lag results in one vector.
x <- data - mean(data)
sum(x[1:99] * x[2:100]) / sum(x^2)# TIME LAG OF 100
Upvotes: 2
Views: 2978
Reputation: 986
You can try this if you want ...
data<-rweibull(168,2,3)
time<-seq(from=as.POSIXct("2014-01-01 00:00"),to=as.POSIXct("2014-01-07 23:00"),by="hour")
df<-data.frame(time,data)
x <- data - mean(data)
maxLag<-100
acfResults<- data.frame(k=c(1:maxLag), acf=NA, stringsAsFactors = FALSE)
for (k in 1: maxLag) {
N <-nrow(df)-k
acf<- sum(x[1:N]*x[(1+k):(N+k)]) / sum(x^2)
acfResults[k,1] <-k
acfResults[k,2] <-acf
}
Upvotes: 3