title: Specifying a correlation structure for a linear mixed model using the ramps package in R
body: <p>I am trying to create a linear mixed model (lmm) that allows for a spatial correlation between points (have lat/long for each point). I would like the spatial correlation to be based upon the great circular distance between points.</p>
  
  <p>The package <code>ramps</code> includes a correlation structure that computes the ‘haversine’ distance – although I am having trouble implementing it. I have previously used other correlation structures (<code>corGaus</code>, <code>corExp</code>) and not had any difficulties. I am assuming the <code>corRGaus</code> with the 'haversine' metric can be implemented in the same way. </p>
  
  <p>I am able to successfully create an lmm with spatial correlation calculated on a planar distance using the <code>lme</code> function.</p>
  
  <p>I am also able to create a linear model (not mixed) with spatial correlation calculated using great circular distance although there are errors with the correlation structure using the <code>gls</code> command. </p>
  
  <p>When trying to the use the <code>gls</code> command for a linear model with the great circular distance I have the following errors:</p>
  
  <pre><code>x = runif(20, 1,50)
  y = runif(20, 1,50)
  gls(x ~ y, cor = corRGaus(form = ~ x + y))
  
  Generalized least squares fit by REML
   Model: x ~ y 
   Data: NULL 
  Log-restricted-likelihood: -78.44925
  
  Coefficients:
   (Intercept)            y 
  24.762656602  0.007822469 
  
  Correlation Structure: corRGaus
   Formula: ~x + y 
   Parameter estimate(s):
  Error in attr(object, "fixed") &amp;&amp; unconstrained : 
   invalid 'x' type in 'x &amp;&amp; y'
  </code></pre>
  
  <p>When I increase the size of the data there are memory allocation errors (still a very small dataset):</p>
  
  <pre><code>x = runif(100, 1, 50)
  y = runif(100, 1, 50)
  lat = runif(100, -90, 90)
  long = runif(100, -180, 180)
  gls(x ~ y, cor = corRGaus(form = ~ x + y))
  
  Error in glsEstimate(glsSt, control = glsEstControl) : 
  'Calloc' could not allocate memory (18446744073709551616 of 8 bytes)
  </code></pre>
  
  <p>When trying to run a mixed model using the <code>lme</code> command and the <code>corRGaus</code> from the <code>ramps</code> package the following results:</p>
  
  <pre><code>x = runif(100, 1, 50)
  y = runif(100, 1, 50)
  LC = c(rep(1, 50) , rep(2, 50))
  lat = runif(100, -90, 90)
  long = runif(100, -180, 180)
  
  lme(x ~ y,random = ~ y|LC, cor = corRGaus(form = ~ long + lat))
  
  Error in `coef&lt;-.corSpatial`(`*tmp*`, value = value[parMap[, i]]) : 
    NA/NaN/Inf in foreign function call (arg 1)
  In addition: Warning messages:
  1: In nlminb(c(coef(lmeSt)), function(lmePars) -logLik(lmeSt, lmePars),  :
    NA/NaN function evaluation
  2: In nlminb(c(coef(lmeSt)), function(lmePars) -logLik(lmeSt, lmePars),  :
    NA/NaN function evaluation
  </code></pre>
  
  <p>I am unsure about how to proceed with this method. The "haversine" function is what I want to use to complete my models, but I am having trouble implementing them. There are very few questions anywhere about the <code>ramps</code> package, and I have seen very few implementations. Any helps would be greatly appreciated. </p>
  
  <p>I have previously attempted to modify the <code>nlme</code> package and was unable to do so. I posted a question about <a href="https://stackoverflow.com/questions/18772919/how-to-update-and-recompile-nlme-source-code-in-r">this</a>, where I was recommended to use the <code>ramps</code> package. </p>
  
  <p>I am using R 3.0.0 on a Windows 8 computer. </p>