## ----binomial-logit-----------------------------------------------------------
library(CMCMC)

set.seed(1)
n <- 100
x1 <- rnorm(n)
x2 <- rnorm(n)

eta <- -0.3 + 0.8 * x1 - 0.5 * x2
p <- 1 / (1 + exp(-eta))
y <- rbinom(n, size = 1, prob = p)

dat <- data.frame(y = y, x1 = x1, x2 = x2)

fit_binomial <- CMCMC::glm_cmcmc(
  y ~ x1 + x2,
  data = dat,
  family = binomial("logit"),
  k = 128,
  it = 100,
  covit = 10,
  prior_var = 100,
  seed = 1,
  verbose = TRUE
)

fit_binomial
summary(fit_binomial)
coef(fit_binomial)


## ----binomial-probit----------------------------------------------------------
fit_binomial_probit <- CMCMC::glm_cmcmc(
  y ~ x1 + x2,
  data = dat,
  family = binomial("probit"),
  k = 128,
  it = 100,
  covit = 10,
  seed = 1
)


## ----poisson-log--------------------------------------------------------------
library(CMCMC)

set.seed(2)
n <- 120
x1 <- rnorm(n)
x2 <- rnorm(n)

eta <- 0.2 + 0.4 * x1 - 0.3 * x2
mu <- exp(eta)
y <- rpois(n, lambda = mu)

dat <- data.frame(y = y, x1 = x1, x2 = x2)

fit_poisson <- CMCMC::glm_cmcmc(
  y ~ x1 + x2,
  data = dat,
  family = poisson("log"),
  k = 128,
  it = 100,
  covit = 10,
  prior_var = 100,
  seed = 2,
  verbose = TRUE
)

fit_poisson
summary(fit_poisson)
coef(fit_poisson)


## ----gaussian-identity--------------------------------------------------------
library(CMCMC)

set.seed(3)
n <- 120
x1 <- rnorm(n)
x2 <- rnorm(n)

mu <- 0.5 + 1.2 * x1 - 0.7 * x2
sigma <- 0.4
y <- mu + rnorm(n, sd = sigma)

dat <- data.frame(y = y, x1 = x1, x2 = x2)

fit_gaussian <- CMCMC::glm_cmcmc(
  y ~ x1 + x2,
  data = dat,
  family = gaussian("identity"),
  k = 128,
  it = 100,
  covit = 10,
  prior_var = 100,
  prior.override = list(
    sigma_prior = "uniform_sigma",
    sigma_upper = 1
  ),
  seed = 3,
  verbose = TRUE
)

fit_gaussian
summary(fit_gaussian)
coef(fit_gaussian)


## ----gaussian-log-------------------------------------------------------------
set.seed(4)
n <- 120
x1 <- rnorm(n)
x2 <- rnorm(n)

eta <- 0.2 + 0.15 * x1 - 0.1 * x2
mu <- exp(eta)
y <- mu + rnorm(n, sd = 0.2)

dat <- data.frame(y = y, x1 = x1, x2 = x2)

fit_gaussian_log <- CMCMC::glm_cmcmc(
  y ~ x1 + x2,
  data = dat,
  family = gaussian("log"),
  k = 128,
  it = 100,
  covit = 10,
  prior.override = list(
    sigma_prior = "exponential_sigma",
    sigma_upper = 1,
    sigma_tail_prob = 0.05
  ),
  seed = 4
)


## ----gaussian-inverse---------------------------------------------------------
set.seed(5)
n <- 120
x1 <- runif(n, -0.5, 0.5)
x2 <- runif(n, -0.5, 0.5)

eta <- 1.4 + 0.2 * x1 - 0.1 * x2
mu <- 1 / eta
y <- mu + rnorm(n, sd = 0.05)

dat <- data.frame(y = y, x1 = x1, x2 = x2)

fit_gaussian_inverse <- CMCMC::glm_cmcmc(
  y ~ x1 + x2,
  data = dat,
  family = gaussian("inverse"),
  k = 128,
  it = 100,
  covit = 10,
  prior.override = list(
    sigma_prior = "uniform_sigma",
    sigma_upper = 0.3
  ),
  seed = 5
)


## ----gamma-log----------------------------------------------------------------
library(CMCMC)

set.seed(6)
n <- 120
x1 <- rnorm(n)
x2 <- rnorm(n)

eta <- 0.3 + 0.4 * x1 - 0.2 * x2
mu <- exp(eta)
alpha <- 8
y <- rgamma(n, shape = alpha, rate = alpha / mu)

dat <- data.frame(y = y, x1 = x1, x2 = x2)

fit_gamma <- CMCMC::glm_cmcmc(
  y ~ x1 + x2,
  data = dat,
  family = Gamma("log"),
  k = 128,
  it = 100,
  covit = 10,
  prior_var = 100,
  prior.override = list(
    cv_prior = "uniform_cv",
    cv_upper = 1
  ),
  seed = 6,
  verbose = TRUE
)

fit_gamma
summary(fit_gamma)
coef(fit_gamma)


## ----gamma-tail-priors--------------------------------------------------------
fit_gamma_pc <- CMCMC::glm_cmcmc(
  y ~ x1 + x2,
  data = dat,
  family = Gamma("log"),
  k = 128,
  it = 100,
  covit = 10,
  prior.override = list(
    cv_prior = "exponential_cv",
    cv_upper = 0.75,
    cv_tail_prob = 0.05
  ),
  seed = 6
)

fit_gamma_half_cauchy <- CMCMC::glm_cmcmc(
  y ~ x1 + x2,
  data = dat,
  family = Gamma("log"),
  k = 128,
  it = 100,
  covit = 10,
  prior.override = list(
    cv_prior = "half_cauchy_cv",
    cv_upper = 0.75,
    cv_tail_prob = 0.05
  ),
  seed = 6
)


## ----output-methods-----------------------------------------------------------
coef(fit_binomial)
summary(fit_gaussian)

