thank you very much for your response! But I have now another problem: optim( ) gives me the exactly same initial values for the 4 parameters. I have tried many initial values, and it always didnt change them. opt$convergence = 1. I increased the number of iterations, but the problem is still there. I also made sure that the function doesnt return "NAN" or "INF" values... Does anyone have any idea where the problem could come from ? Many thanks. > To: r-h...@stat.math.ethz.ch > From: bbol...@gmail.com > Date: Tue, 12 Apr 2011 17:24:05 +0000 > Subject: Re: [R] question about optim > > chirine wolley <wolley.chirine <at> hotmail.com> writes: > > > Dear R-users, I would like to use optim( ) to minimize a function > > which depends on 4 parameters: 2 vectors, a scalar, and a matrix. > > And I have a hard to define the parameters at the beginning of the > > function, and then to call optim. Indeed, all the examples I have > > seen dont treat cases where parameters are not all real. Here is my > > code, it doesnt work but its just to show you where is exactly my > > problem: > > > suppose the dimensions of X are (Xc,Xr) > and the dimensions of x3 (I can't easily figure out what they should > be) are (m,n) > > > g=function(x,matrice) > > { > > x1 = x[1:Xc] # 1st parameter x1 is a vector > > x2 = x[Xc+1] # 2nd parameter x2 is real > x3 <- matrix(x[(Xc+2):(Xc+1+m*n)],nrow=m,ncol=n) > ## be careful about column- vs row-encoding, possibly use byrow=TRUE > > x4 <- x[(Xc+1+m*n+1):length(x)] #4th paramater is a vector > > > res1=rep(0,nrow(X)) > > res2=matrix(0,nrow=nrow(X),ncol=ncol(Y)) > > for (i in 1:nrow(X)) > > { > > res1[i]=log(1/(1+exp(-t(x1)%*%X[i,]-x2))) > > for (t in 1:ncol(Y)) > > { > > res2[i,t]=log(((1+exp(-t(x3[,t])%*%X[i,]-x4[t]))/(sqrt(2*pi)))* > > (exp(-0.5*((1+exp(-t(x3[,t])%*%X[i,]-x4[t]))*(1-Y[i,t]))^(2)))) > > } > > } > > sum(res1)+sum(res2) # the function to minimize > > } > > > > to call optim(), concatenate reasonable initial guesses for your > parameters. (I have no idea what 'c' is in your code below ...) > > > opt=optim(c(alpha[,c+1],beta[c+1],w,gamma),g) > > ### and how can we call optim ??? [[alternative HTML version deleted]]
______________________________________________ R-help@r-project.org 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.