简体   繁体   English

R中的时间序列预测,单变量时间序列

[英]Time series forecasting in R, univariate time series

I am currently working on a project for school that requires me to perform time series forecasting in R on a given set of data. 我目前正在为一个学校项目工作,该项目要求我对R中的给定数据集进行时间序列预测。 I have looked up countless examples on how to do this, but every example I find contains a dataset that records data, for example, once a month over the course of 15 years. 我查找了无数示例来了解如何执行此操作,但是我发现的每个示例都包含一个记录数据的数据集,例如,在15年的时间里每月记录一次。 The dataset given to me by my professor has recorded data for every .001 seconds, and there are multiple data entries for the same second. 我的教授给我的数据集每0.001秒记录一次数据,并且同一秒有多个数据条目。 For example, at the end of the data there are five different entries for .02500 seconds. 例如,数据末尾有五个不同的条目,持续时间为.02500秒。

My understanding of a univariate time series is a time series that takes measurements at a specific period of time, like every month or every thousandth of a second. 我对单变量时间序列的理解是在特定时间段(例如每月或千分之一秒)进行测量的时间序列。 Whenever I try to do time series forecasting on the dataset ( adeno ), I get the error shown below under the code. 每当我尝试对数据集( adeno )进行时间序列预测时,都会在代码下显示以下错误。

> fit <- auto.arima(adeno)
Error in auto.arima(adeno) : 
  auto.arima can only handle univariate time series

Can anyone tell me where I'm going wrong or if I'm misunderstanding something? 谁能告诉我我要去哪里错了,或者我误会了什么? I've tried trying to convert the dataset into a time series by using the ts() command in R but I must be doing something wrong because even after that it says it's not aunivariate time series. 我尝试通过在R中使用ts()命令尝试将数据集转换为时间序列,但我必须做错了什么,因为即使在此之后,它也表示不是单变量时间序列。

The error means what it says, auto.arima can only handle univariate time series. 该错误表示其含义, auto.arima仅能处理单变量时间序列。 Since you mentioned that your dataset has multiple entries for each time unit, it is a multivariate time series if you convert it using ts . 由于您提到数据集的每个时间单位都有多个条目,因此,如果使用ts转换,则它是一个多元时间序列。 You can do something along the lines of: 您可以按照以下方式进行操作:

adenoTS = ts(adeno)
arima_fit = auto.arima(adenoTS[,1])

To address your second question in the comment section, I used the airquality dataset for demonstration: 为了在评论部分解决您的第二个问题,我使用airquality数据集进行了演示:

library(forecast)

# Convert as time series
airTS = ts(airquality)

# Plot multivariate ts
plot(airTS[,1:4])

# Run auto.arima on a single ts
arima_fit = auto.arima(airTS[,3])

# Forecast for the next 10 time units
arima_forecast = forecast(arima_fit, h = 10)

# Plot forecasts
plot(arima_forecast)

forecast() from the forecast package allows you to forecast for the next h time units. forecast()forecast包可以让你在未来的预测h的时间单位。 What "time units" mean in this case depends on how you defined your time series in the airTS = ts(airquality) step. 在这种情况下,“时间单位”的含义取决于您如何在airTS = ts(airquality)步骤中定义时间序列。 Here I didn't bother converting it properly, but you can add the start = and frequency = arguments in ts() to specify the start time and frequency of your ts. 在这里,我没有进行适当的转换,但是您可以在ts()添加start =frequency =参数来指定ts()的开始时间和频率。

The plot method for forecast lets you plot your forecast results. forecastplot方法可让您绘制预测结果。 See ?plot.forecast . 参见?plot.forecast

在此处输入图片说明

Although my dataset has not have multiple entries for each time unit, it was purely univariate, the function didn't work. 尽管我的数据集中每个时间单位都没有多个条目,但它纯粹是单变量的,该功能无法正常工作。

Adding [,1] in the auto.arima argument helped to solve the problem:: autoarima1 <- auto.arima(TR_2015_2019_ts [,1]) 在auto.arima参数中添加[,1]有助于解决问题:autoarima1 <-auto.arima(TR_2015_2019_ts [,1])

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

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