You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying a censored poisson regression using Turing, but unfortunately I am unable to do so. For context, I have some data where we asked respondents how many times they did an activity, but the options were top-coded. Naturally, this skews the responses.
When I try use Turing.@addlogprob! StatsFuns.poislogpdf() to try account for the censoring, I get the following error message:
Based on the message, I assume it is something to do with poislogccdf / poislogpdf within a Turing model. Using poislogccdf(exp(lambda[1]), y[1]) is fine if done outside the Turing model.
Here is some code for a minimum working example:
using Turing, Distributions, StatsFuns
# data
N_sims = 1000
x = rand(N_sims)
lambda = 0.3 .+ 0.5 .* x
y = rand.(Poisson.(exp.(lambda)))
censored = ifelse.(y .> 3, 1, 0)
y[y .> 3] .= 3
@model function poisson_censored(y, x, censored, N)
# priors
α ~ Normal(0, 2)
β ~ Normal(0, 2)
# model
for n in 1:N
if censored[n] == 1
Turing.@addlogprob! StatsFuns.poislogccdf(exp.(α .+ β .* x[n]), y[n])
#Turing.@addlogprob! logccdf.(Poisson(exp.(α .+ β .* x[n])), y[n])
elseif censored[n] == 0
Turing.@addlogprob! StatsFuns.poislogpdf(exp.(α .+ β .* x[n]), y[n])
#Turing.@addlogprob! logpdf(Poisson(exp.(α .+ β .* x[n])), y[n])
end
end
end
mod_censored = poisson_censored(y, x, censored, N_sims)
chn_censored = sample(mod_censored, NUTS(), 2_000)
# works!
poislogccdf(exp(lambda[1]), y[1])
The text was updated successfully, but these errors were encountered:
The issue is not Turing specific. The problem is that StatsFuns.poslogccdf does not support dual numbers currently (it still uses the Rmath backend) (and it is also what logccdf(Poisson(), ...) ends up calling so it shouldn't matter what you use). JuliaStats/StatsFuns.jl#113 will improve this situation and replace it with a Julia implementation. AD should then work, possibly after defining some additional derivatives for specific functions in SpecialFunctions that are used by this implementation.
For the time being you can try to use the implementation in the PR instead of StatsFuns.poislogccdf.
I am trying a censored poisson regression using Turing, but unfortunately I am unable to do so. For context, I have some data where we asked respondents how many times they did an activity, but the options were top-coded. Naturally, this skews the responses.
When I try use
Turing.@addlogprob! StatsFuns.poislogpdf()
to try account for the censoring, I get the following error message:Based on the message, I assume it is something to do with
poislogccdf
/poislogpdf
within a Turing model. Usingpoislogccdf(exp(lambda[1]), y[1])
is fine if done outside the Turing model.Here is some code for a minimum working example:
The text was updated successfully, but these errors were encountered: