# User-defined correlation matrix in R package nlme with negative values

I have a nonlinear model with residuals that are negatively autocorrelated at short distances.

I can find no spatial correlation structures in nlme that can easily handle negative autocorrelation as most have bounds on parameter values so that the correlation is between 0 and 1.

First, is there something I am missing?

I tried to roll my own by calculating a correlation matrix with some negative values off the diagonal and then setting the correlation structure as follows within the function gnls:

correlation=corSymm(corr9x[lower.tri(corr9x)])


where corr9x is the $n \times n$ matrix of correlations that I set up based upon distances between points in the data set. Some of these correlations are positive and some are negative. They are based on a Moran's I correlogram that I calculated from the residuals returned from a gnls model fit with NO spatial correlation employed.

I get the following error:

Initial values for corSymm do not define a positive-definite correlation structure


I am unsure if the matrix is rejected out of hand because it contains negative values or if there is something I can do to coerce it. I have checked the lower triangle matrix returned and it matches what I intended.

Any input is appreciated.

• I am having the same problem. If I pass the custom matrix into the gnls function I get: Error in Initialize.corSymm(corSymm(corCust, fixed = T), data.frame(x = x)) : initial value for "corSymm" parameters of wrong dimension If I just pass through the lower triangle of the matrix I get: initial values for "corSymm" do not define a positive-definite correlation structure Did either user ever find a solution to this? Thanks. – user31449 Oct 14 '13 at 7:48
• This is more suitable as a comment to the questio, rather than an answer. – fredrikhs Oct 14 '13 at 8:49

I was having the same problem. I'm not sure if it's correct - but I was able to get past the positive definite problem by using the command nearPD(), which computes the nearest positive definite matrix.

I am now running into the problem that @wvguy8258 mentioned about the wrong dimensions.

• Interesting idea. Unfortunately, there is no such thing as that claimed by nearPD: unless a matrix already is positive-definite, there is no "nearest" p-d matrix. Maybe you just get a nonnegative-definite matrix. The distinction is crucial: it means nearPD may be returning a reduced rank matrix and that could be the source of the "wrong dimensions" error. In the end, no definitive general answer to this problem can be expected, because the diagnosis depends on exactly what matrix one is specifying and how it is specified (the OP hasn't given us those crucial details). – whuber Sep 25 '13 at 16:37
• @whuber Thanks - back to the drawing board on this one. Any ideas is there are any successful examples about creating a correlation structure using the distance between points? – Sarah Sep 25 '13 at 18:24
• I'm not quite sure what you're getting at, Sam, but I am reminded of the interpretation of correlation coefficients as the cosines of angles between points (qua vectors). – whuber Sep 25 '13 at 18:57

Are you just passing the lower triangle into the correlation structure?

Your error is saying your correlation matrix is not positive-definite. For a matrix to be positive definite, it will be nxn and symmetric. Pass the whole matrix to the function...

Otherwise, you're likely going to have to post the matrix for more help.

EDIT: I should of said most definitions of "positive definite" require symmetric matrices, primarily due to their origins in quadratic forms.

• Yes, I found an example where this appears to be the method. r.789695.n4.nabble.com/… and using the full matrix results in "initial value for corSymm parameters of wrong dimension" error message. I am still looking for a worked example as the help files are not clear. – wvguy8258 May 6 '13 at 0:29
• A square symmetric matrix is not necessarily positive definite (the one-by-one matrix with entry $-1$ is a simple counterexample) and positive definite matrices do not have to be symmetric. – whuber Sep 11 '13 at 15:32
• Whuber- there is no agreed upon definition of "positive definite" for non-hermetian matrices. However, traditionally, positive definite is applied to quadratic forms, which are described by symmetric matrices only. Thus, most definitions of "positive definite" require symmetry. – TLJ Oct 22 '13 at 20:44