Problem with using optim


#1

I am working on an R code and I have been facing problem while using function optim.
I get an error "Error in optim(lambda = lambda.init, cost, phi = phi, Sd = Sd, method = "CG") :
cannot coerce type 'closure' to vector of type 'double' "
i am not sure why. I have been checking my code again and again but cannot find the mistake. Any help will be appreciated.
Thanks in advance


#2

Could you please turn this into a self-contained reprex (short for reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.

install.packages("reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

For pointers specific to the community site, check out the reprex FAQ.


Is it possible to save a template answer somewhere on this site?
#3

Hey,
I was doing reprex but there is a tiny bit of problem, I am importing data in R and it doesn't seem to work that well for loading the data. What should I do to share my issue here? I am using R for first time so I am not confident with the things to do.
Kindly help me out here

Thanks in advance


#4

Please find attached the reprex output of the code I am trying to run. Any help will be appreciated.


#5

When you run reprex the generated markdown (which you have a screenshot of, above) will either be on your clipboard, or in a markdown file, which you can copy and paste directly into the community site here.

(Jenny shows basic usage in about two minutes, if you prefer video)


#6

(I’m assuming you are using stats::optim. There are actually other functions with that name in other packages — pasting in your full reprex output would clear up this point!)

From the documentation for optim():

optim(par, fn, gr = NULL, …,
      method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN",
                 "Brent"),
      lower = -Inf, upper = Inf,
      control = list(), hessian = FALSE)

This tells us that:

  • The first argument (par) must be the initial values to optimize over
  • The second argument (fn) must be the function to optimize
  • The third argument is optional with a default value of NULL
  • Then come the ...("dots") where you supply any other arguments for the function you passed as fn
  • After the ... is the method parameter, which (since it follows ...) must be supplied with a name

You seem to be calling optim() with:

optim(lambda = lambda.init, cost, phi = phi, Sd = Sd, method = "CG")

Is lambda.init meant to be the initial values you want to optimize over? If so, you need to either pass it by position (no parameter name, must be the first argument), or pass it with the correct parameter name of par:

# Pass `par` by position
optim(lambda.init, cost, phi = phi, Sd = Sd, method = "CG")

# If you pass `par` by name it can appear out of order,
# but note that some people may find this style a bit 
# confusing! (wait, what's the first argument?)
optim(cost, par = lambda.init, phi = phi, Sd = Sd, method = "CG")

For more background on how to pass arguments into R functions, this chapter of Roger Peng's R Programming for Data Science is a good place to start: https://bookdown.org/rdpeng/rprogdatascience/functions.html


#7

Thanks, it seems to work.