[R-sig-ME] mysterious syntax error in call to nlmer

Jacob Wegelin jacobwegelin at fastmail.fm
Sun Mar 27 19:25:19 CEST 2011


I'm trying to fit my first nlmer model. I follow Doug Bates's code at http://lme4.r-forge.r-project.org/slides/2009-07-01-Lausanne/8NLMMD.pdf and use lme4_0.999375-39.

A call to nls works fine, but when I turn it into a call to nlmer and add the random component, errors are returned. Providing start as a named vector returns one kind of error; providing start as a list returns a different error.

My data are simulated but they contain noise (per the warning on the nlmer help page).

You can see a plot of the data at http://jacobwegelin.net/r-help/2011_03_27_1311_simulated.pdf.

The error messages are as follows. Code to reproduce the entire problem is below under Details.

> roop <-nlmer( 
+ 		Y ~ xintercept + amplitude * sin( frequency * X - xshift )
+ 			~ (xshift | ID)
+ 		, data=mdoot
+ 		, start=list(xintercept=1, amplitude=1.5, frequency=1, xshift=pi/2)
+ 		)
Error: length(start$fixef) > 0 is not TRUE
> 
> roop <-nlmer( 
+ 		Y ~ xintercept + amplitude * sin( frequency * X - xshift )
+ 			~ (xshift | ID)
+ 		, data=mdoot
+ 		, start=c(xintercept=1, amplitude=1.5, frequency=1, xshift=pi/2)
+ 		)
Error in nlmer(Y ~ xintercept + amplitude * sin(frequency * X - xshift) ~  :
   gradient attribute of evaluated model must be a numeric matrix
>


Details:

set.seed(1)
N<-100; nIDs<-20
X<-seq(1,19, length=N)
Y<-sin(X) + rnorm(N)/10
IDs<-1:nIDs
trueRandomEffects<-data.frame(
 	xintercept=rnorm(nIDs)/3
 	, amplitude=1+ rnorm(nIDs)/9
 	, frequency =1+rnorm(nIDs)/10
 	, xshift =runif(nIDs) * pi
 	)
rownames( trueRandomEffects)<- IDs
mdoot<- DAT<-data.frame(ID=IDs[1], X=X, Y=Y)
for(thisperson in IDs[-1]) {
 	nextOne<-DAT
 	nextOne$Y<-  (
 			trueRandomEffects[thisperson,"xintercept"]
 			+ trueRandomEffects[thisperson,"amplitude"]
 			* sin(
 			trueRandomEffects[thisperson,"frequency"]  * X
 			- trueRandomEffects[thisperson,"xshift"]
 			)
 		+ rnorm(N)/10
 			)
 	nextOne$ID<-  thisperson
 	mdoot<-rbind( mdoot, nextOne)
}
attr(mdoot, "ranef")<-trueRandomEffects

require(ggplot2)
print(
ggplot(data=mdoot, mapping=aes(x=X, y=Y, group=ID, color=ID)) + geom_path()
)

whump<-nls(
 		Y ~ xintercept + amplitude * sin( frequency * X - xshift
 		)
 		, data=mdoot
 		, start=list(xintercept=1, amplitude=1.5, frequency=1, xshift=pi/2)
 		)
print(whump)

print(summary(whump))
mdoot$nlsfit<-predict(whump)
print(head(mdoot))

print(
ggplot(data=mdoot, mapping=aes(x=X, y=Y, group=ID, color=ID)) + geom_path() + geom_point(mapping=aes(x=X, y=nlsfit))
)

# Now where it stops working:

require(lme4)
roop <-nlmer(
 		Y ~ xintercept + amplitude * sin( frequency * X - xshift )
 			~ (xshift | ID)
 		, data=mdoot
 		, start=list(xintercept=1, amplitude=1.5, frequency=1, xshift=pi/2)
 		)

roop <-nlmer(
 		Y ~ xintercept + amplitude * sin( frequency * X - xshift )
 			~ (xshift | ID)
 		, data=mdoot
 		, start=c(xintercept=1, amplitude=1.5, frequency=1, xshift=pi/2)
 		)

> sessionInfo()
R version 2.12.2 (2011-02-25)
Platform: i386-apple-darwin9.8.0/i386 (32-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods
base

other attached packages:
[1] lme4_0.999375-39   Matrix_0.999375-46 lattice_0.19-17
ggplot2_0.8.8      proto_0.3-8 
[6] reshape_0.8.3      plyr_1.2.1

loaded via a namespace (and not attached):
[1] digest_0.4.2  nlme_3.1-98   stats4_2.12.2
>

Thanks for any insights

Jacob A. Wegelin
Assistant Professor
Department of Biostatistics
Virginia Commonwealth University
730 East Broad Street Room 3006
P. O. Box 980032
Richmond VA 23298-0032
U.S.A. 
E-mail: jacobwegelin at fastmail.fm 
URL: http://www.people.vcu.edu/~jwegelin




More information about the R-sig-mixed-models mailing list