# Brms custom skew\_generalized\_t family

**URL:** <https://discourse.mc-stan.org/t/brms-custom-skew-generalized-t-family/28483>\
**Category:** Modeling\
**Tags:** brms\
**Created:** [August 9, 2022, 11:35am UTC](https://discourse.mc-stan.org/t/brms-custom-skew-generalized-t-family/28483 "2022-08-09T11:35:25Z")\
**Posts on this page:** 1\
**Showing post:** 20

<div class="post-metadata">

**Author:** ![sea83](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@sea83](https://discourse.mc-stan.org/u/sea83)\
**Post date:** [April 7, 2023, 12:31am UTC](https://discourse.mc-stan.org/t/brms-custom-skew-generalized-t-family/28483/20 "2023-04-07T00:31:36Z")

</div>

So I’ve updated the `posterior_epred_skew_generalized_t()` function, and now I am able to use some of the convenience functions in brms, however the conditional effects plot looks quite bizarre and I’m not sure if it’s because of how I coded the `posterior_epred_skew_generalized_t()` function.

This is the new function:

```plaintext
posterior_epred_skew_generalized_t <- function(prep) {
  mu <- brms::get_dpar(prep, "mu")
  sigma <- brms::get_dpar(prep, "sigma")
  lambda <- brms::get_dpar(prep, "lambda")
  p <- brms::get_dpar(prep, "p")
  q <- brms::get_dpar(prep, "q")
  
  ndraws <- prep$ndraws
  ns <- nrow(mu)
  
  # Use apply() to generate the epred matrix
  epred <- apply(cbind(mu, sigma, lambda, p, q), 1, function(x) {
    rskew_generalized_t(ndraws, x[1], x[2], x[3], x[4], x[5])
  })
  
  return(epred)
}

```

And this is my model:

```plaintext
init_fun <- function() {
  list(mu = rnorm(1, 0, 1),
       sigma = runif(1, 0.1, 1),
       lambda = runif(1, -0.99, 0.99),
       p = runif(1, 1, 10),
       q = runif(1, 1, 10))
}

SPT_rain_model <- brm(bf(SPT ~ rain + (rain | tag)), 
                      data = data[complete.cases(data[,c("rain", "SPT")]),],
                      save_pars = save_pars(all = TRUE),
                      iter = 5000,
                      init = init_fun,
                      prior = c(
                        prior(student_t(3,580, 70), class = Intercept,),
                        prior(student_t(3,0, 20), class = b),
                        prior(student_t(3,0, 20), class = sd),
                        prior(cauchy(0, 25), class = sigma),
                        prior(uniform(-0.99, 0.99), class = lambda, lb = -0.99, ub = 0.99),
                        prior(exponential(.1), class = p, lb = 2),
                        prior(exponential(.1), class = q, lb = 2)
                        ),
                      family = skew_generalized_t, stanvars = stanvars,
                      refresh = 1,
                      backend = "cmdstanr",
                      threads = threading(2),
                      control = list(max_treedepth = 10, adapt_delta = .9))

```

And here is the conditional effects plot

 ![test student t dist plot](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/6/1/61c4b519b66bbb3e4bb2ebd4cc47b02030fd74b7.png)

I tried transposing the epred matrix, thinking maybe that was my issue, but things still look jagged.

```plaintext
posterior_epred_skew_generalized_t <- function(prep) {
  mu <- brms::get_dpar(prep, "mu")
  sigma <- brms::get_dpar(prep, "sigma")
  lambda <- brms::get_dpar(prep, "lambda")
  p <- brms::get_dpar(prep, "p")
  q <- brms::get_dpar(prep, "q")
  
  ndraws <- prep$ndraws
  ns <- nrow(mu)
  
  # Use apply() to generate the epred matrix
  epred <- t(apply(cbind(mu, sigma, lambda, p, q), 1, function(x) {
    rskew_generalized_t(ndraws, x[1], x[2], x[3], x[4], x[5])
  }))
  
  
  return(epred)
}

```

 ![testplot2](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/5/5/554fa6f191ecf2ea43722770cac694c5b5e1e518.png)

Does anyone have any advice as to how I can improve this?

Also, I am posting to this thread because these are the people who I know to be interested in this distribution and who might find these functions helpful. Let me know if it’s better that I create a new thread.

---

_[View the full topic](https://discourse.mc-stan.org/t/brms-custom-skew-generalized-t-family/28483)._
