Ross
Ross

Reputation: 521

optim() not giving correct minima

I'm using optim to try and find the critical region in a binomial test, however after a certain sample size it fails to converge on the correct value.

Seems like the function is well behaved so not sure why it stops working at this point.

N <- 116

optim(1, function(x) abs(1 - pbinom(x, N, 0.1) - 0.05), method = "Brent", lower = 1, upper = N)

The optim function as above works for N < 116.

Upvotes: 1

Views: 91

Answers (2)

Ben Bolker
Ben Bolker

Reputation: 226182

You should probably use the built-in qbinom function, which computes specified values of the quantile (inverse CDF) function for the binomial distribution: it works fine for any reasonable value of N.

N <- 116
qbinom(0.95, size = N, prob = 0.1)

The function is not well-behaved from an optimization point of view: as explained here, it is piecewise constant.

Upvotes: 1

chinsoon12
chinsoon12

Reputation: 25225

The gradient at your starting point is almost 0 and the algorithm cannot move to the next best solution.

One way is to use another starting point:

optim(0.1*N, function(x) abs(1 - pbinom(x, N, 0.1) - 0.05), method = "Brent", lower = 1, upper = N)

or to use optimize since its one dimensional:

optimize(function(x) abs(1 - pbinom(x, N, 0.1) - 0.05), c(1,N))

Upvotes: 0

Related Questions