Loading [MathJax]/jax/output/HTML-CSS/jax.js

The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

3. Priors

1 Set-up

suppressPackageStartupMessages({
  library(bage)
  library(poputils)
  library(ggplot2)
  library(dplyr)
})  
plot_exch <- function(draws) {
  ggplot(draws, aes(x = element, y = value)) +
    facet_wrap(vars(draw), nrow = 1) +
    geom_hline(yintercept = 0, col = "grey") +
    geom_point(col = "darkblue", size = 0.7) +
    scale_x_continuous(n.breaks = max(draws$element)) +
    xlab("Unit") +
    ylab("") +
    theme(text = element_text(size = 8))
}          
plot_cor_one <- function(draws) {
  ggplot(draws, aes(x = along, y = value)) +
    facet_wrap(vars(draw), nrow = 1) +
    geom_hline(yintercept = 0, col = "grey") +
    geom_line(col = "darkblue") +
    xlab("Unit") +
    ylab("") +
    theme(text = element_text(size = 8))
}          
plot_cor_many <- function(draws) {
  ggplot(draws, aes(x = along, y = value)) +
    facet_grid(vars(by), vars(draw)) +
    geom_hline(yintercept = 0, col = "grey") +
    geom_line(col = "darkblue") +
    xlab("Unit") +
    ylab("") +
    theme(text = element_text(size = 8))
}          
plot_svd_one <- function(draws) {
  ggplot(draws, aes(x = age_mid(age), y = value, color = sexgender)) +
    facet_wrap(vars(draw), nrow = 1) +
    geom_line() +
    scale_color_manual(values = c("darkgreen", "darkorange")) +
    xlab("Age") +
    ylab("") +
    theme(text = element_text(size = 8),
          legend.position = "top",
          legend.title = element_blank())
}
plot_svd_many <- function(draws) {
  draws |>
    mutate(element = paste("Unit", element)) |>
    ggplot(aes(x = age_mid(age), y = value, color = sexgender)) +
    facet_grid(vars(element), vars(draw)) +
    geom_line() +
    scale_color_manual(values = c("darkgreen", "darkorange")) +
    xlab("Age") +
    ylab("") +
    theme(text = element_text(size = 8),
          legend.position = "top",
          legend.title = element_blank())
}

2 Exchangeable Units

2.1 Fixed Normal NFix()

2.1.1 Model

βjN(0,sd2)

2.1.2 All defaults

sd = 1

set.seed(0)

NFix() |>
  generate(n_element = 10, n_draw = 8) |>
  plot_exch()

2.1.3 Reduce sd

sd = 0.01

set.seed(0)

NFix(sd = 0.01) |>
  generate(n_element = 10, n_draw = 8) |>
  plot_exch()

2.2 Normal N()

2.2.1 Model

βjN(0,τ2)τN+(0,s)

2.2.2 All defaults

s = 1

set.seed(0)

N() |>
  generate(n_element = 10, n_draw = 8) |>
  plot_exch()

2.2.3 Reduce s

s = 0.01

set.seed(0)

N(s = 0.01) |>
  generate(n_element = 10, n_draw = 8) |>
  plot_exch()

3 Units Correlated With Neighbours

3.1 Random Walk RW()

3.1.1 Model

β1N(0,sd2)βjN(βj1,τ2),j=2,,JτN+(0,s2)

3.1.2 All defaults

s = 1, sd = 1

set.seed(0)

RW() |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.1.3 Reduce s

s = 0.01, sd = 1

set.seed(0)

RW(s = 0.01) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.1.4 Reduce s and sd

s = 0.01, sd = 0

set.seed(0)

RW(s = 0.01, sd = 0) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.2 Second-Order Random Walk RW2()

3.2.1 Model

β1N(0,sd2)β2N(β1,sd_slope2)βjN(2βj1βj2,τ2),j=3,,JτN+(0,s2)

3.2.2 All defaults

s = 1, sd = 1, sd_slope = 1

set.seed(0)

RW2() |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.2.3 Reduce s

s = 0.01, sd = 1, sd_slope = 1

set.seed(0)

RW2(s = 0.01) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.2.4 Reduce s and sd_slope

s = 0.01, sd = 1, sd_slope = 0.01

set.seed(0)

RW2(s = 0.01, sd_slope = 0.01) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.2.5 Reduce s, sd, and sd_slope

s = 0.01, sd = 0, sd_slope = 0.01

