[R] Using optimize with array variables

Vik harryhooddude at gmail.com
Thu Oct 1 18:16:39 CEST 2009


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)}

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!




More information about the R-help mailing list