Re: [R] Loop problem

From: Adaikalavan Ramasamy <ramasamy_at_cancer.org.uk>
Date: Wed 03 Aug 2005 - 20:45:52 EST

  1. You need to simplify your codes a bit and show people how the input might look like if you want useful responses. Perhaps a small reproducible example or brief description of what you are trying to do might help. See the posting guide for more details.
  2. I am guessing here but are you interested in multinomial logistic regression ? If so, searching the R-help suggests glm with family="binomial", multinom in nnet package and http://www.stat.auckland.ac.nz/~yee/VGAM/ . Remember that you may need 'Tr' to be a factor.
  3. You should avoid writing functions that depend on global variables as they may change. If you really need your MLE function, here is an (untested) way to rewrite it such that it requires explicit inputs.

  my.MLE <- function (x, mu, v){

    n <- length( x )
    tmp <- n*log(2*pi) + n*log(v) + sum( ( x - mu )^2 ) / v     return( 0.5*tmp )

  }

and you call it with

   my.MLE( x=logitTr1$logitp, mu=p, v=logitTr1$sd^2 )

Regards, Adai

On Tue, 2005-08-02 at 20:36 +0100, Hathaikan Chootrakool wrote:
> Dear everyone
>
> I am a new user,would like to combine these code together by using a
> loop,each function has three value as Tr = 1 - 3,how can i combine
> together?
>
>
> logitTr1 <-logit[logit[,"Study"]&logit[,"Tr"]==1,]
> (number of row in each group (1-3) is difference but equal in colume)
>
> fnTr1 <- function (p) sum(
> n/2*log(2*pi)+log(1/logitTr1$sd)+1/2*(logitTr1$logitp*logitTr1$logitp-2*logitTr1$logitp*p+p^2)
> *1/logitTr1$sd*logitTr1$sd )
> (maximum likelyhood function)
>
> outTr1<- nlm (fnTr1,p=c(10),hessian=TRUE)
> minimumTr1 <- outTr1$minimum
> valueTr1 <- outTr1$estimate
> (estimate the value)
>
> The problem is the program couldn't work by using logitTr[i],fnTr[i]
> outTr[i],minimumTr[i],value[i] in a loop. The function logitTr[i] is the
> data matrix which is not equal each group, fnTr[i] is the
> maximumlikelyhood function for estimation in next step;
> outTr[i],minimumTr[i],valueTr[i].
>
>
>
> Has anyone got any idea to help me?,thank you very much.
>
> Hathaikan
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Aug 03 20:51:23 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 15:02:26 EST