rsymmetric

Creating a symmetric matrix in R


I have a matrix in R that is supposed to be symmetric, however, due to machine precision the matrix is never symmetric (the values differ by around 10^-16). Since I know the matrix is symmetric I have been doing this so far to get around the problem:

s.diag = diag(s)
s[lower.tri(s,diag=T)] = 0
s = s + t(s) + diag(s.diag,S)

Is there a better one line command for this?


Solution

  • Is the workaround really necessary if the values only differ by that much?

    Someone pointed out that my previous answer was wrong. I like some of the other ones better, but since I can't delete this one (accepted by a user who left), here's yet another solution using the micEcon package:

    symMatrix(s[upper.tri(s, TRUE)], nrow=nrow(s), byrow=TRUE)