回归时间序列
Regression Time Series
我有 6 个月的数据,从 2015 年 11 月到 2016 年 4 月(每个大约 3600 行)。我必须预测 5 月份的数据。我认为这是一个非平稳时间序列。我是 R 编程的新手。请帮忙。
数据是这样的。
Nov dec Jan Feb Mar Apr May
0 0 0 2 0 0 ?
1 1 0 0 0 1 ?
0 1 0 0 1 0 ?
5 2 1 0 1 3 ?
0 0 2 0 1 0 ?
4 0 2 0 1 1 ?
我不确定你的问题是什么。如何做出预测?或者如何测试(非)平稳时间序列?
要测试固定时间序列,请安装 tseries
包。通过在控制台中键入 install.packages("tseries")
并将以下命令添加到您的代码 library(tseries)
来执行此操作(此命令加载已安装的包)。然后您可以使用 adf.test
函数执行增强的 Dickey-Fuller 测试。有关如何使用此功能的更多信息,请在安装并加载 tseries
程序包后输入 ?adf.test
。
如何进行预测完全取决于您使用的模型。例如,如果您使用 ar
函数制作 AR 模型,则可以使用 predict.ar
函数进行预测。通常只使用 predict
就足够了,因为 R 可以识别对象(在我的示例中是一个 ar
对象),因此知道调用 predict.ar
.
希望对您有所帮助!
编辑:
这是一段简单的代码,向您展示我将如何对您的数据集进行预测
#This is not an efficient code, but intstructive and easy readable imo
#I will make an AR(p) model, where p is chosen by the ar() function
#note that for an AR model, we need a time series
#I make an AR model for each row, since each row reperesents the time series of 1 subject in your data
#load data
myData <- read.csv(FILENAME)
May <- NULL #use this to store the predictions for May
for (i in 1:nrow(myData))
{
timeSeries <- t(myData[i,]) #for an ar model, we need a time series, so the rows in myData
arModel <- ar(timeSeries) #build the model
arPrediction <- predict(arModel) #make the prediction
May <- rbind(May, arPrediction) #add the prediction to other predictions
}
data <- round(runif(10, min = 0, max = 10))
arModel <- ar(data)
arPrediction <- predict(arModel)
我有 6 个月的数据,从 2015 年 11 月到 2016 年 4 月(每个大约 3600 行)。我必须预测 5 月份的数据。我认为这是一个非平稳时间序列。我是 R 编程的新手。请帮忙。
数据是这样的。
Nov dec Jan Feb Mar Apr May
0 0 0 2 0 0 ?
1 1 0 0 0 1 ?
0 1 0 0 1 0 ?
5 2 1 0 1 3 ?
0 0 2 0 1 0 ?
4 0 2 0 1 1 ?
我不确定你的问题是什么。如何做出预测?或者如何测试(非)平稳时间序列?
要测试固定时间序列,请安装 tseries
包。通过在控制台中键入 install.packages("tseries")
并将以下命令添加到您的代码 library(tseries)
来执行此操作(此命令加载已安装的包)。然后您可以使用 adf.test
函数执行增强的 Dickey-Fuller 测试。有关如何使用此功能的更多信息,请在安装并加载 tseries
程序包后输入 ?adf.test
。
如何进行预测完全取决于您使用的模型。例如,如果您使用 ar
函数制作 AR 模型,则可以使用 predict.ar
函数进行预测。通常只使用 predict
就足够了,因为 R 可以识别对象(在我的示例中是一个 ar
对象),因此知道调用 predict.ar
.
希望对您有所帮助!
编辑: 这是一段简单的代码,向您展示我将如何对您的数据集进行预测
#This is not an efficient code, but intstructive and easy readable imo
#I will make an AR(p) model, where p is chosen by the ar() function
#note that for an AR model, we need a time series
#I make an AR model for each row, since each row reperesents the time series of 1 subject in your data
#load data
myData <- read.csv(FILENAME)
May <- NULL #use this to store the predictions for May
for (i in 1:nrow(myData))
{
timeSeries <- t(myData[i,]) #for an ar model, we need a time series, so the rows in myData
arModel <- ar(timeSeries) #build the model
arPrediction <- predict(arModel) #make the prediction
May <- rbind(May, arPrediction) #add the prediction to other predictions
}
data <- round(runif(10, min = 0, max = 10))
arModel <- ar(data)
arPrediction <- predict(arModel)