[R-sig-ME] deriv example for nlmer

Helen Sofaer helen at lamar.colostate.edu
Tue Oct 19 21:27:52 CEST 2010


Thanks--that does make it clear.
Thanks again for your help with the deriv function, 
Helen

On Tue, 19 Oct 2010 14:49:24 -0400, Manuel Morales
<Manuel.A.Morales at williams.edu> wrote:
> Oops - I should have checked the code before posting! Note that the
> issue is with what is returned by body(logisKsite) ... you wouldn't need
> the [[2]] if the function logisKsite didn't have brackets.
> 
> Compare:
> 
> fn1 <- function(x) x
> fn2 <- function(x) {
>   x
> }
> 
> body(fn1)
> body(fn2)
> body(fn2)[[2]]               
> 
> 
> 
> On Tue, 2010-10-19 at 11:43 -0600, Helen Sofaer wrote:
>> Thanks!!
>> 
>> With your deriv code I got the error: Function '`{`' is not in the
>> derivatives table
>> Googling the error led me to a suggestion that I add [[2]] to the deriv
>> function like this:
>> 
>> logisSiteDeriv = deriv(body(logisKsite)[[2]], namevec = c("Asym",
"xmid",
>> "K", "Kdiff", "middiff"), function.arg=logisKsite)
>> 
>> And that function runs in nlmer! So, I've very happy, except I am
curious
>> as to what the [[2]] does...
>> 
>> Thanks again for your quick response.
>> Helen
>> 
>> On Tue, 19 Oct 2010 13:11:47 -0400, Manuel Morales
>> <Manuel.A.Morales at williams.edu> wrote:
>> > How about:
>> > 
>> > grModel <- function(Age, site, Asym, xmid, K, Kdiff, middiff) {
>> >   Asym/(1 + exp((xmid+middiff*site - Age)*(K+Kdiff*site)))
>> > }
>> > 
>> > grModg <- deriv(body(grModel),
>> >                 namevec = c("Asym", "xmid", "K", "Kdiff", "middiff"),
>> >                 function.arg=grModel)
>> > 
>> > startsite <- c(Asym=9, xmid=3, K=.5, Kdiff=0, middiff=0)
>> > 
>> > 
>> > nlmer(weight ~ grModg(Age,site,Asym,xmid,K,Kdiff,middiff) ~
>> > (K|Nest_ID),
>> >                       data=growth, start=startsite, verbose = TRUE)
>> > 
>> > On Tue, 2010-10-19 at 08:25 -0600, Helen Sofaer wrote:
>> >> Hi Thierry,
>> >> My main goal is to test for a difference between populations, which
I
>> >> can't do with SSlogis.
>> >> Check out the code for my function to see the model I'm trying to
run.
>> >> 
>> >> Thanks,
>> >> Helen
>> >> 
>> >> 
>> >> On Tue, 19 Oct 2010 10:09:33 +0200, "ONKELINX, Thierry"
>> >> <Thierry.ONKELINX at inbo.be> wrote:
>> >> > Dear Helen,
>> >> > 
>> >> > Won't it be easier to stick with Sslogis to run the model and then
>> >> > convert scal to K for your report?
>> >> > 
>> >> > HTH,
>> >> > 
>> >> > Thierry
>> >> > 
>> >> >
>>
------------------------------------------------------------------------
>> >> > ----
>> >> > ir. Thierry Onkelinx
>> >> > Instituut voor natuur- en bosonderzoek
>> >> > team Biometrie & Kwaliteitszorg
>> >> > Gaverstraat 4
>> >> > 9500 Geraardsbergen
>> >> > Belgium
>> >> > 
>> >> > Research Institute for Nature and Forest
>> >> > team Biometrics & Quality Assurance
>> >> > Gaverstraat 4
>> >> > 9500 Geraardsbergen
>> >> > Belgium
>> >> > 
>> >> > tel. + 32 54/436 185
>> >> > Thierry.Onkelinx at inbo.be
>> >> > www.inbo.be
>> >> > 
>> >> > To call in the statistician after the experiment is done may be no
>> more
>> >> > than asking him to perform a post-mortem examination: he may be
able
>> to
>> >> > say what the experiment died of.
>> >> > ~ Sir Ronald Aylmer Fisher
>> >> > 
>> >> > The plural of anecdote is not data.
>> >> > ~ Roger Brinner
>> >> > 
>> >> > The combination of some data and an aching desire for an answer
does
>> >> > not
>> >> > ensure that a reasonable answer can be extracted from a given body
>> >> > of
>> >> > data.
>> >> > ~ John Tukey
>> >> >   
>> >> > 
>> >> >> -----Oorspronkelijk bericht-----
>> >> >> Van: r-sig-mixed-models-bounces at r-project.org 
>> >> >> [mailto:r-sig-mixed-models-bounces at r-project.org] Namens Helen
>> Sofaer
>> >> >> Verzonden: dinsdag 19 oktober 2010 4:07
>> >> >> Aan: r-sig-mixed-models at r-project.org
>> >> >> Onderwerp: [R-sig-ME] deriv example for nlmer
>> >> >> 
>> >> >> 
>> >> >> Hello modelers,
>> >> >> I am working on a growth rate analysis in lme4, where my goal 
>> >> >> is to test for differences in growth rates between two 
>> >> >> populations of birds while incorporating random effects for 
>> >> >> nests (to deal with the lack of independence between 
>> >> >> siblings) and for each nestling (to deal with repeated 
>> >> >> measures of individuals). 
>> >> >> 
>> >> >> I'd like to build a model that is parameterized slightly 
>> >> >> differently than the SSlogis self-starting model, where K = 
>> >> >> 1/scal (this is simply due to convention in the bird world). 
>> >> >> I can tweak the SSlogis code (given in the help file for 
>> >> >> selfStart) and the model runs, but cannot test for 
>> >> >> differences between populations. Since determining reasonable 
>> >> >> starting values is not a problem, and I'd like to be able to 
>> >> >> easily adjust the formula to incorporate additional 
>> >> >> covariates, I would like to avoid using a self-starting function.
>> >> >> 
>> >> >> It is my understanding that nlmer requires a function, but 
>> >> >> not necessarily a self-starting one. However, I can't find an 
>> >> >> example of any code that does this. 
>> >> >> 
>> >> >> I can build a very simple function that allows for 
>> >> >> differences between populations (referred to as site, which 
>> >> >> is a 0 1 dummy variable) in both the inflection point (xmid) 
>> >> >> and the growth rate (K). I'm trying to start with the 
>> >> >> simplest reasonable model, so I didn't allow for differences 
>> >> >> in the asymptote. This function works in nls:
>> >> >> 
>> >> >> logisKsite = function(Age, site, Asym, xmid, K, Kdiff, middiff){
>> >> >> 	Asym/(1 + exp((xmid+middiff*site - Age)*(K+Kdiff*site)))
>> >> >> 	}
>> >> >> 
>> >> >> startsite = c(Asym = 9, xmid = 3, K = .5, Kdiff=0, middiff=0) 
>> >> >> nls_logisKsite = nls(weight ~ logisKsite(Age, site, Asym, 
>> >> >> xmid, K, Kdiff, middiff), growth, start = startsite)
>> >> >> 
>> >> >> The nlmer model I am trying to run is (for simplicity, this 
>> >> >> model includes only a random effect of nest, only on the K
>> parameter):
>> >> >> nlmer_logisKsite = nlmer(weight ~ logisKsite(Age, site, Asym, 
>> >> >> xmid, K, Kdiff, middiff) ~ (K | Nest_ID), growth, start = 
>> >> >> startsite, verbose = TRUE)
>> >> >> 
>> >> >> Running this model in nlmer produces the error: gradient 
>> >> >> attribute of evaluated model must be a numeric matrix Dr. 
>> >> >> Bates' posted lectures note that the model must provide 
>> >> >> derivatives, via the deriv function. I haven't been 
>> >> >> successful getting this to work, even in the model without 
>> >> >> site effects.
>> >> >> 
>> >> >> If there is an example of how to run nlmer without a 
>> >> >> self-start function, could someone please point me to it? 
>> >> >> Advise on how to implement the deriv function would also be 
>> >> >> very helpful. 
>> >> >> 
>> >> >> Thank you very much,
>> >> >> 
>> >> >> Helen
>> >> >> 
>> >> >> Helen Sofaer, PhD Candidate in Ecology, Colorado State University
>> >> >> 
>> >> >> _______________________________________________
>> >> >> R-sig-mixed-models at r-project.org mailing list 
>> >> >> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
>> >> >>
>> >>
>>

-- 
Helen Sofaer
PhD Candidate
Colorado State University
Graduate Degree Program in Ecology
Biology Department
Program for Interdisciplinary Mathematics, Ecology, and Statistics Fellow
(970) 491-2782




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