Hi,
I’m trying to implement a linear model with a categorical variable in turing but am stuck.
The model has a different μ, depending on whether S=0 or S=1:
Wᵢ ~ Normal(μᵢ, σ)
μᵢ = α_S[i]
α = [α1, α2]
Priors: αᵢ ~ Normal(60, 10)
My implementation looks like this:
@model function linear_model(X, Y, S)
# Set variance prior
σ2 ~ Uniform(0.0, 10.0);
# Set intercept prior
α1 ~ Normal(60, 10);
α2 ~ Normal(60, 10);
# Set slope prior
β ~ LogNormal(0.0, 1.0);
# S == 1 means male
μ1 = α1 .+ β .* (X[S .== 1] .- mean(X[S .== 1]));
# S == 0 means female
μ2 = α2 .+ β .* (X[S .== 0] .- mean(X[S .== 0]));
μall = zeros(size(S))
μall[S .== 0] = μ2[:]
μall[S .== 1] = μ1[:]
W ~ MvNormal(μall, sqrt(σ2));
end
model = linear_model(X, Y, S);
sample(model, NUTS(0.8), 4)
But this throws
ERROR: TypeError: in typeassert, expected Float64, got a value of type ForwardDiff.Dual{Nothing, Float64, 12}
when setting μall[S .== 0] = μ2[:]
Does anyone have a suggestion on how to handle categorical variables?