[R] Why the contrain does not work for selecting a particular range of data?

Duncan Murdoch murdoch at stats.uwo.ca
Sun Jul 23 14:14:33 CEST 2006


On 7/23/2006 4:07 AM, Xin wrote:
> Dear:
> 
>     Continuing the issue of 'ifelse'! I selecting the data whose 'x2'=1 for maximizing likelihood. I used two way to do this but the results are different.

In the first case you used ifelse(), in the second you used if().  They 
behave differently:  ifelse() evaluates all tests in a vector, if() only 
evaluates one.  You probably want ifelse() in both cases.

Duncan Murdoch

>    
>     1.Way one I use the data for x2=1 and run the program. It works for me. Tthe program is described as below:
> function (parameters,y1,x11)
> {
> p<-parameters[1]
> alpha1<-parameters[2]
> beta1<-parameters[3]
> delta1<-parameters[4]
> lamda1<-parameters[5]
> 
> mu<-alpha1*((x11)^beta1)*exp(-delta1*(x11^lamda1))
> 
> ifelse(y1>0|x11>0,
> 
> L<-lgamma(y1+p)+p*(log(p)-log(mu+p))+y1*(log(mu)-log(mu+p))-lfactorial(y1)-lgamma(p)
> 
> ,Inf)
> 
> L
> 
> }
> 
>     This is working for me.
> 
>    2 Way two: I select the data whose x2=1 in the whole range of data. It works but it is not right comparing the value of MLE. the program is:
> 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(x1>0 & x2==1)
> 
> {
> 
> L<-lgamma(y+p)+p*(log(p)-log(mu+p))+y*(log(mu)-log(mu+p))-lfactorial(y)-lgamma(p)
> 
> }
> 
> 
> 
> L
> 
> }
> 
>    The reason why I edit the program by the second way is I want to use one program for getting results of the different range of data.
> 
> Anyone can help? Please!
> 
> Thanks!
> 
> 
> 
> Xin Shi
> 
> 
> 
> My Estimation function for way two is :
> 
> function (parameters, y, x1,x2)
> 
> {
> 
> nx1 <- length(x1);
> 
> nx2 <- length(x2);
> 
> ny <- length(y);
> 
> x1 <- matrix(x1,nrow=nx1,ncol=1);
> 
> x2 <- matrix(x2,nrow=nx2,ncol=1);
> 
> y <- matrix(y,nrow=ny,ncol=1);
> 
> ##Likelihood
> 
> ##----------
> 
> Lvec <- matrix(0,nrow=nx1,ncol=1)
> 
> for (i in 1:ny)
> 
> {
> 
> Lvec[i] <- nb_L3(parameters, y[i],x1[i],x2[i])
> 
> LL <- -sum(Lvec)
> 
> }
> 
> LL
> 
> }
> 
> 	[[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