On 6 October 2011 16:20, Dimitris.Kapetanakis <dimitrios.kapetana...@gmail.com> wrote: > Dear all, > > I would like to create a code for semiparametric Klein and Spady's > estimator. For that I created a function that provides the log-likelihood > function for each observation (so it is a function of betas and i, where i > denotes the observation). Now, in order to maximize the log-likelihood > function, I have to sum these log-likelihood functions for each i and so to > get another function that is a function only of betas and so to maximize it > through maxLik for instance. Is that possible? > > In order to be more clear I give an example of how it could be: > > Prob1 <- function(b, i) > g.yj(b,y=1,h.np,i)/(g.yj(b,y=1,h.np,i)+g.yj(b,y=0,h.np,i)) > loglik.i<- function(b, i) Y[i,]*log(Prob1(b,i))+(1-Y[i,])*log(1-Prob1(b,i)) > > where b denotes the betas, i the observations, Y is the response vector and > g.yj(b,1,h.np,i) a function that I created previously, Prob1(b,i) is a > function that gives the conditional probability for observation i and > loglik.i(b,i) is function that gives the log-likelihood for observation i. > > How can I sum the loglik.i(b,i) for each i and remain as a function of b > ONLY in order to maximize it??? > > For exemple this could be done manually by > loglik<- function(b) > loglik.i(b,1)+loglik.i(b,2)+loglik.i(b,3)+….+loglik.i(b,N) > > but how can I do it automatically for all observations?
loglik <- function(b) sapply( 1:N, loglik.i, b = b ) Please note that logLik( b ) returns a *vector* of the likelihood contributions of each observation. maxLik() takes the sum of the elements of this vector automatically. If logLik( b ) returns a vector of the likelihood contributions of each observation (rather than just the sum), the BHHH optimisation method can be used. /Arne -- Arne Henningsen http://www.arne-henningsen.name ______________________________________________ R-help@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.