[R] apply

Guenther, Cameron Cameron.Guenther at MyFWC.com
Fri Jan 13 20:03:17 CET 2006


Hello,
I have a dataset d which is
 >d
         pop     catch 
1   66462.01 10807.757 
2   87486.73 46257.885 
3   57211.64  9345.058 
4   71321.62  4892.868 
5  100024.89 27334.248 
6  104504.91 48535.092 
7   95295.51 39348.195 
8   93737.35 34343.489 
9   89375.05 28750.743 
10  95312.65 30755.064 
11 100888.17 55404.370 
12  84790.23 37751.074 
13  81129.82 29277.443 
14  69797.09 21500.398 
15  61690.34 18199.664 
16  60671.08 19349.051 
17  62852.57 16300.982 
18  90646.32 34793.148 

And a function opt1:

opt1 <- function (x) {
  Z <- x + M
  c.hat <- (x/Z)*pop*(1-exp(-Z))
  y <- (catch - c.hat)^2
  return(y)
  }

And define M = 0.25 
For each row I want to return a value F that is a minimization of the
opt1 function

I have tried many variations of:
d$F<-apply(d,2,optim(0.3,opt1,method="BFGS")
and even:
For (I in 1:length(pop))apply(d,2,optim(0.3,opt1,method="BFGS"))

Every time I get the same error message

Error in optim(0.3, opt1, method = "BFGS") : 
        objective function in optim evaluates to length 18 not 1

So the apply function is returning 18 values of F which I want but the
function only wants to return 1 value.

Any Suggestions.
Thanks,
Cam




More information about the R-help mailing list