I have data like this aa p sc met p1 34 met p1 56 met p2 45 met p2 33 ser p1 34 ser p1 56 ser p2 45 ser p2 33 ... I want to estimate the following variances for: sc in p1 for met sc in p2 for met sc between p1 and p2 for met sc for met sc in p1 for ser sc in p2 for ser sc between p1 and p2 for ser sc for ser Does anybody how to write code for this?