I am working through the survivability analysis shown here. My specific question is regarding the grid approximation method, implemented in the following R
code:
# function to get log-likelihood of the data for a given shape & scale pair
grid_function <- function(shape, scale) {
dweibull(data_tbl$fatigue_duration, shape = shape, scale = scale, log = T) %>%
sum()
}
# set up grid of possible shape, scale parameters
n <- 100
shape_grid <- seq(1, 3, length.out = n)
scale_grid <- seq(60, 130, length.out = n)
two_param_grid <- expand_grid(shape_grid, scale_grid)
# map the grid_function over all candidate parameter pairs
# multiply LL by prior and convert to probability
full_tbl <- two_param_grid %>%
mutate(log_likelihood = map2(shape_grid, scale_grid, grid_function)) %>%
unnest() %>%
mutate(
shape_prior = 1,
scale_prior = 1
) %>%
mutate(product = log_likelihood + shape_prior + scale_prior) %>%
mutate(probability = exp(product - max(product)))
I don't understand the last two lines creating the product and probability values in the table.
- Why are we adding the shape/scale prior to the PDF value?
- Why do we use
product - max(product)
in the probability calculation?
My understanding is that we use the log values to avoid datatype precision errors, but it's not clear to me why we don't just take the PDF log-likelihood values directly and instead use these operations. Does this approach change if we use different priors (e.g., non-flat priors)?