Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Assume that we are in the time series data setting, where we have data at equally-spaced times
If we were writing out the full correlation matrix for
(Side note: This is an example of a correlation matrix which has Toeplitz structure.)
Given
ar1_cor <- function(n, rho) { exponent <- abs(matrix(1:n - 1, nrow = n, ncol = n, byrow = TRUE) - (1:n - 1)) rho^exponent }
In the function above, n
is the number of rows in the desired correlation matrix (which is the same as the number of columns), and rho
is the
Here is an example of how the function can be used:
ar1_cor(4, 0.9) # [,1] [,2] [,3] [,4] # [1,] 1.000 0.90 0.81 0.729 # [2,] 0.900 1.00 0.90 0.810 # [3,] 0.810 0.90 1.00 0.900 # [4,] 0.729 0.81 0.90 1.000
Such a function might be useful when trying to generate data that has such a correlation structure. For example, it could be passed as the Sigma
parameter for MASS::mvrnorm()
, which generates samples from a multivariate normal distribution.
Can you think of other ways to generate this matrix?
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.