Unable to fit repeated measures in R I've been trying to fit a repeated measures model in R using lmer but I get an error message. I've used SAS in the past and I was able to fit a somewhat similar model, but I can't seem to be able to do it in R. 
This is the SAS code:
proc glimmix data=dat;
class size day position subject;
model y = day size size*day position size*position/ ddfm=kr;
random day /subject=subject(size) type=ar(1) residual;
lsmeans size|day position ;

The following is the R code:
m1 = lmer(y ~ 1  + Size + Position + Size*Day + Position + (1 + Day|ID), 
     data = dat, REML = F)

and these are the error messages I get:
Warning messages:
1: In checkZrank(reTrms$Zt, n = n, control, nonSmall = 1e+06) :
  number of observations <= rank(Z); variance-covariance matrix will be unidentifiable
2: In (function (fn, par, lower = rep.int(-Inf, n), upper = rep.int(Inf,  :
  failure to converge in 10000 evaluations

The data consists of 242 observations taken from 22 individuals over a 11 day period, plus some other variables were observed as well (i.e. position). Size, day, subject and position are factors and y is the observed response.
The data structure (I can provide the whole 242 observations if that would help):
> dput(head(dat,4))
structure(list(y = c(7.12820702168194, 7.70152948380659, 6.42263644198494, 
6.9249926063685), Day = structure(1:4, .Label = c("1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11"), class = "factor"), 
    Size = c(1, 1, 1, 1), Subject = structure(c(1L, 1L, 1L, 1L
    ), .Label = c("101X", "104X", "111X", "112X", "118X", "119X", 
    "126X", "139X", "26X", "41X", "44X", "45X", "46X", "48X", 
    "55X", "63X", "67X", "68X", "79X", "84X", "87X", "92X"), class = "factor"), 
    Position = c(1, 1, 1, 2)), .Names = c("y", "Day", "Size", 
"Subject", "Position"), row.names = c(NA, 4L), class = "data.frame")

I'd appreciate any help with this issue. 
Regards,
 A: There are a few issues here.


*

*You're trying to fit an AR1 model; lme4 doesn't have that capability (yet).  I don't know SAS syntax that well, so I can't tell, but I think you're specifying that the random effects themselves are autocorrelated. lme (from the nlme package) allows AR1 residuals, but not AR1 random effects (it might be possible, but I'm pretty sure it's not).

*The only random-effects correlation structure that lme4 currently handles is "unstructured", i.e. the only constraint is that the correlation structure has to be positive definite.  Therefore, the interaction of day with subject is confounded with the residual variance term (since there is exactly one observation for each day:subject combination).  It also means you have a really big model, since you are trying to fit the parameters of an 11-by-11 variance-covariance matrix (thus this is a 66-parameter nonlinear optimization!)

*for what it's worth, size*day in R formula notation specifies the main effects as well as the interaction.


So this is not exactly the same model but is a reasonably close approximation:
library(nlme)
m1 = lme(y ~ Size*Day + Size*Position,
         random = ~ 1 | ID,
         correlation = corAR1(form=~Day|ID))
         data = dat, method="ML")

The Kenward-Roger degrees-of-freedom calculation is available in the pbkrtest package, but it only works with lmer models (not lme models).
Your other alternative would be to fit in lmer:
library(lme4)
m2 = lmer(y ~ Size*Day + Size*Position + (1|ID),
         data = dat, REML=FALSE)

and then check the residuals for autocorrelation (and hope it's weak/non-significant).
If you treat day as a continuous covariate then you can (and probably should) include a random effect term of the form (Day|ID), which will allow for among-individual variation in time trend.
