Skip to content

Instantly share code, notes, and snippets.

@goldingn
Created September 28, 2017 01:24
Show Gist options
  • Save goldingn/cd676c508b917ada070f7705a1d95848 to your computer and use it in GitHub Desktop.
Save goldingn/cd676c508b917ada070f7705a1d95848 to your computer and use it in GitHub Desktop.
# fake data
n <- 1000
m <- 50
x <- matrix(rnorm(n * m), n, m)
b <- rnorm(m, 0, 2) * rbinom(m, 1, 0.2)
eta <- x %*% b
y <- rnorm(n, eta, 0.3)
library (greta)
# parameters & priors
alpha <- normal(0, 1)
sigma <- normal(0, 1, truncation = c(0, Inf))
tau <- cauchy(0, 5)
beta <- normal(0, tau, dim = m)
# model
mu <- alpha + x %*% beta
distribution(y) <- normal(mu, sigma)
# inference
m <- model(beta)
draws <- mcmc(m)
# plot
est <- summary(draws)[[1]][, "Mean"]
plot(est ~ b)
abline(0, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment