简体   繁体   English

如何使用这些数据在 R 中制作每小时时间序列?

[英]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.所以我需要使用 R 中的 ts() 函数将此帧转换为时间序列。 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.我不确定参数,我不知道频率是否应该设置为 24(一天中的小时)或 10(11:00 到 21:00),如上所示。 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.频率是样本之间时间的倒数,即每分钟 1/2 个样本或每小时 30 个样本。 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 .时间序列模型只能在时间序列静止时在 R 中创建。 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.使用不同的采样率会使时间序列变得不稳定,因此您将无法在 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. Analytics Vidhya 上的这个页面提供了平稳和非平稳时间序列的一个很好的定义,而R 博客上的这个页面提供了一些与分析非平稳时间序列相关的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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