title: Defining a function that calculates the covariance-matrix of a correlation-matrix
body: <p>I have some problems with the transformation of a matrix and the names of the rows and columns.</p>
  
  <p>My problem is as follows:</p>
  
  <p>As input-matrix I have a (symmetric) <strong>correlation matrix</strong> like this one:</p>
  
  <p><img src="https://i.stack.imgur.com/XTmFf.png" alt="enter image description here"></p>
  
  <p>The <strong>correlation-vector</strong> is given by the values of the lower triangular matrix:</p>
  
  <p><img src="https://i.stack.imgur.com/YK7vF.png" alt="enter image description here"></p>
  
  <p>Now, I want to compute the variance-covariance-matrix of the these correlations, which are approximately normally distributed with the <strong>variance-covariance-matrix</strong>:</p>
  
  <p><img src="https://i.stack.imgur.com/1bnbZ.png" alt="enter image description here"></p>
  
  <p>The <strong>variances</strong> can be approximated by</p>
  
  <p><img src="https://i.stack.imgur.com/bHeNm.png" alt="enter image description here"></p>
  
  <p>-> N is the sample size (in this example N = 66)</p>
  
  <p>The <strong>covariances</strong> can be approximated by</p>
  
  <p><img src="https://i.stack.imgur.com/L4P44.png" alt="enter image description here"></p>
  
  <p>For example the <strong>covariance between r_02 and r_13</strong> is given by</p>
  
  <p><img src="https://i.stack.imgur.com/Dersg.png" alt="enter image description here"></p>
  
  <p>Now, I want to define a function in R which gets the correlation matrix as input and returns the variance-covariance matrix. However, I have problems to implement the calculation of the covariances. My idea is to give names to the elements of the correlation_vector as shown above (r_01, r_02...). Then I want to create the empty variance-cocariance matrix, which has the length of the correlation_vector. The rows and the columns should have the same names as the correlation_vector, so I can call them for example by [01][03]. Then I want to implement a for-loop which sets the value of i and j as well as k and l as shown in the formula for the covariance to the columns and rows of the correlations that I need as input for the covariance-formula. These must always be six different values (ij; ik; il; jk; jl; lk). This is my idea, but I don't now how to implement this in R.</p>
  
  <p>This is my code (without the calculation of the covariances):</p>
  
  <pre><code>require(corpcor)
  
  correlation_matrix_input &lt;- matrix(data=c(1.00,0.561,0.393,0.561,0.561,1.00,0.286,0.549,0.393,0.286,1.00,0.286,0.561,0.549,0.286,1.00),ncol=4,byrow=T)
  
  N &lt;- 66 # Sample Size
  
  vector_of_correlations &lt;- sm2vec(correlation_matrix_input, diag=F) # lower triangular matrix of correlation_matrix_input
  
  variance_covariance_matrix &lt;- matrix(nrow = length(vector_of_correlations), ncol = length(vector_of_correlations)) # creates the empty variance-covariance matrix
  
  
  # function to fill the matrix by calculating the variance and the covariances
  
  variances_covariances &lt;- function(vector_of_correlations_input, sample_size) {
  
      for (i in (seq(along = vector_of_correlations_input))) {
          for (j in (seq(along = vector_of_correlations_input))) {
  
              # calculate the variances for the diagonale
              if (i == j) {
                  variance_covariance_matrix[i,j] = ((1-vector_of_correlations_input[i]**2)**2)/sample_size 
              }
  
              # calculate the covariances
              if (i != j) {
  
                  variance_covariance_matrix[i,j] = ???
  
              }
          }
      }
  
  return(variance_covariance_matrix); 
  }
  </code></pre>
  
  <p>Does anyone have an idea, how to implement the calculation of the covariances using the formula shown above?</p>
  
  <p>I would be grateful for any kind of help regarding this problem!!!</p>