# Computation of correlation between variables pertaining to groups and variables pertaining to subjects included in groups

I have a number of observations that are organized in groups and subgroups. I would like to estimate the correlation coefficient between a variable that refers to each group and a variable that refers to each subject, while accounting for the existing correlation induced by the specificities of each group and subgroup.

Since it is a bit difficult to explain my variables, I will provide an equivalent fictitious example:

Say I want to calculate the correlation between the altitude of a school with the performance of its students. I have a dataset in which the students are organized by school and classroom. It is known that the school and the classroom (say because of the teacher) influence the performance of the students. On the other hand, the altitude refers to the school and is common for all the students of each school.

It is unclear to me which type of model I would need to use.

This strikes me as a good candidate for a multilevel model. From the first paragraph of the Wikipedia entry:

Multilevel models are statistical models of parameters that vary at more than one level. An example could be a model of student performance that contains measures for individual students as well as measures for classrooms within which the students are grouped.

• Thanks for the answer! That's where I am currently at. I have not found any reference though for correlating "group" with "subject" variables. Essentially, in the example I described above, what I would need is the pearson correlation coefficient between altitude and student performance. If I had millions of schools in my dataset, I could randomly choose just one student per school and estimate the desired correlation using my altitude-performance pairs. However, if I do that, I discard a large part of my dataset, which in my case is a problem. Sep 11, 2017 at 10:34

Like @ScouserInTrousers I think a multilevel model is what you want. If you look at the results of a multilevel model with only altitude as a fixed effect, then the r squared from that may be the correlation you want - or a good substitute. See this thread.