0

I am trying to use R to calculate the marginal likelihood of a set of data (likelihood_N) with respect to the parameter v0. To do this, I created the following subfunctions:

#take in values of v and return array of repeating 1/40
prior_u= function(v){
    rep(1/40, length(v)}

likelihood=function(v0,data){
#turn dataframe to vectors
    Gmu=data[2]$V2
    v=data[1]$V1

    #for a specific value v0, calculate an array of theoretical values for Gmu
    Gteo=(1 - (1/g)*(sqrt(log(2)/pi)) * exp(- (log(2)*((v-v0)^2))/(g^2)))

    #final function = normal distribution of the theoretical vs real values
    return(dnorm(Gmu,Gteo,sd=s))
}

likelihood_N=function(v0,data){
#calculates likelihood of entire dataset and gives back its product 
    prod(likelihood(v0,data))
}

Now, to find the marginal likelihood, I need to integrate the function likelihood_N over all possible values of v0.

integrate(likelihood_N, 80, 120, data1, subdivisions = N)

However when I use theintegrate R built in function it always gives me the following error message:

Warning message in (v - v0): "longer object length is not a multiple of shorter object length"

Error in integrate(likelihood_N, start, stop, dataset_1, subdivisions = N): evaluation of function gave a result of wrong length

Regardless of the number of subdivisions N I choose.

I understand this has to do with the way I defined how the functions take in values of v0, since each function assumes only one value as parameter, but I don't really see how else I could define these functions to fix it (everything I try just ends up wielding the same error message). I'm really new to R so I don't know if there is maybe another integration function that would be more appropriate for this case.

Does anyone know how I can fix this?

Rye
  • 135
  • 6
  • 1
    Please show the code where you call `integrate()`. – Gregor Thomas Apr 22 '22 at 18:15
  • I've added it to the question and also some more context – Rye Apr 22 '22 at 18:19
  • 1. Fix your code, it's not working, 2. Read our [nice tutorial for minimal working examples](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Cheers! – jay.sf Apr 22 '22 at 18:56

0 Answers0