Monday, 15 August 2011

optimization - Can I pass more arguments to optimize() function in R -



optimization - Can I pass more arguments to optimize() function in R -

the optimize function designed handle 1 function argument understand it. function can depend on different input ranges of variables , parameters.

for example:

x.r<-rnorm(100) y<-function(x,p) -x^2+p*1/exp(x^3) optimize(y,upper=range(x.r)[2],lower=range(x.r)[1])

x.r. domain of x beingness passed y can alter sample sample. instead of using generic x, want able draw many x.r domains of values run y over. there way modify optimize function pass both y, function, , x it's domain variables optimize parameter(s) p?

with above error:

> optimize(y,upper=range(x.r)[2],lower=range(x.r)[1]) error in p * 1 : 'p' missing

edit: per suggestion below (which makes sense... but) tried.

x.r<-rnorm(100) p<-seq(-5,5,1) y<-function(x,p) -x^2+p*1/exp(x^3) optimize(y,upper=range(x.r)[2],lower=range(x.r)[1],p)

results was: error in p * 1 : 'p' missing

also,

> optimize(y,upper=range(x.r)[2],lower=range(x.r)[1],p,x.r) error in optimize(y, upper = range(x.r)[2], lower = range(x.r)[1], p, : invalid function value in 'optimize'

any other ideas?

edit 2: per comments below loop work, seems odd way approach it. i'm not sure if it's uniquely taking x.r domain business relationship either.

x.r<-rnorm(100) p<-seq(-5,5,1) y<-function(p=p,x=x.r) -x^2+p*1/exp(x^3) for(v in p){ print(optimize(y,upper=range(p)[2],lower=range(p)[1],p=v))} > for(v in p){ + print(optimize(y,upper=range(p)[2],lower=range(p)[1],p=v))} $minimum [1] -4.999944 $objective [1] -9.637547e+54 $minimum [1] -4.999944 $objective ...

i phone call optimise on p

x.r <- rnorm(100) p <- seq(-5,5,1) y<-function(x,p) -x^2+ p/exp(x^3) ll <- lapply(p,function(i) optimize(y,upper=range(x.r)[2],lower=range(x.r)[1],p=i)) ll[[which.min(sapply(ll,'[[','minimum'))]] $minimum [1] -2.14628 $objective [1] -98357.67

of course of study if maximum = true lastly becomes

ll[[which.max(sapply(ll,'[[','maximum'))]]

r optimization

No comments:

Post a Comment