[R] ifelse command

Carlos J. Gil Bellosta cgb at datanalytics.com
Sat Jul 22 21:10:20 CEST 2006


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 at 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.



More information about the R-help mailing list