I think this will do it, though there might be a simpler way for(j in 1:50{ for (i in 1:4000){ ind[i,j] <- equals(group[i],j) wtd_pred[i,j] <- ind[i,j]*pred[i] } mean_pred[j] <- sum(wtd_pred[,j])/sum(ind[,j]) } Best wishes Andrew -- Dr. Andrew Millard e: [log in to unmask] | t: +44 191 334 1147 w: http://www.dur.ac.uk/archaeology/staff/?id=160 Senior Lecturer in Archaeology, & Associate Director of the Institute of Medieval & Early Modern Studies, Durham University, UK > -----Original Message----- > From: (The BUGS software mailing list) [mailto:[log in to unmask]] On > Behalf Of Basile Chaix > Sent: 24 September 2015 20:08 > To: [log in to unmask] > Subject: [BUGS] Calculation of an average by group in Winbugs > > Dear colleagues, > > In my Winbugs model, I have a variable, eg for 4000 observations > reporting group membership, and there are 50 groups coded from 1 to 50 > in this variable. The variable is group[i] with i from 1 to 4000. > > From my model, I calculate a predicted value, pred[i], with i from 1 > to 4000. > > I simply want to calculate at each iteration the mean value of pred > for each of the 50 groups of the group variable. The mean of the > prediction by groups will be mean_pred[j] with j ranging from 1 to 50 > > How to calculate mean_pred[j] from pred[i] and group[i]? > > Thank you so much for your help, > > Basile > > ------------------------------------------------------------------- > This list is for discussion of modelling issues and the BUGS software. > For help with crashes and error messages, first mail bugs@mrc- > bsu.cam.ac.uk To mail the BUGS list, mail to [log in to unmask] > Before mailing, please check the archive at > www.jiscmail.ac.uk/lists/bugs.html Please do not mail attachments to > the list. To leave the BUGS list, send LEAVE BUGS to > [log in to unmask] If this fails, mail bugs- > [log in to unmask], NOT the whole list ------------------------------------------------------------------- This list is for discussion of modelling issues and the BUGS software. For help with crashes and error messages, first mail [log in to unmask] To mail the BUGS list, mail to [log in to unmask] Before mailing, please check the archive at www.jiscmail.ac.uk/lists/bugs.html Please do not mail attachments to the list. To leave the BUGS list, send LEAVE BUGS to [log in to unmask] If this fails, mail [log in to unmask], NOT the whole list