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

Jacob Wegelin jacobwegelin at fastmail.fm
Tue Apr 5 22:08:40 CEST 2011


Thank you for the solution. Below the example is reproduced with your code (which makes it work), plus code to compare the fitted values graphically.

I removed the lme4 portion has been removed, since zero variance for xshift is wrong. Why does lmer{lme4} return such an estimate? Is there something wrong with the way we called lmer?

Your tangential question is answered below.

set.seed(1)
N<-100; nIDs<-20
X<-seq(1,19, length=N)
Y<-sin(X) + rnorm(N)/10
IDs<-1:nIDs

## I have a silly tangential question.  Why put the commas
##  on the next line?  I haven't seen this convention much before, 
##  but it has started appearing on this list.  In general it
##  makes much more sense to me (and occasionally makes a 
##  difference) to put connectors (commas, arithmetic operators etc.)
##  at the end of the previous line rather than the beginning of
##  the next ...
## 
##	Answer: I edit these files in vim.  With vim, I can easily
##	delete a line or put a # in front of a line to comment it out.
##	See the "junk" line commented out below for example. 
##	If the commas had been at the end of the line, I would have had 
##	to remember to delete the comma at the end of the previous line.

##	With parentheses around the entire expression, + , - * are not 
##	needed at the end of lines to show that more is to come.


trueRandomEffects<-data.frame(
  	xintercept=rnorm(nIDs)/3
  	, amplitude=1+ rnorm(nIDs)/9
  	, frequency =1+rnorm(nIDs)/10
  	, xshift =runif(nIDs) * pi
# 	, junk=wrong_idea
  	)
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

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

parnames <- c("xintercept","amplitude","frequency","xshift")
dd <- deriv(expression(xintercept + amplitude *
         sin( frequency * X - xshift)),
     namevec=parnames, function.arg=c(parnames,"X"))
library(nlme)


roop2 <-nlme(  Y ~ dd(xintercept,amplitude,frequency,xshift,X),
               data=mdoot,
              fixed = xintercept + amplitude + frequency + xshift ~ 1,
              random = xshift~1|ID,
               start=c(xintercept=1, amplitude=1.5,
                             frequency=1, xshift=pi/2)
              )
summary(roop2)

mdoot$nlmefit0<-predict(roop2 , level=0)
mdoot$nlmefit1<-predict(roop2 , level=1)

require(ggplot2)
layernlsfit<-layer(
 	data=mdoot
 	, mapping=aes(x=X, y=nlsfit)
 	, geom="point"
 	, geom_params=list(color="red")
 	)
# print(ggplot() + layernlsfit)


layerFixFit<-layer(
 	data=mdoot
 	, mapping=aes(x=X, y=nlmefit0)
 	, geom="line"
 	, geom_params=list(lwd=2)
 	)
# print(ggplot() + layerFixFit)

layerRanFit<-layer(
 	data=mdoot[mdoot$ID < 6, ]
 	, mapping=aes(x=X, y=nlmefit1, group=ID, color=ID)
 	, geom="line"
 	, geom_params=list(lty=2)
 	)
# print(ggplot() + layerRanFit)

print(
 	ggplot()
 	+ layerFixFit
 	+ layerRanFit
 	+ layernlsfit
 	+ scale_y_continuous(name="fitted value")
 	)

##	Back to the tangential question: 
##	Note how easy it would be with a single # at the start of a line
##	to remove one of the layers--without ever touching an electronic 
##	mouse and without deleting + marks.

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

On Sun, 27 Mar 2011, Ben Bolker wrote:

> Jacob Wegelin <jacobwegelin at ...> writes:
>
>
>  Thank you for your very clear and reproducible example!
<snip>




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