[R] how to do a IF ELSE in a matrix format

Hey Sky heyskywalker at yahoo.com
Tue Aug 3 19:02:41 CEST 2010


question again.

here is part of my original code for my research question. originally I used a 
loop with matrix form to do the work and it works (though looks ugly). I have 
tried the methods mentioned in the above posts and thought I did the same thing. 
but the new code does not work. the error message is:

Error in optim(guess, myfunc, data = mydata, method = "BFGS") : 
initial value in 'vmmin' is not finite

which is the same error message if I have set up wrong numbers of estimated 
parameters. but I have changed nothing but just this part. the command I use is 
optim() with a self-writen function for maximum likelihood estimation.


here is the part of my originall code. n is the number of observations:

for(j in 1:n) {
    if (edu[j,i]==1) lia[j,i]=1/v8[j,i]
    if (wrk[j,i]==1) lia[j,i]=exp(vbar2[j,i])/v8[j,i]
}

and I have tried:
lia[,i]<-ifelse(edu[,i]==1,1/v8[,i],0)
lia[,i]<-ifelse(wrk[,i]==1,exp(vbar2[,i])/v8[,i],0)

any suggestions? thanks a lot for every of them.

Nan
from Montreal





More information about the R-help mailing list