0
votes

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.

1
I do not know this package but (just guessing here!) it looks that the function did not received the proper argument, are you sure yp is a matrix?SabDeM
No. yp is the vector of observations from 1710 to 1980. However, the package timeSeries with the function cor(.) says that "x" must be " an univariate object of class timeSeries". I converted my vector yp in a time series with as.ts(yp).Mercier
Ok get it. Could you please provide a little piece of your data with dput?SabDeM
I'm wondering if your terminology is confused. There is no such statistical animal as a correlation of a single vector. Correlation is a a relation of two items. There is such a thing as the auto-correlation function applied to a single vector. Asking for all pairwise something (differences?) might suggest you instead want a distance matrix.IRTFM
Here is a link that shows what is yp: pastebin.com/XxVL3zNnMercier

1 Answers

1
votes

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.