Q:

lme4 + Analysis by Group

I have a file that I am working with that contains State Math Test Scores for Grades 4-8 for multiple schools.  I am planning on using the lmer command from lme4 to run a separate unconditional model for each grade level (I am doing this because the exams have different scales).  I created a for-loop that splits the original data file into grade-specific dataframes:
for(i in levels(MathTestData$CURR_GRADE_LVL)){
  assign(paste("MathTest.",i,sep=""), MathTestData[MathTestData$CURR_GRADE_LVL==i, ])
}

Is there a way to use a loop command that can run an unconditional model for each grade-level dataframe?  For the purposes of this example, let's call the dependent variable "MathScore" and the Level-2 id "SchoolID."
Since the grade levels are between 4 and 8, I tried the following for-loop, but it did not work (I get the error:  "Error: 'data' not found, and some variables missing from formula environment"):
for(i in 4:8){
  UnconditionalModel.i <- lmer(MathScore~1+(1|SchoolID), data=MathTest.i)
  summary(UnconditionalModel.i)
}

The main issue I have is that I understand one can use lapply on a list as suggested by others, but by doing so, you end up losing a lot of valuable information that would otherwise be provided by the lme4 and lmerTest packages (specifically p-values and test statistics for the fixed effects, and the random effects are presented as standard deviations as opposed to variances).

A:

I think you could use multilevel regression on your entire data frame.
lmer(mathscor ~ schoolid + (1 + schooldid | CURR_GRADE_LVL) ) 

This way your slope and intercept change for each grade. You can use ranef to recover the coefficient for each grade

