Reputation: 101
I have a time series of the number of sunspots from 1710 to 1980 base on a dataset from R.
I am trying to estimate the matrix of correlation of the "y" values. I tried to use the cor(.) function from the timeSeries package( https://cran.r-project.org/web/packages/timeSeries/timeSeries.pdf )(page 64). But it doesn't work.
Let "yp" be my vector of observations from 1710 to 1980(time series object). 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.
Upvotes: 0
Views: 4994
Reputation: 4349
The reason that you are receiving an error is that the time series example you provided (yp) is a vector. 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
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. 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.
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.
Upvotes: 1