Home > Mobile >  Create Time series object from a tibble in R to generate a ggseasonplot
Create Time series object from a tibble in R to generate a ggseasonplot

Time:09-29

I am trying to create a timeseries ts object in R from daily time series data for a single year. The data is a tibble and looks like this:

       Year  Month Day   Simple_Assault
       <chr> <chr> <chr>          <int>
     1 2020  01    01                 6
     2 2020  01    02                 6
     3 2020  01    05                 4
     4 2020  01    06                 4
     5 2020  01    07                 1
     6 2020  01    08                 5
     7 2020  01    09                 4
     8 2020  01    10                 2
     9 2020  01    11                 5
    10 2020  01    12                 2
# ... with 102 more rows

I get an error while creating the ts object. The idea is to create a time series object to generate a ggseasonplot similar to the one shown below:

enter image description here

Code

    library(tidyverse)
    
    # Create a ts object from a tibble
    data_ts = ts(data, start = 2020, end = 2020, frequency = 8) 
    Error in attr(data, "tsp") <- c(start, end, frequency) : 
      invalid time series parameters specified

    # Getting an error possibly due to incorrect argument use

CodePudding user response:

I made two mistakes first I gave a wrong value to frequency, since I have a daily time series, I should have used 12 as frequency. Secondly, the start argument had to be modified too.

Code

# Create time series 
data_ts = ts(crime_2701,start = c(2020,1), frequency =12)

# Check
plot(data_ts)

# Create a season plot
ggseasonplot(data_ts[,4], polar=TRUE) 

enter image description here

  • Related