Error in galax_samples_mx2[, 6] : subscript out of bounds

I am receiving the following error message when running my code. Not sure what is the error message referring to. Any help would be appreciated.

MIX_Model =  "model{
  for (i in 1:n) {
    y[i] ~ dnorm(mu[z[i]], tau)
    z[i] ~ dcat(group_probs)
    group_probs ~ ddirich(d)tau ~ dgamma(0.15, 0.1)
    for (j in 1:ngroups) {
    mu_raw[j] ~ dnorm(0, 1E-6)
    d[j] = 2sd[j] = pow(tau,-0.5)
    mu = sort(mu_raw)

tc2 = textConnection(MIX_Model)

mm = jags.model(tc2, data = galax_Data, n.chains = 4)

update(mm, n.iter = 100000)

samples_g = coda.samples(mm,variable.names = c("group_probs","tau","sd"),n.iter = 100000)

mydic = dic.samples(mm,n.iter = 100000)

galax_samples_mx2 = as.matrix(samples_g[[1]])

rmix = function(y, group_probs, group_means, group_sds) {
  group1_density = dnorm(y, group_means[1], group_sds[1])
  group2_density = dnorm(y, group_means[2], group_sds[2])
  mix_density = group_probs[1] * group1_density + group_probs[2] * group2_density

dens = rmix(scale(galaxies)[1:82,],
c(mean(galax_samples_mx2[, 1]), mean(galax_samples_mx2[, 2])),
c(mean(galax_samples_mx2[, 3]), mean(galax_samples_mx2[, 4])),
c(mean(galax_samples_mx2[, 5]), mean(galax_samples_mx2[, 6])))

plot(scale(galaxies)[1:82, ],
     col = "red",
     xlab = "galaxies",
     type = "l")

 Error in galax_samples_mx2[, 6] : subscript out of bounds


With this code you are trying to calculate the mean of the sixth column of galax_samples_mx2 but the error message is telling you that galax_samples_mx2 doesn't have 6 columns.

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Thanks for your help with this. I have uploaded all the code. Thanks

That was not what I asked for, your example is still not reproducible since you are not providing sample data and very likely the error message is related to the structure of your data.

I'm afraid I can't help you any further without a reproducible example.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.