Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

fix(08_ordinal_reg): fix Bijectors breaking change #88

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"

[compat]
Turing = "0.29"
Bijectors = "0.13"
julia = "1"
11 changes: 7 additions & 4 deletions _literate/08_ordinal_reg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,27 +269,28 @@ using Bijectors
using LazyArrays
using LinearAlgebra
using Random: seed!
using Bijectors: transformed, OrderedBijector
seed!(123)
setprogress!(false) # hide

@model function ordreg(X, y; predictors=size(X, 2), ncateg=maximum(y))
#priors
cutpoints ~ Bijectors.ordered(filldist(TDist(3) * 5, ncateg - 1))
cutpoints ~ transformed(filldist(TDist(3) * 5, ncateg - 1), OrderedBijector())
β ~ filldist(TDist(3) * 2.5, predictors)

#likelihood
return y ~ arraydist([OrderedLogistic(X[i, :] ⋅ β, cutpoints) for i in 1:length(y)])
end;

# First, let's deal with the new stuff in our model: the **`Bijectors.ordered`**.
# First, let's deal with the new stuff in our model: the **`Bijectors.jl`'s `transformed` and `OrderedBijector`**.
# As I've said in the [4. **How to use Turing**](/pages/04_Turing/),
# Turing has a rich ecosystem of packages.
# Bijectors implements a set of functions for transforming constrained random variables
# (e.g. simplexes, intervals) to Euclidean space.
# Here we are defining `cutpoints` as a `ncateg - 1` vector of Student-$t$ distributions
# with mean 0, standard deviation 5 and degrees of freedom $\nu = 3$.
# Remember that we only need $K-1$ cutpoints for all of our $K$ intercepts.
# And we are also constraining it to be an ordered vector with `Bijectors.ordered`,
# And we are also constraining it to be an ordered vector with `transformed(d, OrderedBijector)`,
# such that for all cutpoints $c_i$ we have $c_1 < c_2 < ... c_{k-1}$.

# As before, we are giving $\boldsymbol{\beta}$ a very weakly informative priors of a
Expand Down Expand Up @@ -355,7 +356,9 @@ DataFrames.transform!(
x -> categorical(x; levels=["0-9g/day", "10-19", "20-29", "30+"], ordered=true);
renamecols=false,
)
DataFrames.transform!(esoph, [:agegp, :alcgp, :tobgp] .=> ByRow(levelcode); renamecols=false)
DataFrames.transform!(
esoph, [:agegp, :alcgp, :tobgp] .=> ByRow(levelcode); renamecols=false
)

X = Matrix(select(esoph, [:agegp, :alcgp]))
y = esoph[:, :tobgp]
Expand Down