Re: [R] ifelse command

From: Carlos J. Gil Bellosta <cgb_at_datanalytics.com>
Date: Sun 23 Jul 2006 - 05:10:20 EST

Dear Xin,

Although I have no idea what your function does, I believe it would be "formally" correct in the following way:

foo <- function (parameters,y,x1,x2)
{

	p <-parameters[1]
	alpha1<-parameters[2]
	beta1<-parameters[3]
	delta1<-parameters[4]
	alpha2<-parameters[5]

	mu<-alpha1*((x1)^beta1)*exp(-delta1*(x1^alpha2))

	if(y>0 & x1>0 & x2==1) return( lgamma(y+p)+p*(log(p)-log(mu
+p))+y*(log(mu)-log(mu+p))-lfactorial(y)-lgamma(p) )
	if(y>0 & x1>0 & x2==2) return( lgamma(y+p)+p*(log(p)-log(mu
+p))+y*(log(mu)-log(mu+p))-lfactorial(y)-lgamma(p) )
	if(y>0 & x1>0 & x2==3) return( lgamma(y+p)+p*(log(p)-log(mu
+p))+y*(log(mu)-log(mu+p))-lfactorial(y)-lgamma(p) )         

        return( "what then?" )
}

Please, compare your code to mine. You will discover more than one error in your code. See also where the if() parenthesis closes. I do not believe your else's are necessary within a function. See also that the code after your ifs is always the same.

Sincerely,

Carlos J. Gil Bellosta
http://www.datanalytics.com
http://www.data-mining-blog.com

El sáb, 22-07-2006 a las 13:25 +0100, Xin escribió:
> Dear:
>
> I try to revise the maximum likelihood function below using something constrains. But it seems something wrong with it. Becasue R would not allow me to edit the function like this. It is very appreciate if you can help.
>
> function (parameters,y,x1,x2)
>
> {
>
> p<-parameters[1]
>
> alpha1<-parameters[2]
>
> beta1<-parameters[3)]
>
> delta1<-parameters[4]
>
> alpha2<-parameters[5]
>
> mu<-alpha1*((x1)^beta1)*exp(-delta1*(x1^alpha2))
>
> if(y>0 & x1>0 & x2==1,
>
> L<-lgamma(y+p)+p*(log(p)-log(mu+p))+y*(log(mu)-log(mu+p))-lfactorial(y)-lgamma(p)
>
> )
>
> else
>
> if(y>0 & x1>0 & x2==2,
>
> L<-lgamma(y+p)+p*(log(p)-log(mu+p))+y*(log(mu)-log(mu+p))-lfactorial(y)-lgamma(p)
>
> )
>
> else
>
> if(y>0 & x1>0 & x2==3,
>
> L<-lgamma(y+p)+p*(log(p)-log(mu+p))+y*(log(mu)-log(mu+p))-lfactorial(y)-lgamma(p)
>
> )
>
> else
>
> L
>
> }
>
> Thanks a lot!
>
> Xin Shi
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.



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 and provide commented, minimal, self-contained, reproducible code. Received on Sun Jul 23 05:07:32 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sun 23 Jul 2006 - 06:16:56 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.