-1

Below is the code I have. It works for primitive functions, such as sin. However, when using a function called gllik, it returns an error in f(y0): unused argument (y0). I'm not sure how to correct this.

newton_search2 <- function(f, h, guess, conv=0.001) {
   y0 <- guess
   N = 100
   i <- 1; y1 <- y0
   p <- numeric(N)
   while (i <= N) {
      make_derivative <- function(f, h) {
       (f(y0 + h) - f(y0 - h)) / (2*h)
       }
    y1 <- (y0 - (f(y0)/make_derivative(f, h)))
    p[i] <- y1
    i <- i + 1
    if (abs(y1 - y0) < conv) break
    y0 <- y1
 }
 return (p[(i-1)])
}

The gllik function is as follows:

x <- rgamma(n=30, shape=4.5)
gllik <- function() {
 s <- sum(log(x))
 n <- length(x)
 function(a) {
   (a - 1) * s - n * lgamma(a)
   }
 }

The code I used was:

newton_search2(gllik, 0.001, mean(x), conv = 0.001)

I'm not sure how to fix the error or get the correct answer which is supposed to be 4.5 (the maximum liklihood estimate of a).

user114634
  • 11
  • 4
  • Your code is not commented and your question is not a real question. What is the code that is returning an error? – MaZe Jul 13 '15 at 15:18
  • Oh, sorry. I forgot to include that. The code I put in is newton_search2(gllik, 0.001, mean(x), conv = 0.001) and then I get an error saying that y0 is an unused argument. I'm not sure how to make this work. – user114634 Jul 13 '15 at 15:20
  • Unless I misunderstand, gllink isn't a mathematical function. It's a function which is returning a function. It works if you use: newton_search2(gllik, 0.001, mean(x), conv = 0.001) – Mhairi McNeill Jul 13 '15 at 15:36

1 Answers1

3

The problem is that gllik does not take any arguments. Furthermore, it returns a function and not a value.

Perhaps what you want to to is the following?

gllik <- function(a) {
  s <- sum(log(x))
  n <- length(x)
  return((a - 1) * s - n * lgamma(a))
}

EDIT: An alternative solution is to just use the returned function. While this type of construction is often elegant, it does seem like overkill in this case:

newton_search2(gllik(), 0.001, mean(x), conv = 0.001)
Lars Lau Raket
  • 1,905
  • 20
  • 35
  • I believe that gllik should return a numeric value(s). I'm not sure how to do that. – user114634 Jul 13 '15 at 15:40
  • Maybe think about putting all of the variables you need in gllik. scoping allows you to not, but maybe make it a bit more explicit. or a function(a,...) – Steve Bronder Jul 13 '15 at 15:43
  • That worked. However, it gave me double the answer I was looking for. I think I have to take the derivative twice somehow. – user114634 Jul 13 '15 at 15:52
  • This seems to be a problem with your optimization method. Compare with: `optim(1, gllik, method = 'Brent', lower = 0, upper = 100, control = list(fnscale = -1))` – Lars Lau Raket Jul 13 '15 at 16:02
  • When I knit the R Markdown file to HTML, this code gives me different answers each time I knit it. – user114634 Jul 14 '15 at 13:09
  • @user114634: That is probably because you generate new random data every time? You can use set.seed(1) to generate the same random data every time. – Lars Lau Raket Jul 14 '15 at 13:20
  • What if I want the search to go on the first and second derivative of gllik()? Would I need to fix the while loop in my newton_search2? – user114634 Jul 14 '15 at 14:01