0

I’m working on an analysis using Vector autoregression (VAR) model. I’m working with data that has the date format in yyyy-mm-dd hh:mm:ss. But the examples I find online are mostly YYYY-MM format.

Example:

y1<- ts(y$y1, start= c(2020, 5), frequency = 12)

Additionally, the data I’m trying to use is from twitter and the information is not consistent in the timeframe. Also, I did check to make sure I have no duplicated rows. How would I perform a VAR analysis on the data in the following format? enter image description here

Bonnie
  • 23
  • 4
  • provide a sample data using `dput` refer https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Nad Pat Sep 12 '21 at 06:25
  • Use the fable package instead of forecast package. Forecast is on maintenance and fable is the new version. See [chapter 12.3](https://otexts.com/fpp3/VAR.html) of Forecasting: Principles and Practice for an example. – phiver Sep 12 '21 at 08:06

1 Answers1

0

This is to create a tsibble needed for fable to do VAR with a resolution of seconds and only one row per time point:

library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(fable)
#> Loading required package: fabletools

tribble(
  ~Datetime, ~A, ~B, ~C,
  "2014-02-27 17:28:11", 626, 0,0,
  "2014-02-27 17:28:11", 626, 0,0,
  "2014-02-19 14:16:20", 0,0,1
) %>%
  mutate(Datetime = parse_datetime(Datetime, format = "%Y-%m-%d %H:%M:%z")) %>%
  distinct(Datetime, .keep_all = TRUE) %>%
  as_tsibble(index = Datetime)
#> # A tsibble: 2 x 4 [12h 12m] <UTC>
#>   Datetime                A     B     C
#>   <dttm>              <dbl> <dbl> <dbl>
#> 1 2014-02-18 18:16:00     0     0     1
#> 2 2014-02-27 06:28:00   626     0     0

Created on 2021-09-13 by the reprex package (v2.0.1)

danlooo
  • 10,067
  • 2
  • 8
  • 22