Skip to contents

Load the coin model and example data available within treepplr

model <- tp_model("coin")
data <- tp_data("coin")

The data in this example is a sequence of coin flip results. phyjson is the data format that treeppl can read.

data
#> $coinflips
#>  [1] FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE
#> [13] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE
#> 
#> attr(,"class")
#> [1] "phyjson"

Run the coin.tppl TreePPL program.

output_json <- tp_treeppl(model = model, model_file_name = "coin", data = data, data_file_name = "coin")

Plot the posterior distribution (normalized weights).

output <-  tp_parse_coin(output_json)
max <- max(output["weights"])
output["weights"] <- exp(output["weights"] - max)

ggplot(output) +
  geom_histogram(aes(samples, y = after_stat(density), weight=weights), col = "white", fill = "lightblue") +
  geom_density(aes(samples, weight=weights)) +
  theme_bw()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.