1
$\begingroup$

I am trying to run a mixed effect model on soils data that were collected at two locations in a randomized complete block design (4 blocks). I am interested in the effect of location and depth (below ground surface; 10 depths) as fixed effects, with Block (nested within Location) as the random effect. I also know that Depth is spatially auto-correlated (that is soils from 0-15cm are similar to soils at 15-30cm but not less so to soils at 30-50cm). I am trying to use the lme model below, but am getting an error message:

Error in structure(res, levels = lv, names = nm, class = "factor") :
'names' attribute [70] must be the same length as the vector [0]

Data structure:

  • Nitrate concentration - dependent variable
  • 2 Locations
  • 10 soil depth categories (autocorrelated)
  • 4 Blocks (nested within location)

Link to dataset: http://www.agroecologylab.com/other-links.html

library("nlme")
InitdataKETZ <- read.csv("http://www.agroecologylab.com/uploads/2/7/2/8/27281831/initial_dp_ketz_zeron.csv")
fit <- lme(ConcNO3 ~ Location*Depth, random = ~1|Location/Block, 
           correlation = corAR1(form = ~Depth|Location/Block),
           data = InitdataKETZ)

If it is of any help, I have been told that the proper SAS code would be:

proc mixed;
class location block DepthCat;
model ConcNO3= Location Depth Depth*Location/ddfm=kr;
random block;
repeated Depth/ subject=Location*Block type=ar(1) r;
run;
$\endgroup$
4
  • $\begingroup$ Please provide a reproducible example for people to work with. $\endgroup$ Commented Jun 18, 2015 at 21:33
  • 1
    $\begingroup$ I'm voting to close this question as off-topic because it is about how to use R without a reproducible example. $\endgroup$ Commented Jun 18, 2015 at 21:33
  • 1
    $\begingroup$ I've edited this to make it fully reproducible by adding two lines of R code; i) load the nlme package, and ii) read the data directly from the indicated location. $\endgroup$ Commented Jun 18, 2015 at 21:49
  • $\begingroup$ Thanks, Gavin. This is my first StackExchange post. I was sure I was going to get the format wrong. $\endgroup$
    – Kate Tully
    Commented Jun 19, 2015 at 10:41

1 Answer 1

2
$\begingroup$

I had the same problem and finally figured it out. The documentation of corAR1 says that the form argument takes a covariate t, and this covariate must be an integer. In your data set, Depth is a factor. If you use the as.numeric() function, it will convert Depth to a form that is usable for the covariance structure:

ARTest <- lme(ConcNO3 ~ Location*Depth, random = ~1|Location/Block, 
       correlation = corAR1(form = ~as.numeric(Depth)|Location/Block),
       data = InitdataKETZ)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.