set.seed(0)

RW2(s = 0.01, sd = 0, sd_slope = 0.01) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.3 Autoregressive AR()

3.3.1 Model

βjN(ϕ1βj1++ϕn_coefβjn_coef,ω2) TMB derives a value of ω that gives each βj variance τ2. The prior for τ is τN+(0,s2). The prior for each ϕk is ϕk+12Beta(shape1,shape2).

3.3.2 All defaults

n_coef = 2, s = 1

set.seed(0)

AR() |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.3.3 Increase n_coef

set.seed(0)

AR(n_coef = 3) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.3.4 Reduce s

set.seed(0)

AR(s = 0.01) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.3.5 Specify ‘along’ and ‘by’ dimensions

set.seed(0)

AR() |>
  generate(n_along = 20, n_by = 3, n_draw = 8) |>
  plot_cor_many()

3.3.6 Specify ‘along’ and ‘by’ dimensions, set con = "by"

set.seed(0)

AR(con = "by") |>
  generate(n_along = 20, n_by = 3, n_draw = 8) |>
  plot_cor_many()

3.4 First-Order Autoregressive AR1()

3.4.1 Model

βjN(ϕβj1,ω2) TMB derives a value of ω that gives each βj variance τ2. The prior for τ is τN+(0,s2). The prior for ϕ is ϕminmaxminBeta(shape1,shape2).

3.4.2 All defaults

s = 1, min = 0.8, max = 0.98

set.seed(0)

AR1() |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.4.3 Reduce s

set.seed(0)

AR1(s = 0.01) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.4.4 Reduce s and modify min, max

set.seed(0)

AR1(s = 0.01, min = -1, max = 1) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.5 Random Walk with Seasonal Effects RW_Seas()

3.5.1 Model

βj=αj+λjα1N(0,sd2)αjN(αj1,τ2),j=2,,JτN+(0,s2)λjN(0,sd_seas2),j=1,,n_seas1λj=j1s=1λjs,j=n_seas,2n_seas,λjN(λjn_seas,ω2),otherwiseωN+(0,s_seas2)

3.5.2 All defaults

s = 1, sd = 1, s_seas = 0, sd_seas = 1

set.seed(0)

RW_Seas(n_seas = 4) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.5.3 Reduce s, sd

s = 0.01, sd = 0, s_seas = 0, sd_seas = 1

set.seed(0)

RW_Seas(n_seas = 4, s = 0.01, sd = 0) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.5.4 Increase s_seas

s = 0.01, sd = 0, s_seas = 1, sd_seas = 1

set.seed(0)

RW_Seas(n_seas = 4, s = 0.01, sd = 0, s_seas = 1) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.5.5 Reduce sd_seas

s = 0.01, sd = 0, s_seas = 1, s_seas = 0, sd_seas = 0.01

set.seed(0)

RW_Seas(n_seas = 4, s = 0.01, sd = 0, sd_seas = 0.01) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.6 Second-Order Random Walk with Seasonal Effects RW2_Seas()

3.6.1 Model

βj=αj+λjα1N(0,sd2)α2N(α1,sd_slope2)αjN(2αj1αj2,τ2),j=3,,JτN+(0,s2)λjN(0,sd_seas2),j=1,,n_seas1λj=j1s=1λjs,j=n_seas,2n_seas,λjN(λjn_seas,ω2),otherwiseωN+(0,s_seas2)

3.6.2 All defaults

s = 1, sd = 1, sd_slope = 1, s_seas = 0, sd_seas = 1

set.seed(0)

RW2_Seas(n_seas = 4) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.6.3 Reduce s, sd, sd_slope, sd_seas

s = 0.01, sd = 0, sd_slope = 0.01, s_seas = 0, sd_seas = 0.01

set.seed(0)

RW2_Seas(n_seas = 4, s = 0.01, sd = 0, sd_slope = 0.01, sd_seas = 0.01) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.7 Linear Lin()

3.7.1 Model

βj=αj+ϵjαj=(jJ+12)ηηN(mean_slope,sd_slope2)ϵN(0,τ2)τN+(0,s2)

3.7.2 All defaults

s = 1, mean_slope = 0, sd_slope = 1

set.seed(0)

Lin() |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.7.3 Reduce s

s = 0, mean_slope = 0, sd_slope = 1

set.seed(0)

Lin(s = 0) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.7.4 Modify mean_slope, sd_slope

