Skip to content

Instantly share code, notes, and snippets.

@RottenFruits
Last active May 6, 2018 05:50
Show Gist options
  • Save RottenFruits/c714d7354aa867aa15e4d9664871be59 to your computer and use it in GitHub Desktop.
Save RottenFruits/c714d7354aa867aa15e4d9664871be59 to your computer and use it in GitHub Desktop.
gretaパッケージを活用したベイズモデリングの紹介
# library
library(greta)
library (bayesplot)
library(tidyverse)
# data
y <- as_data(iris$Sepal.Length)
Species <-as.numeric(iris$Species)
# variables and priors
mu <- normal(0, 10, dim = 3)
sd <- uniform(0, 10, dim = 3)
# operations
mean <- mu[Species]
sd_species <- sd[Species]
# likelihood
distribution(y) <- normal(mean, sd_species)
# defining the model
m <- model(mu, sd)
# plotting
plot(m)
# sampling
draws <- mcmc(m, n_samples = 1000)
summary(draws)
#result plot
mcmc_trace(draws)
mcmc_intervals(draws)
# library
library(greta)
library (bayesplot)
library(tidyverse)
# data
x <- as_data(iris$Petal.Length)
y <- as_data(iris$Sepal.Length)
Species <-as.numeric(iris$Species)
n_species <- length(unique(Species))
# variables and priors
mu <- normal(0, 10, dim = n_species)
sd <- uniform(0, 10, dim = n_species)
coef <- normal(0, 10, dim = n_species)
# operations
mean <- mu[Species] + coef[Species] * x
sd_species <- sd[Species]
# likelihood
distribution(y) <- normal(mean, sd_species)
# defining the model
m <- model(mu, sd, coef)
# plotting
plot(m)
# sampling
draws <- mcmc(m, n_samples = 1000)
summary(draws)
#result plot
mcmc_trace(draws)
mcmc_intervals(draws)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment