I am trying to integrate a very simple likelihood function (three Bernoulli trials) in R, but it seems that I have not understood how integrate function works. My code is as follows:
integrate(
function(theta) prod(
dbinom(
x = c(1,0,1), #x is the data: "success, fail, success"
size = 1, #Just one Bernoulli trial times the length of x.
prob = theta, #Since, this is a likelihood function, the parameter value is the variable
log = F #We use a likelihood instead of log-likelihood
)
),
lower = 0, #The parameter theta cannot be smaller than zero...
upper = 1 #...nor greater than one.
)
However, I get this error message:
Error in integrate(function(theta) prod(dbinom(x = c(1, 0, 1), size = 1, :
evaluation of function gave a result of wrong length
Now, why is the result of wrong length? The result is always of length 1, since the anonymous function uses prod function, which in turn creates a product of all the vector elements the function dbinom returns (this vector has the length of three, since its first argument x has length of three).
What the result should be then to be of right length?