s = 1, mean_slope = 0.2, sd_slope = 0.1

set.seed(0)

Lin(mean_slope = 0.2, sd_slope = 0.1) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.7.5 Specify ‘along’ and ‘by’ dimensions

set.seed(0)

Lin() |>
  generate(n_along = 20, n_by = 3, n_draw = 8) |>
  plot_cor_many()

3.7.6 Specify ‘along’ and ‘by’ dimensions, set con = "by"

set.seed(0)

Lin(con = "by") |>
  generate(n_along = 20, n_by = 3, n_draw = 8) |>
  plot_cor_many()

3.8 Linear with AR Errors Lin_AR()

3.8.1 Model

βj=αj+ϵjαj=(jJ+12)ηηN(mean_slope,sd_slope2)ϵjN(ϕ1ϵj1++ϕn_coefϵjn_coef,ω2)τN+(0,s2)ϕk+12Beta(shape1,shape2)

3.8.2 All defaults

n_coef = 2, s = 1, mean_slope = 0, sd_slope = 1

set.seed(0)

Lin_AR() |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.8.3 Reduce s, sd_slope

s = 0.1, mean_slope = 0, sd_slope = 0.01

set.seed(0)

Lin_AR(s = 0.1, sd_slope = 0.01) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.9 Linear with AR1 Errors Lin_AR1()

3.9.1 Model

βj=αj+ϵjαj=(jJ+12)ηηN(mean_slope,sd_slope2)ϵjN(ϕϵj1,ω2)τN+(0,s2)ϕminmaxminBeta(shape1,shape2)

3.9.2 All defaults

s = 1, min = 0.8, max = 0.98, mean_slope = 0, sd_slope = 1

set.seed(0)

Lin_AR1() |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.9.3 Modify min, max

s = 1, min = -1, max = 1, mean_slope = 0, sd_slope = 1

set.seed(0)

Lin_AR1(min = -1, max = 1) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.9.4 Reduce s, sd_slope

s = 0.1, min = 0.8, max = 0.98, mean_slope = 0, sd_slope = 0.02

set.seed(0)

Lin_AR1(s = 0.1, sd_slope = 0.02) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.10 Penalised Spline Sp()

3.10.1 Model

ββ=\bmXααα1N(0,sd2)α2N(α1,sd_slope2)αjN(2αj1αj2,τ2),j=3,,JτN+(0,s2)

3.10.2 All defaults

n_comp = NULL, s = 1, sd = 1, sd_slope = 1

set.seed(0)

Sp() |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.10.3 Specify n_comp

n_comp = 15, s = 1, sd = 1, sd_slope = 1

set.seed(0)

Sp(n_comp = 5) |>
  generate(n_draw = 8) |>
  plot_cor_one()

3.10.4 Reduce s, sd, sd_slope

n_comp = NULL, s = 0.01, sd = 0.01, sd_slope = 0.01

set.seed(0)

Sp(s = 0.01, sd = 0.01, sd_slope = 0.01) |>
  generate(n_draw = 8) |>
  plot_cor_one()

4 SVD-Based Priors

4.1 Exchangeable SVD()

4.1.1 Model

ββ=FFαα+gg

4.1.2 All defaults

n_comp = NULL, indep = TRUE

set.seed(0)

SVD(HMD) |>
  generate(n_draw = 8) |>
  plot_svd_one()

4.1.3 Increase n_comp

n_comp = 5, indep = TRUE

SVD(HMD, n_comp = 5) |>
  generate(n_draw = 8) |>
  plot_svd_one()

4.1.4 Set indep to FALSE

SVD(HMD, indep = FALSE) |>
  generate(n_draw = 8) |>
  plot_svd_one()

4.1.5 Multiple units

SVD(HMD, indep = FALSE) |>
  generate(n_draw = 8, n_element = 3) |>
  plot_svd_many()

4.2 Dynamic SVD Prior: SVD_AR()

SVD_AR(HMD, indep = FALSE, s = 0.1) |>
  generate(n_draw = 6, n_along = 5) |>
    ggplot(aes(x = age_mid(age), 
                      y = value, 
                      color = sexgender)) +
      facet_grid(vars(draw), vars(along)) +
      geom_line() +
      scale_color_manual(values = c("darkgreen", "darkorange")) +
      xlab("Age") +
      ylab("") +
      theme(text = element_text(size = 8),
            legend.position = "top",
            legend.title = element_blank())

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.