[R] apply

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Sat Jan 14 00:15:51 CET 2006


In addition the error message explicitly says:

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

And from the objective function we see

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

The last line should be replaced by:

sum(y)

rather than "return(y)". See ?optim, ?apply, and the reference Bert 
gives below.

HTH,

--sundar


Berton Gunter wrote:
> You are not calling apply() properly. Please read relevant reference
> material carefully. You might also wish to pick up one of the several good
> books on R (check CRAN website) -- I like V&R's S PROGRAMMING.
> 
> I did not go through your example in detail, but your apply() call should be
> of the form
> 
> apply(d,2,function(x)optim(x,...))
> 
> You may or may not get into scoping problems with the opt1 argument and have
> to pass it in explicitly -- I can't remember how things work with optim. 
> 
> -- Bert Gunter
> Genentech Non-Clinical Statistics
> South San Francisco, CA
>  
> "The business of the statistician is to catalyze the scientific learning
> process."  - George E. P. Box
>  
>  
> 
> 
>>-----Original Message-----
>>From: r-help-bounces at stat.math.ethz.ch 
>>[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of 
>>Guenther, Cameron
>>Sent: Friday, January 13, 2006 11:03 AM
>>To: R-help at r-project.org
>>Subject: [R] apply
>>
>>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
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide! 
>>http://www.R-project.org/posting-guide.html
>>
> 
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list