Why am I getting the message "Error in fn(par, ...) : unused argument (data = list(c...."?

84 views Asked by At

I am trying to use 'optim' but get the error message:

Error in fn(par, ...) : unused argument (data = list(c(0.095332651338155, 0.147662379627106, 0.185281561262674, 0.3850592427471, 0.184463315955858, 0.357989394826069, 0.290168101387964, 0.270670186833919,....

My code is:

Opt_S <- function(p0) {k=p0[1]; Fr=p0[2];
ssq <-0
for (z in 1:33){
Sest[z] <- k/(1-(sel_sarWC[z]/Fr))
ssq <- ssq+log((sel_smthpt[z]-Sest[z])^2)
 }
}
p0 <- c(-1,0.5)
xxx <- optim(p0, fn=Opt_S, data=sarWCdf)

The function runs on its own without a problem but when trying to run 'optim', I encounter this problem. I have tried different options for specifying the initial values for the parameters to be optimized but get the same result. The approach for this shown above is taken from the answer to a similar question posted on stack overflow in 2018 but it hasn't solved my problem.

0

There are 0 answers