Search code examples
rtime-series

Dataframe as time series with time (hours)


The task is to check whether there is cointegration between time series

   DT                  Os.ilosc   BG.ilosc
 1 2023-01-01 00:00:00       45           799.
 2 2023-01-01 01:00:00       45          1100.
 3 2023-01-01 02:00:00       45          1131.
 4 2023-01-01 03:00:00       45          1077.
 5 2023-01-01 04:00:00       45.8        1094.
 6 2023-01-01 05:00:00       45          1158.
 7 2023-01-01 06:00:00       44.8        1060 
 8 2023-01-01 07:00:00       45.2         984.
 9 2023-01-01 08:00:00       45.2         965.
10 2023-01-01 09:00:00       45.8         939.

The process is fast, so data is saved every hour. I am using adf.test from the tseries package. Do I need to transform my dataframe into a time series? I usually come across daily or monthly, quarterly, yearly time series: How to convert dataframe into time series?

How to transform a dataframe containing hours?


Solution

  • The adf.test() checks for stationarity in a time series. Its a prerequisite for cointegratiuon analyses.

    However, to check for cointegration between two time series, we often use the Johansen Test, as the ADF test is used on single time series to check for stationarity, not for cointegration directly.

    You can check for cointegration using the ca.jo() function from the "urca" package, which performs the Johansen Test:

    # Install and load necessary package
    if(!require(urca)) { install.packages("urca") }
    library(urca)
    #assuming these are the different time series you're interested in
    time_series <- df[,c("Os.ilosc", "BG.ilosc")]
    
    # Run the Johansen Test
    jotest <- ca.jo(time_series, type="trace", ecdet="none", K=2)
    summary(jotest)
    

    Its unclear which time series you are referring to though. Do you have that data for another time series? Or are you just trying to check this time series for stationarity which is why you need the adf.test?

    If thats the case, try making a time series object and then runing the adf.test

    df$DT <- as.POSIXct(df$DT)
    
    adf.test(df$Os.ilosc)
    
    adf.test(df$BG.ilosc)