Thanks Charles, I have been able to change the function and to make it work. I have also gotten the same results using nlm().
Now the question I have (and that google has not helped on) is which optimization functions can I use for solving for two distinct variables instead of only one? Thanks again. On Thu, Oct 1, 2009 at 1:06 PM, Charles C. Berry <cbe...@tajo.ucsd.edu>wrote: > On Thu, 1 Oct 2009, Vik wrote: > > Hello, >> >> I am trying to figure out how to use optimize() with array variables >> as inputs. I have a for loop in the function definition: >> >> SS <- function(int,slo,x,y){ >> for(i in 1:length(x)) ((int+slo*x[i])-y[i])^2->squares[i] >> sum(squares)->>sum_squares >> output_txt = c ("The sum of squares is", sum_squares) >> print(output_txt, quote=FALSE)} >> >> > > Try running your code in a clean session. There is an error (and not the > one you quote below). > > > After you get rid of that error, try > > val <- SS ( 0.1, 1, 1, 1 ) > > then > > print( val ) > > The difficulty optimize is having should be apparent. > > In spite of the pressure you are under, you will need to slow down, write > readable code, and take the time to learn about writing functions in R > before you can hope to get anywhere with a problem like this. > > Read the posting guide. For a problem like this one, the 'Some' mentioned > below includes many of the people on this list who can provide useful, > knowledgeable answers: > > Some consider it good manners to include a concise signature > specifying affiliation > > > HTH, > > Chuck > > > p.s. do not use '<<-' or '->>' until you really, really know what it does > (which means you understand 'scoping')! > > Even assuming I make x and y single-integer variables, for example: >> >> optimize(SS, c(0,1), tol = 0.0001, x=1, y=1, slo=1) >> >> I get the error: >> >> Error in optimize(SS, c(0, 1), tol = 1e-04, x = 1, y = 1, slo = 1) : >> invalid function value in 'optimize' >> >> Not sure what that means. This also happens when x and y are defined >> as arrays. Does optimize require me to input values for all variables >> that are not being optimized (i.e. for n-1 of the inputs)? Why am I >> getting an error. >> >> I am new to R and under pressure to get some calculations done for my >> work. I'd appreciate an answer to the specific question - and I fully >> understand that I need to read up on R basics in more depth, which I >> am doing as best I can while trying to get this work done. Thanks! >> >> ______________________________________________ >> 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. >> >> > Charles C. Berry (858) 534-2098 > Dept of Family/Preventive > Medicine > E mailto:cbe...@tajo.ucsd.edu UC San Diego > http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 > > > [[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.