简体   繁体   English

用时间序列估计R中的相关矩阵?

[英]Estimating correlation matrix in R with time series?

I have a time series of the number of sunspots from 1710 to 1980 base on a dataset from R. 我有一个基于R的数据集从1710到1980的黑子数的时间序列。

I am trying to estimate the matrix of correlation of the "y" values. 我正在尝试估计“ y”值的相关矩阵。 I tried to use the cor(.) function from the timeSeries package( https://cran.r-project.org/web/packages/timeSeries/timeSeries.pdf )(page 64). 我试图使用timeSeries包中的cor(。)函数( https://cran.r-project.org/web/packages/timeSeries/timeSeries.pdf )(第64页)。 But it doesn't work. 但这是行不通的。

Let "yp" be my vector of observations from 1710 to 1980(time series object). 假设“ yp”是我从1710年到1980年(时间序列对象)的观测向量。 My code is: 我的代码是:

CorrelationMatrice=cor(yp,y=NULL,use = "all.obs", method = c("pearson"))

Thank you for reading this post. 感谢您阅读这篇文章。

The error is of the following form: 该错误具有以下形式:

Error in cor(yp, y = NULL, use = "all.obs", method = c("pearson")) : 
  give 'x' and 'y' or 'x' as a matrix

I would like that my correlation matrix gives the correlation between each pair of observations ci and cj from yp. 我希望我的相关矩阵给出来自yp的每对观测值ci和cj之间的相关性。

The reason that you are receiving an error is that the time series example you provided (yp) is a vector. 收到错误的原因是,您提供的时间序列示例(yp)是矢量。 If you look closely at the documentation for the timeSeries package, you will see that the sample time series used are matrixes and not vectors, which is why you are receiving the following error message 如果仔细查看timeSeries软件包的文档,您将看到使用的示例时间序列是矩阵而不是向量,这就是为什么您收到以下错误消息的原因

Error in cor(yp, y = NULL, use = "all.obs", method = c("pearson")) : give 'x' and 'y' or 'x' as a matrix

If you use a matrix for yp rather than a vector, you will not get an error. 如果将矩阵用于yp而不是向量,则不会出现错误。 Example: 例:

my_ts <- <- as.data.frame(timeSeries(matrix(rnorm(24), 12), timeCalendar()))

cor(my_ts, y = NULL, use = "all.obs", method = c("pearson"))

           TS.1       TS.2
TS.1 1.00000000 0.02275777
TS.2 0.02275777 1.00000000

You could attempt to use you vector for both X and Y cor(yp, y = yp, use = "all.obs", method = c("pearson")) and get a correlation matrix that was 271x271, but there wouldn't be much point as you would receive a correlation of 1. 您可以尝试对X和Y cor(yp, y = yp, use = "all.obs", method = c("pearson"))使用向量cor(yp, y = yp, use = "all.obs", method = c("pearson"))并得到一个271x271的相关矩阵,但是不会一点,因为您将获得1的相关性。

cor(yp, y = yp, use = "all.obs", method = c("pearson"))
[1] 1

In order you generate the correlation matrix that you are looking for you need to compare two different time series rather than comparing one-time series to itself. 为了生成所需的相关矩阵,您需要比较两个不同的时间序列,而不是将一个时间序列与其自身进行比较。 For example, you could compare the Dow Jones Industrial Average to the Euro/Dollar exchange rate over a certain period of time. 例如,您可以将一段时间内的道琼斯工业平均水平与欧元/美元汇率进行比较。

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

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