gpt4 book ai didi

r - 将单变量时间序列转化为 R 中的训练和测试拆分

转载 作者:行者123 更新时间:2023-12-03 16:42:36 25 4
gpt4 key购买 nike

我想将单变量时间序列(14139 个观察值)分成分别占 60% 和 40% 的训练和测试集。我输入命令
splits (APILts, c(rep("train", 8483), "test"))然后 R 结果:
Error: is.timeSeries(x) is not TRUE

最佳答案

要拆分时间序列,您需要一个作为时间序列的向量。

该错误表明您的 APILts 不是 ts 对象:

Error: is.timeSeries(x) is not TRUE

这是一个关于如何使用 ts 对象分割时间序列的示例:
data(AirPassengers)

数据被分配给一个方便的向量

这是避免每次更改代码的简单方法
series <- AirPassengers

绘制系列
plot(series, col="darkblue", ylab="Passegners on airplanes")

绘制系列的季节性分布
windows(width=800,height=350)   # set the window with the dimensions you need

boxplot(split(series, cycle(series)), names = month.abb, col = "gold")

测试集的大小通常约为总样本的 40%

所以,我们将把系列分成训练集和测试集
# Training set
# Use data from 1949 to 1955 for forecasting

sr = window(series, start=1949, end=c(1955,12))

# Test set
# Use remaining data from 1956 to 1960 to test accuracy

ser = window(series, start=1956, end=c(1960,12))

现在我们准备开始了。

将数据转换为时间序列
# Data 

dat <- c(27, 28, 25, 22, 19, 21, 24, 24, 22, 16, 27, 41, 29, 24, 15, 27, 25, 21, 15, 41, 19, 24, 34, 20, 25, 34, 31, 29, 38, 36, 27, 37, 31, 28, 25, 34, 40, 36, 39, 19, 40, 31, 29, 39, 29, 40, 34, 31)


# Convert the data to time series

series <- ts(dat, frequency = 12, start = c(1969, 1))

# Inspect the series

series

plot(series)

请注意,有时,如果您已将数据作为数据框上传,则必须精确指定列:
# In this example, the data you want to convert into a ts object are in the first column

series <- ts(dat[[1]], frequency = 12, start = c(1969, 1))

关于r - 将单变量时间序列转化为 R 中的训练和测试拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38114601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com