[R] optimization

Prof J C Nash (U30A) nashjc at uottawa.ca
Sat Nov 16 22:26:30 CET 2013


There are lots of errors in your code. In particular, the optimization
routines do not like functions that ignore the parameters.

And you have not provided out or out1 to the optimizer -- they are
returned as elements of func(), but not correctly.

Please try some of the examples for optim or optimx to learn how to
structure your problem.

JN


On 13-11-16 06:00 AM, r-help-request at r-project.org wrote:
> Message: 19
> Date: Fri, 15 Nov 2013 09:17:47 -0800 (PST)
> From: IZHAK shabsogh <ishaqbaba at yahoo.com>
> To: "r-help at r-project.org" <r-help at r-project.org>
> Subject: [R] optimization
> Message-ID:
> 	<1384535867.58595.YahooMailNeo at web142506.mail.bf1.yahoo.com>
> Content-Type: text/plain
> 
> x1<-c(5.548,4.896,1.964,3.586,3.824,3.111,3.607,3.557,2.989,18.053,3.773,1.253,2.094,2.726,1.758,5.011,2.455,0.913,0.890,2.468,4.168,4.810,34.319,1.531,1.481,2.239,4.204,3.463,1.727)
> y<-c(2.590,3.770,1.270,1.445,3.290,0.930,1.600,1.250,3.450,1.096,1.745,1.060,0.890,2.755,1.515,4.770,2.220,0.590,0.530,1.910,4.010,1.745,1.965,2.555,0.770,0.720,1.730,2.860,0.760)
> x2<-c(0.137,2.499,0.419,1.699,0.605,0.677,0.159,1.699,0.340,2.899,0.082,0.425,0.444,0.225,0.241,0.099,0.644,0.266,0.351,0.027,0.030,3.400,1.499,0.351,0.082,0.518,0.471,0.036,0.721)
> k<-rep(1,29)
> x<-data.frame(k,x1,x2)
> 
> freg<-function(y,x1,x2){
>   reg<- rlm(y ~ x1 + x2 , data=x)
>   return(reg)
> }
> 
> 
>  func <- function(x1,x2,b){
>       fit<-freg(y,x1,x2)
>         b<-c(coef(fit))
>       dv<-1+ b[2]*x2^b[3]
>         dv1<-b[2]*x2^b[3]*log(x2)
>       out <- ( x1/(1+ b[2]*x2^b[3]))
>       out1<-  c(-x1*x2^b[3]/dv^2,-x1* dv1/dv^2)
>         return(list( out,out1))
>        }>
> optim(par=c(b[2],b[3]), fn=out, gr =out1,
>         method = c("BFGS"),
>         lower = -Inf, upper = Inf,
>         control = list(), hessian = T)
> can someone help me try running this code because i try many occasion but prove  abortive. the aim is
> to optimize the parameter of the model that is parameter estimates using optimization 
> 
> 	[[alternative HTML version deleted]]



More information about the R-help mailing list