[R] run optim() on a list

markleeds at verizon.net markleeds at verizon.net
Sun Sep 7 23:58:25 CEST 2008


  you need to send the data into the log likelihood function otherwise 
it just uses the global dataset. see the example on page 42 of the
enclosed document by Roger Peng.




On Sun, Sep 7, 2008 at  5:31 PM, Weidong Gu wrote:

> Hi,
>
>
> I am at the end of my wit to figure out how to run the optim function 
> on
> a list.
>
>
> Basically, I have a data set of three columns as "Site", "Pool" and
> "Positivity" ( the full data set is copied at the end). I want to run
> the maximal likelihood estimation separately on subsets split by 
> "Site"
>
>
> data<-read.table(...)
>
> sp<-split(data,data$Site)
>
>
> # My likelihood function is
>
> like<-function(p,...){
>
>   for(i in 1:length(Pool)){
>
>                         if(Positivity[i]==1)
> log.l[i]<-log(1-(1-p)^Pool[i])
>
>                         else log.l[i]<-Pool[i]*log(1-p)
>
>                         }
>
>                         return(sum(log.l))
>
> }
>
> # Then I run
> lapply(sp,function(x) optim (0.1,like,control=list(fnscale=-1)))
>
>
> #But it gives an estimation based on the full data, not separately on
> sp[[1]], sp[[2]],... I tried do.call without success. So, your help
> would be appreciated.
>
>
>
> Weidong Gu
>
>
> Department of Medicine
> University of Alabama, Birmingham
> 1900 University Blvd., Birmingham, Alabama 35294
> Email: wgu at uab.edu
> PH: (205)-975-9053
>
>
> Site      Pool     Positivity
>
> UBA_1
>
> 22
>
> 0
>
> UBA_1
>
> 50
>
> 0
>
> UBA_1
>
> 23
>
> 0
>
> UBA_1
>
> 25
>
> 0
>
> UBA_1
>
> 35
>
> 0
>
> UBA_1
>
> 24
>
> 0
>
> UBA_1
>
> 26
>
> 0
>
> Cham_res
>
> 43
>
> 0
>
> Cham_res
>
> 45
>
> 0
>
> Cham_res
>
> 34
>
> 0
>
> Cham_res
>
> 24
>
> 0
>
> Cham_res
>
> 21
>
> 0
>
> Cham_res
>
> 16
>
> 0
>
> Cham_res
>
> 28
>
> 0
>
> Cham_res
>
> 50
>
> 0
>
> Cham_res
>
> 50
>
> 1
>
> Cham_res
>
> 39
>
> 1
>
> UBA_2
>
> 16
>
> 0
>
> UBA_2
>
> 18
>
> 1
>
> UBA_2
>
> 42
>
> 1
>
> UBA_2
>
> 35
>
> 1
>
> UBA_2
>
> 50
>
> 1
>
> UBA_2
>
> 26
>
> 0
>
> UBA_2
>
> 20
>
> 0
>
> UBA_2
>
> 16
>
> 0
>
> UBA_2
>
> 19
>
> 0
>
> UBA_2
>
> 50
>
> 0
>
> UBA_2
>
> 26
>
> 0
>
> UBA_2
>
> 13
>
> 1
>
> UBA_2
>
> 30
>
> 1
>
> UBA_3
>
> 17
>
> 0
>
> UBA_3
>
> 20
>
> 0
>
> UBA_3
>
> 19
>
> 0
>
> UBA_3
>
> 50
>
> 0
>
> UBA_3
>
> 24
>
> 1
>
> UBA_3
>
> 18
>
> 1
>
> UBA_3
>
> 16
>
> 1
>
> UBA_3
>
> 14
>
> 0
>
> UBA_3
>
> 12
>
> 0
>
> UBA_3
>
> 15
>
> 0
>
> UBA_3
>
> 11
>
> 0
>
> UBA_3
>
> 20
>
> 1
>
> UBA_3
>
> 19
>
> 1
>
> UBA_3
>
> 31
>
> 1
>
> UBA_4
>
> 12
>
> 0
>
> UBA_4
>
> 11
>
> 0
>
> UBA_4
>
> 12
>
> 0
>
> UBA_4
>
> 21
>
> 0
>
> UBA_4
>
> 33
>
> 0
>
> UBA_4
>
> 15
>
> 0
>
> UBA_4
>
> 10
>
> 0
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: r-classes-scope.pdf
Type: application/pdf
Size: 104274 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080907/24663835/attachment.pdf>


More information about the R-help mailing list