Finding the exact percentile

Hi. I am trying to where in the predictive distribution our observation is. We have 13 observations and 13 predictive distributions. We know the value of each observation, but we want to find which percentile that specific value has for each observation. How can I find with r code the percentile of a specific value? And how can I implement it into my for loop.

kva_val95 <- numeric(length(y))
kva_val5 <- numeric(length(y))
rmse_val <- numeric(length(y))
for(i in 1:length(x1)){
  x1_new <- x1[i]
  x1_loo <- x1[-i]
  x2_new <- x2[i]
  x2_loo <- x2[-i]
  y_loo <- y[-i]
  stan_data <- list(N = length(y_loo), x1 = x1_loo, x1_new = x1_new, x2 = x2_loo, x2_new = x2_new, y = y_loo, nu = nu)
  # Modell
  mod <- stan(file = "stan_model1.stan", data = stan_data)
  # Get predictive distribution
  y_pred <- extract(object = mod, pars = "y_new")$y_new
  E_predicted <- mean(y_pred)
  # RMSE
  rmse_val[i] <- (E_predicted - y[i])^2
  kva_val5[i] <- quantile(y_pred, probs = 0.05)
  kva_val95[i] <- quantile(y_pred, probs = 0.95)

Appreciate if someone have any input on this issue.

1 Like


so, you have 13 obs → let’s consider ith obs → obs_i

Do you want to find what quantile obs_i is against the predictive distribution for obs_i (pred_i)? I think ecdf works for this

1 Like