简体   繁体   中英

How to make an hourly time series in R with this data?

times         booked_res
11:00         23
13:00         26
15:00         27
17:00         25
19:00         28
21:00         30

So I need to use the ts() function in R to convert this frame into a time series. The column on the right are the number of people reserved in each time. How should I approach this? I'm not sure about the arguments and I don't know if the frequency should be set to 24 (hours in a day) or 10 (11:00 to 21:00) as shown above. Any help appreciated.

First, find the frequency by noting that you are taking a sample every two minutes. The frequency is the inverse of the time between samples, which is 1/2 samples per minute or 30 samples per hour. The data you're interested in is on the right, so you can just use that data vector rather than the entire data frame. The code to convert that into a time series is simply:

 booked_res <- c(23,26,27,25,28,30)
 ts(booked_res,frequency = 30)

A simple plot with your data might be coded like this:

plot(ts(booked_res,frequency = 30),ylab='Number of people reserved',xlab='Time (in hours) since start of sampling',main='Time series chart of people reservations')

UPDATE:

A time series model can only be created in R when the times series is stationary . Using a varying sample rate would make the time series non-stationary , and so you wouldn't be able to create a time-series object in R.

This page on Analytics Vidhya provides a nice definition of stationary and non-stationary time series, while this page on R bloggers gives some resources that relate to analyzing a non-stationary time series.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM