r - 时间序列图

标签 r statistics time-series

以下 R 代码生成一个时间序列图,而接下来的代码块生成 8 个图。

births <- scan("http://robjhyndman.com/tsdldata/data/nybirths.dat")

birthstimeseries <- ts(births, frequency=12, start=c(1946,1))
birthstimeseries

plot.ts(birthstimeseries)

第二 block 代码:

library(Matrix)

i <- c(1, 3, 8, 4, 2, 7, 6, 9, 1, 4, 10, 5, 11, 2, 12)
j <- c(2, 5, 3, 8, 6, 2, 4, 2, 4, 5, 2, 7, 3, 2, 1)
x <- rpois(15, 5)
M4 <- sparseMatrix(i, j, x = x)
M4_regular_matrix <- as(M4, "matrix")

timeseries <- ts(M4_regular_matrix)
print(timeseries)

plot.ts(timeseries)

第一个图将 ts 对象视为单个时间序列,而第二 block 代码则考虑 8 个不同的时间序列。我想将第二个数据集转换为单个时间序列并仅生成一个图表。我怎样才能做到这一点?

最佳答案

将矩阵转换为向量,并指定频率加上开始日期:

timeseries <- ts(as.vector(M4_regular_matrix), frequency = 12, start = c(2023, 1))

plot.ts(timeseries)

enter image description here

关于r - 时间序列图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76842104/

相关文章:

r - 如何告诉 lapply 忽略错误并处理列表中的下一个内容?

r - 将 dplyr 中的 get(paste0()) 对象解析为 Shiny 应用程序

algorithm - 销售排名算法

python - 如何训练具有多个系列的 statsmodels.tsa.ARIMA 模型

matlab - 如何找到两条曲线之间的相似度和相似度得分?

r - 对数据帧中特定值的总和进行高效重采样

r - 如何在 R 中对每行使用不同的字符位置对列进行子串

java - (Java)统计分析作业

algorithm - 预测篮球中罚球命中率的概率?

time-series - 执行 "online"线性插值