Skip to content

Bayesian analysis using Stan, including Bayesian dashboard tool

License

Notifications You must be signed in to change notification settings

mathematica-mpr/easybayesian

Repository files navigation

Why should I use it?

This package makes using STAN easy!

How do I use it?

Create some fake data:

set.seed(9782)
library(dplyr)
N <- 1000
df1 <- data.frame(
  x1 = rnorm(n = N, mean = 10, sd = 3),
  x2 = runif(n = N, min = 0, max = 10),
  c = sample(LETTERS, size = N, replace = T)
  ) %>% mutate(Tr = ifelse(c %in% c("A","E","I","O","U"), yes = 1, no = 0)) %>%
  mutate(y = 0.5*x1 + 0.75*x2 + 0.5*Tr + rnorm(N,0,1))

Run stan lm

library(easybayesian)
library(rstan)
lm1 <- stanlm(formula = y ~ x1 + x2 + Tr, data = df1)

Regression table

regtbl(lm1, type = "html", caption = "")
Rhat n\_eff Model 1
x1 1.00 2866 0.50\*
\[0.48; 0.52\]
x2 1.00 2957 0.76\*
\[0.73; 0.78\]
Tr 1.00 2930 0.59\*
\[0.43; 0.75\]
Constant 1.00 2914 -0.11
\[-0.34; 0.13\]
N 1000
R&\#770 log-posterior 1.00
Neff log-posterior 1519
\* outside the 95% credible interval.
Rhat is the potential scale reduction factor on split chains (at convergence, Rhat=1).
n\_{eff} is a crude measure of effective sample size.
The log posterior quantifies the combined posterior density of all model parameters.
### Goodness of fit
gof.table(lm1, caption = "My caption!", type="html")
Rhat n\_eff
x1 1.00 2866.37
x2 1.00 2957.42
Tr 1.00 2929.98
Constant 1.00 2914.08
### Plot
p <- posteriorplot(model = lm1, parameter = "Tr", cutoff = 0.4, credibleIntervalWidth = .95)

Interpret

interpret(model = lm1, name = "Tr", cutoff = 0)
#> [[1]]
#> [1] "There is a 95% probability that the true impact of the intervention is between 0.43 and 0.75 units."
#> 
#> [[2]]
#> [1] "There is a 100% probability that the intervention increases the outcome by 0 units or more."

Clustered

lm1 <- stanlm(formula = y ~ x1 + x2 + Tr, cluster = "c", data = df1)

Regression table

regtbl(lm1, type = "html", caption = "")
Rhat n\_eff Model 1
x1 1.00 4000 0.50\*
\[0.48; 0.52\]
x2 1.00 4000 0.76\*
\[0.73; 0.78\]
Tr 1.00 3159 0.59\*
\[0.42; 0.76\]
Constant 1.00 4000 -0.11
\[-0.35; 0.13\]
N 1000
Clusters 26
R&\#770 log-posterior 1.00
Neff log-posterior 1302
\* outside the 95% credible interval.
Rhat is the potential scale reduction factor on split chains (at convergence, Rhat=1).
n\_{eff} is a crude measure of effective sample size.
The log posterior quantifies the combined posterior density of all model parameters.
### Goodness of fit
gof.table(lm1, caption = "My caption!", type="html")
Rhat n\_eff
x1 1.00 4000.00
x2 1.00 4000.00
Tr 1.00 3158.52
Constant 1.00 4000.00
### Plot
p <- posteriorplot(model = lm1, parameter = "Tr", cutoff = 0.4, credibleIntervalWidth = .95)

Interpret

interpret(model = lm1, name = "Tr", cutoff = 0)
#> [[1]]
#> [1] "There is a 95% probability that the true impact of the intervention is between 0.42 and 0.76 units."
#> 
#> [[2]]
#> [1] "There is a 100% probability that the intervention increases the outcome by 0 units or more."

Dashboard

bayesiandashboard()

Bayesian Dashboard

How do I get it?

For now, this package is not public. But if you are here, I probably want to share it with you.

To install the package you can run devtools::install_github(repo = 'ignacio82/easybayesian', auth_token = '40748ac7538e4b47244a58cf9b1479d48e7bd531')

About

Bayesian analysis using Stan, including Bayesian dashboard tool

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published