Hello,
I would like to retrieve the sigma parameters using a distributional model with brms
. The data is generated as:
b0 = 0.8
b1 = 0.2
a = 0.5
h2 = 0.4
h = sqrt(h2)
e = sqrt(1-h^2)
E = rnorm(1000, 0, 1)
N = length(E)
g = rnorm(N,0,1)
sigma = sqrt((b0*e + b1*e*E)^2)
y = a*E + b0*h*g + b1*h*E*g + rnorm(N,0,sigma)
dat = data.frame(y, g, E)
print(paste0("b0*e = ", round(b0*e, 3), " and b1*e = ", round(b1*e, 3)))
"b0*e = 0.62 and b1*e = 0.155"
This is the model I am running but it has the problem of negative values for the scale parameter:
f = bf(y ~ g + E + g * E, sigma ~ 1 + E)
fit = brm(f, data = dat, family = brmsfamily("gaussian", link_sigma = "identity"), chains = 1)
summary(fit)
If I try the non-linear approach the values are right but still got the scale issue:
f = bf(y ~ g + E + g * E) +
nlf(sigma ~ sqrt(vest)^2) +
lf(vest ~ 1 + E)
fit = brm(f, data = dat,
family = brmsfamily("gaussian", link_sigma = "identity"), chains = 1,
prior = c(prior(student_t(7, 0, 8), class='b', coef = 'E', nlpar = 'vest')))
SAMPLING FOR MODEL '8714549d61cef2c62abe24dcb0310027' NOW (CHAIN 1).
Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: normal_lpdf: Scale parameter[1] is nan, but must be > 0! (in 'model7d783aa92632_8714549d61cef2c62abe24dcb0310027' at line 42)
...
Family: gaussian
Links: mu = identity; sigma = identity
Formula: y ~ g + E + g * E
sigma ~ sqrt(vest)^2
vest ~ 1 + E
Data: dat (Number of observations: 1000)
Samples: 1 chains, each with iter = 2000; warmup = 1000; thin = 1;
total post-warmup samples = 1000
Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept 0.00 0.02 -0.04 0.04 1.00 932 749
vest_Intercept 0.61 0.01 0.58 0.64 1.00 785 746
vest_E 0.16 0.01 0.14 0.17 1.00 813 546
g 0.53 0.02 0.49 0.56 1.00 722 615
E 0.50 0.01 0.47 0.52 1.00 844 765
g:E 0.13 0.01 0.11 0.16 1.00 711 813
Samples were drawn using sampling(NUTS). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).
For sure I am making a silly mistake. Any suggestions on how to specify the model to recover b0*e
and b1*e
for sigma?
Thank you in advance!