-2

when we create a function why we use -(minus) in return, in R program see this example

f <- function(pars) {
  L <- (n*log(pars[1]))+(n*pars[1]*log(T1))+(n*pars[1]*log(pars[2]))-
       (n*log((T1^pars[1])-(pars[2]^pars[1])))- ((pars[1]+1)*sum(log(pars[2]+x)))
  return(-L)  
}

here why we use (-L) in return what if I use return (L)

The sourse of example Error in f(x, ...) : argument "x" is missing, with no default in nlm

Manal
  • 17
  • 4
  • If you use return(L) then you'll get L instead of -L. I think you're over thinking this one. – Dason Sep 10 '17 at 21:43
  • I confused, Is this mandatory in R to return when my function is L or not because I see a lot of codes used minus when the function has not minus sign – Manal Sep 10 '17 at 21:52
  • 2
    Usually they do this with the aim of `maximization`. When optimizing functions in R the default set up of the optimizing functions is usually to minimize. A Short cut to this is just to use a `-` sign in the log likelihood function which on being minimized will in turn be maximized. – Onyambu Sep 10 '17 at 22:00

2 Answers2

5

Usually they do this with the aim of maximization. When optimizing functions in R the default set up of the optimizing functions is usually to minimize. A Short cut to this is just to use a - sign in the log likelihood function which on being minimized will in turn be maximized. Although not necessary. You can be able to use the controls within the optimizing functions to indicate whether you need to maximize or minimize your log likelihood function

Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • Is it useful when i use it Bayesian approach or only for classical approach? +1 very thanks – Manal Sep 10 '17 at 22:52
  • Any optimization problem can use a negative sign as long as you understand the underlying principles – Onyambu Sep 10 '17 at 23:07
4

In this case, you use a minus sign because you a create a function which returns -L. The minus sign is not mandatory in R functions in general: you could have also written

L = -L
return(L) 
lebelinoz
  • 4,890
  • 10
  • 33
  • 56
  • what is this mean (create a function which returns -L)? This function does not contain minus sign – Manal Sep 10 '17 at 21:47
  • @Manal Yes it *does* contain a minus sign. In the previous line of code the value of `L` has become the symmetric of what it was. – Rui Barradas Sep 10 '17 at 23:24