[R] question about optim

From: chirine wolley <wolley.chirine_at_hotmail.com>
Date: Tue, 12 Apr 2011 12:02:51 +0200

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:  

g=function(x,matrice)
{

x1 = x[1:ncol(X)]         # 1st parameter x1 is a vector
x2 = x[ncol(X)+1]        # 2nd parameter x2 is real
x3 = x(0,166,5) # 3rd parameter is a matrix ----> How should one write it ???
x4 = x[ncol(X)+1:ncol(X)+1+ncol(Y)] #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
}  

opt=optim(c(alpha[,c+1],beta[c+1],w,gamma),g) ### and how can we call optim ???  

Thank you in advance!                                                

        [[alternative HTML version deleted]]



R-help_at_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. Received on Tue 12 Apr 2011 - 10:24:26 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 12 Apr 2011 - 17:50:28 GMT.

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

list of date sections of archive