This tutorial shows the Negative-Binomial family in VCMoE. Expert coefficients are on the log mean count scale. Library size or size factors should enter through an expert-side offset such as offset(log_size_factor).
library(VCMoE)set.seed(61)
sim <- simulate_vcmoe_negbin(
n = 320,
k = 2,
seed = 61,
separation = 3.0,
mean_count = 18,
scenario = "well_separated"
)
head(sim$data)
#> y u z1 x1 size_factor log_size_factor component
#> 1 41 0.002060810 0.38763954 -0.99764329 0.5894359 -0.5285892 2
#> 2 64 0.006113068 -1.86003760 -0.77784040 1.2244753 0.2025124 2
#> 3 55 0.006241603 -0.61665820 0.86469706 1.2212583 0.1998818 2
#> 4 28 0.013615126 0.24613859 0.29382958 1.5135793 0.4144772 1
#> 5 28 0.016265503 0.01320161 -0.62373067 1.1703616 0.1573127 2
#> 6 109 0.016968450 -1.64503391 0.03270223 1.2195169 0.1984548 2
summary(sim$data$y)
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.00 10.00 21.00 25.94 36.00 174.00The offset is part of the expert formula, not a separate argument. It controls library-size variation before interpreting the component-specific coefficients.
fit <- vcmoe_fit(
y ~ z1 + offset(log_size_factor) | x1,
data = sim$data,
u = "u",
family = "negative-binomial",
k = 2,
bandwidth = 0.60,
u_grid = seq(0.15, 0.85, length.out = 5),
control = list(
maxit = 120,
n_starts = 2,
seed = 62,
warn_ambiguous = FALSE,
ridge = 1e-4,
negbin_theta_ridge = 0.05,
negbin_theta_target = 8
)
)
fit
#> VCMoE fit
#> family: negative-binomial
#> components: 2
#> label alignment: global
#> kernel: epanechnikov (density_over_bandwidth)
#> local basis: (u - u0) / bandwidth
#> u scale: unit
#> grid points: 5
#> bandwidth: 0.6
#> converged grid points: 5/5coef(fit, "expert")[, , "z1"]
#> component
#> u component1 component2
#> 0.15 0.3762088 -0.3165998
#> 0.325 0.4534632 -0.3475787
#> 0.5 0.4544168 -0.3074733
#> 0.675 0.4903412 -0.2570500
#> 0.85 0.6182525 -0.1509355
coef(fit, "theta")
#> component
#> u component1 component2
#> 0.15 8.851984 9.051642
#> 0.325 8.631930 7.123816
#> 0.5 8.229695 6.476805
#> 0.675 7.709741 6.352065
#> 0.85 7.558996 7.029107Component-specific predictions and marginal means are on the count scale.
head(predict(fit, type = "component"))
#> component1 component2
#> [1,] 10.895693 18.51859
#> [2,] 9.716965 78.37390
#> [3,] 15.471706 52.73117
#> [4,] 26.527984 49.73161
#> [5,] 18.791464 41.39765
#> [6,] 10.492933 72.92002
head(predict(fit, type = "mean"))
#> [1] 15.70570 52.71889 37.48414 40.52602 32.87617 48.50699
head(predict(fit, type = "posterior"))
#> [,1] [,2]
#> [1,] 2.534505e-03 0.9974655
#> [2,] 4.941373e-10 1.0000000
#> [3,] 2.028103e-04 0.9997972
#> [4,] 6.408624e-01 0.3591376
#> [5,] 3.556667e-01 0.6443333
#> [6,] 6.560240e-19 1.0000000post <- predict(fit, type = "posterior")
mean(apply(post, 1, max))
#> [1] 0.8498024diagnostics <- vcmoe_diagnostics(fit)
diagnostics[, c("u", "converged", "ambiguous", "posterior_entropy", "effective_n")]
#> u converged ambiguous posterior_entropy effective_n
#> 0.15 0.150 TRUE FALSE 0.3366344 202.3687
#> 0.325 0.325 TRUE FALSE 0.3362907 255.9518
#> 0.5 0.500 TRUE FALSE 0.3071316 297.3595
#> 0.675 0.675 TRUE FALSE 0.2832092 261.0491
#> 0.85 0.850 TRUE FALSE 0.1783882 204.5169plot_coefficients(fit, "expert")plot_posterior(fit)