繁体   English   中英

如何使用PCA对高度相关变量进行时间序列预测?

[英]How to use PCA for time-series predictions on highly correlated variables?

例如,我有一个名为Economic_Trends的数据集,它包含5列,具有10年的数据。 例如

Economic_Trend <- matrix(c(1:10, 2:11, rnorm(30)), byrow = FALSE, nrow = 10, ncol = 5)

然后这是我运行的代码:

mu <- colMeans(Economic_Trend)

Econ_pca <- prcomp(Economic_Trend)

PCA1 <- Econ_pca$x[,1]
PCA2 <- Econ_pca$x[,2]


plot(Econ_pca$x[,1], Econ_pca$x[,2])
plot(lm(PCA1 ~ PCA2))

我看到我现在有两个主要组成部分,但是我不确定在哪里找到订单,因为如果这些年弄乱了怎么办? 我想做预测,以便例如找到未来的十年。 如何使我的PC适应回归模型,找到时间序列的下一步,然后重建原始数据?

谢谢!

重要的是将PC保存为时间序列(ts)对象,以保持其顺序。 这里有一些代码可以帮助解决这些问题并绘制PC:

require(xts)  ## Makes autoplot work
require(ggfortify)  ## Allows multiple PCs to be plotted in one graph
## Maybe some others

## Given code:
Economic_Trend <- matrix(c(1:10, 2:11, rnorm(30)), byrow = FALSE, nrow = 10, 
ncol = 5)
mu <- colMeans(Economic_Trend)

## Tweaked code: 
Econ_pca <- prcomp(Economic_Trend[,3:5]) ## First two columns are order 
## number
PCA1 <- ts(Econ_pca$x[,1])  ## Save first PC as a time series object
PCA2 <- ts(Econ_pca$x[,2])  ## Save second PC as a time series object
p <- autoplot(ts(cbind(PCA1,PCA2)), facets = FALSE) +  
     ## Create ggplot object plotting first 2 PCs
     ggtitle("First two PCs") + ylab("Econ ($?)") + ## Add plot and y-axis labels 
     theme_bw() +  ## Remove grayscale background grid
     theme(axis.text=element_text(size=rel(1)),legend.text = element_text(size = 12))
p  ## Display plot

我看不到您的数据。 但是,如果名称中有“趋势”,则该系列可能具有趋势,并且您希望对其进行预测。 最好应用为时间序列设计的PCA特殊版本。 这是奇异频谱分析(SSA)。

library(Rssa)
library(lattice)
# Decompose 'EuStockMarkets' series with default parameters
ss <- ssa(EuStockMarkets, kind = "mssa")
rec <- reconstruct(ss, groups = list(Trend = 1:2))

foreca <- rforecast(ss, groups = list(Trend = 1:2), 
                    len = 200, only.new = TRUE)
data <- cbind(EuStockMarkets, rec$Trend, foreca)
xyplot(data, type = "l", superpose = TRUE,
       auto.key = list(columns = 3),
       col = c("blue", "darkgreen", "red", "violet"),
       lty = c(rep(2, 4), rep(1, 4), rep(3, 4)))

在此处输入图片说明

暂无
暂无

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

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