Skip to content

Instantly share code, notes, and snippets.

@kenkellner
Last active February 19, 2016 21:31
Show Gist options
  • Save kenkellner/4162de826d8d8d47a75f to your computer and use it in GitHub Desktop.
Save kenkellner/4162de826d8d8d47a75f to your computer and use it in GitHub Desktop.
#There isn't really an easy way to do this, unfortunately.
#In your stated example, the populations are ordered, so you should be able to calculate the mean in JAGS like this:
Pop1.mean <- mean(data[1:3,1:10])
Pop2.mean <- mean(data[4:6,1:10])
#but maybe your actual dataset isn't organized that way, and even if it was you'd have to
#manually go through the dataset to figure out the cutoffs.
#I've also done a hacky workaround as follows. Define new vectors, one for each population.
#For the pop1 vector, the value is 1 if the individual is from population 1, 0 otherwise.
#For pop2 vector, the value is 1 if an individual is from population 2, 0 otherwise, and so on.
#Feed these vectors in as data. You'll also need to know the number of individuals in each population n1 and n2.
pop1 <- c(1,1,1,0,0,0)
pop2 <- c(0,0,0,1,1,1)
n1 <- n2 <- 6
nindividuals <- 6
nmeasurements <- 10
#Then, in your JAGS model:
#Make a new set of nested loops or stick in existing one, it shouldn't matter
for (i in 1:nindividuals){
for (j in 1:nmeasurements){
pop1mean.hold[i,j] <- pop1[i]*data[i,j] #note that this will be the data value if in pop1, 0 otherwise
pop2mean.hold[i,j] <- pop2[i]*data[i,j] #and so on for other populations
}
}
#Then, outside loop, manually calculate the mean:
pop1mean <- sum(pop1mean.hold[])/(n1*nmeasurements) #all non pop 1 are zeroes and so are ignored
pop2mean <- sum(pop2mean.hold[])/(n2*nmeasurements) #note that you need to include the empty brackets in sum or JAGS won't like it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment