I am relatively new to R, but I would like to use it find the values of b1 and b2 that maximize the following equation: (sum(cos(x3+2.33474-2(atan(b1*x1+b2*x2))))) Any help would be greatly appreciated.
"optim"? Josh Schmidt wrote:>I am relatively new to R, but I would like to use it find the values of b1 and >b2 that maximize the following equation: > >(sum(cos(x3+2.33474-2(atan(b1*x1+b2*x2))))) > >Any help would be greatly appreciated. > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >
>===== Original Message From Spencer Graves <spencer.graves at pdf.com> ====>Please "reply to all". Others on r-help may have more time than I do. >spencer graves > >Josh Schmidt wrote: > >>I have tried optim, but I keep getting an error message. I am quite new to >>this, so perhaps I am inputting something incorrectly? >> >> f<-function >>(max,b1,precip,b2){(sum(cos(theta+2.33474-2(atan(b1*max+b2*precip)))))} >> >>I used read.table to store the values for max, precip, and theta in those >>variables. >> >> >> >>>optim(10, f, gr = NULL, >>> >>> >>+ method = c(), >>+ lower = -Inf, upper = Inf, >>+ control = list(), hessian = FALSE) >> >>Error in sum(cos(theta + 2.33474 - 2(atan(b1 * max + b2 * precip)))) : >> attempt to apply non-function >>In addition: Warning message: >>one-diml optimization by Nelder-Mead is unreliable: use optimize in:optim(10,>>f, gr = NULL, method = c(), lower = -Inf, upper = Inf, >> >>Josh Schmidt >>Department of Biology and Wildlife >>University of Alaska Fairbanks >>474-7006 (office) >> >> >>Josh Schmidt Department of Biology and Wildlife University of Alaska Fairbanks 474-7006 (office)
Josh Schmidt wrote:> > I am relatively new to R, but I would like to use it find the values of b1 and > b2 that maximize the following equation: > > (sum(cos(x3+2.33474-2(atan(b1*x1+b2*x2))))) > > Any help would be greatly appreciated.Given b1 and b2 are scalars (we are minimizing f, hence maximizing -f): f <- function(b, x) -sum(cos(x3 + 2.33474 - 2*(atan(b[1]*x1 + b[2]*x2)))) optim(c(0, 0), f, x1=x1, x2=x2, x3=x3) Be careful: Do you know the solution is (a) unique and (b) without many local extrema? The function cos() is extremly dangerous for optimization!!! If you REALLY need it *and* you know optimization makes sense, you might want to give method "SANN" a try ... Uwe Ligges
I tried:> f<-function(b,x)-sum(cos(x1+2.33474-2*(atan(b[1]*x2+b[2]*x3)))) > optim(c(0, 0), f, x1=x1, x2=x2, x3=x3)and got this error. Error in fn(par, ...) : unused argument(s) (x1 ...) I am trying to solve for the circular-linear regression equation in Fisher (1993).>===== Original Message From Uwe Ligges <ligges at statistik.uni-dortmund.de>====>Josh Schmidt wrote:>> >> I am relatively new to R, but I would like to use it find the values of b1and>> b2 that maximize the following equation: >> >> (sum(cos(x3+2.33474-2(atan(b1*x1+b2*x2))))) >> >> Any help would be greatly appreciated. > > > >Given b1 and b2 are scalars (we are minimizing f, hence maximizing -f): > > f <- function(b, x) -sum(cos(x3 + 2.33474 - 2*(atan(b[1]*x1 + >b[2]*x2)))) > optim(c(0, 0), f, x1=x1, x2=x2, x3=x3) > > >Be careful: Do you know the solution is (a) unique and (b) without many >local extrema? >The function cos() is extremly dangerous for optimization!!! If you >REALLY need it *and* you know optimization makes sense, you might want >to give method "SANN" a try ... > >Uwe LiggesJosh Schmidt Department of Biology and Wildlife University of Alaska Fairbanks 474-7006 (office)
Josh Schmidt wrote:> I tried: > >>f<-function(b,x)-sum(cos(x1+2.33474-2*(atan(b[1]*x2+b[2]*x3)))) >>optim(c(0, 0), f, x1=x1, x2=x2, x3=x3)Sorry, should have been: f <- function(b, x1, x2, x3){ -sum(cos(x1+2.33474-2*(atan(b[1]*x2+b[2]*x3)))) } optim(c(0, 0), f, x1=x1, x2=x2, x3=x3) Uwe Ligges> and got this error. > > Error in fn(par, ...) : unused argument(s) (x1 ...) > > I am trying to solve for the circular-linear regression equation in Fisher > (1993). > > > >>===== Original Message From Uwe Ligges <ligges at statistik.uni-dortmund.de> > > ====> >>Josh Schmidt wrote: >> >>>I am relatively new to R, but I would like to use it find the values of b1 > > and > >>>b2 that maximize the following equation: >>> >>>(sum(cos(x3+2.33474-2(atan(b1*x1+b2*x2))))) >>> >>>Any help would be greatly appreciated. >> >> >> >>Given b1 and b2 are scalars (we are minimizing f, hence maximizing -f): >> >>f <- function(b, x) -sum(cos(x3 + 2.33474 - 2*(atan(b[1]*x1 + >>b[2]*x2)))) >>optim(c(0, 0), f, x1=x1, x2=x2, x3=x3) >> >> >>Be careful: Do you know the solution is (a) unique and (b) without many >>local extrema? >>The function cos() is extremly dangerous for optimization!!! If you >>REALLY need it *and* you know optimization makes sense, you might want >>to give method "SANN" a try ... >> >>Uwe Ligges > > > Josh Schmidt > Department of Biology and Wildlife > University of Alaska Fairbanks > 474-7006 (office) > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html