Charlotte Chang
2010-Apr-26 06:00 UTC
[R] Using optim function for logistic model simulation
Hello! I'm a college undergrad desperately trying to finish up my thesis. I have a dataset on the distribution of a grassland bird from the Breeding Bird Survey. I have a very straightforward and simple version of the logistic growth model to describe changes in this bird's abundance over time. The variables are the natural birth rate, mortality, and carrying capacity. The equation itself looks like this: birds <- c*birds + b*birds*(1-birds/K) where c = mortality, b = birth rate, and K = carrying capacity. I'm trying to find the optimal value of K and I've been using the optim function. The problem is that it doesn't really work. My code is below: b<-1.22 c<-0.55 bird<-bird.density[0] eqn1<- function(K1, bird) { for (i in 1:8) { b<-1.22 c<-0.55 bird <- 0.55*bird + b*bird*1-b*bird*bird/K } } k1<-optim(c(0,10),eqn) I realize this is a painfully amateurish question, but I really appreciate any help! The other tricky part about this is that it looks like there are 3 distinct time periods where the bird is increasing, declining, and increasing again. I'm thinking that there are 3 different carrying capacity values, but my understanding of optim is that it can only optimize one parameter variable at a time. So I was going to take the brute force approach of artificially breaking up the equation into 3 parts (i.e. make eqn1, eqn2, and eqn3, and optimize each separately, with the input values for the later two functions coming from the last number of birds from the previous loop). Thank you all very much! charlotte
Hi Charlotte , I can't reproduce your code, but skimming through it - It would appear that: 1) in eqn1<- function(K1, bird) you didn't define "bird" (you did define it before the function, so I'd suggest just removing it from the function call like this: eqn1<- function(K1) 2) you didn't "return" and value at the end of the function. 3) you use different name in optim then in the function you made. 4) I see no point for you to use the for loop. Consider trying the following code: b<-1.22 c<-0.55 bird<-bird.density[0] # I assume this exists eqn<- function(K1, bird) { b<-1.22 c<-0.55 bird <- 0.55*bird + b*bird*1-b*bird*bird/K return(bird) } k1<-optim(c(0,10),eqn) ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Mon, Apr 26, 2010 at 9:00 AM, Charlotte Chang <c.h.w.chang@gmail.com>wrote:> optim[[alternative HTML version deleted]]
Charlotte Chang
2010-Apr-26 08:33 UTC
[R] Using optim function for logistic model simulation
Great! Thank you for your help! -Charlotte>> >> On Mon, Apr 26, 2010 at 1:12 AM, Tal Galili <tal.galili at gmail.com> wrote: >>> >>> mm... >>> I also noticed the function you wrote didn't use?parenthesis, mixed b and c and used different names for K. >>> Your code is a great?exercise?in debugging ?(no offense intended :) ) >>> Try using: >>> bird<-bird.density[0] # I assume this exists >>> eqn<- function(K1, b1 = 1.22, c1 = .55) { >>> ?? ? ? ? ? ? ? b<-1.22 >>> ? ? ? ? ? ? ? ?c<-0.55 >>> ? ? ? ? ? ? ? ?bird <- c1*bird + b1*bird*(1-b1*bird)*bird/K1 >>> ?? ? ? ? ? ?return(bird) >>> } >>> >>> k1<-optim(c(0,10),eqn) >>> >>> >>> >>> ----------------Contact Details:------------------------------------------------------- >>> Contact me: Tal.Galili at gmail.com | ?972-52-7275845 >>> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) >>> ---------------------------------------------------------------------------------------------- >>> >>> >>> >>> >>> On Mon, Apr 26, 2010 at 11:08 AM, Tal Galili <tal.galili at gmail.com> wrote: >>>> >>>> Hi?Charlotte , >>>> I can't reproduce your code, but skimming through it - >>>> It would appear that: >>>> 1) in >>>> eqn1<- function(K1, bird) >>>> you didn't define "bird" (you did define it before the function, so I'd suggest just removing it from the function call like this: >>>> eqn1<- function(K1) >>>> 2) you didn't "return" and value at the end of the function. >>>> 3) you use different name in optim then in the function you made. >>>> 4) I see no point for you to use the for loop. >>>> >>>> >>>> Consider trying the following code: >>>> b<-1.22 >>>> c<-0.55 >>>> bird<-bird.density[0] # I assume this exists >>>> eqn<- function(K1, bird) { >>>> ?? ? ? ? ? ? ? b<-1.22 >>>> ? ? ? ? ? ? ? ?c<-0.55 >>>> ? ? ? ? ? ? ? ?bird <- 0.55*bird + b*bird*1-b*bird*bird/K >>>> ?? ? ? ? ? ?return(bird) >>>> } >>>> >>>> k1<-optim(c(0,10),eqn) >>>> >>>> >>>> ----------------Contact Details:------------------------------------------------------- >>>> Contact me: Tal.Galili at gmail.com | ?972-52-7275845 >>>> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) >>>> ---------------------------------------------------------------------------------------------- >>>> >>>> >>>> >>>> >>>> On Mon, Apr 26, 2010 at 9:00 AM, Charlotte Chang <c.h.w.chang at gmail.com> wrote: >>>>> >>>>> optim >>> >> >