R – How to Sample from a Shifted and Scaled Student-t Distribution with Specified Mean and SD

brmspriorrsimulationt-distribution

I'm currently building some Bayesian models with the brms package and the default intercept prior is student_t(3, 0, 6.3) and so I'm looking to visualise the prior predictive distribution from this prior. Typically I would just use functions like rnorm or unif depending on the distribution; however, the rt function only takes n and df arguments and I cannot specify an SD of 6.3.

How can I sample from this distribution?

Best Answer

While that $6.3$ seems not to mean the standard deviation in Stan, if that's what you want to do...

...do it in multiple steps.

  1. Simulate from the t-distribution with the appropriate degrees of freedom $\nu$, using rt.

  2. Divide by the population standard deviation, $\sqrt{\frac{ \nu }{ \nu-2 }}$. Now the population standard deviation is $1$.

  3. Multiply by your desired standard deviation.

  4. Add your desired mean, since the population mean is $0$.

You can combine these steps in one function.

rt_modified <- function(N, nu, mu, standard_dev){
    x1 <- rt(N, nu) # 1
    x2 <- x1/sqrt(nu/(nu-2)) # 2
    x3 <- x2 * standard_dev # 3
    x4 <- x3 + mu # 4
    return(x4)
}
Related Question