rjags

Expected value command R and JAGS


Assuming this ís my Bayesian model, how can i calculate the expected value of my Weibull distribution? Is there a command for finding the expected value of the Weibull distribution in R and JAGS? Thanks

model{  
#likelihood function  
for (i in 1:n)   
    {  
        t[i] ~ dweib(v,lambda)#MTBF    

        }    

#Prior for MTBF  
v ~ dgamma(0.0001, 0.0001)   
lambda ~ dgamma(0.0001, 0.0001)     
  }  

  #inits
list(v=1, lambda=1,mu=0,tau=1)

#Data
list(n=10, t=c(5.23333333,8.95,8.6,230.983333,1.55,85.1,193.033333,322.966667,306.716667,1077.8)

Solution

  • The mean, or expected value, of the Weibull distribution using the moment of methods with parameters v and lambda, is:

    lambda * Gamma(1 + 1/v)

    JAGS does not have the Gamma function, but we can use a work around with a function that is does have: logfact. You can add this line to your code and track the derived parameter exp_weibull.

    exp_weibull <- lambda * exp(logfact(1/v))

    Gamma is just factorial(x - 1), so the mean simplifies a bit. I illustrate below with some R functions how this derivation is the same.

    lambda <- 5
    v <- 2
    
    mu_traditional <- lambda * gamma(1 + 1/v)
    mu_logged <- lambda * exp(lfactorial(1/v))
    identical(mu_traditional, mu_logged)
    [1] TRUE
    

    EDIT: It seems like JAGS also has the log of the Gamma distribution as well: loggam. Thus, another solution would be

    exp_weibull <- lambda * exp(loggam(1 + 1/v))