Longitudinal lme equation with level 1 and level 2 variables I have data for student test scores taken over many years as well as demographic and screening data (Emohealth, memory, and the dependent variable MathScore changes for each year for each student). When I construct a longitudinal model in R, I'm not sure where to put certain variables. 
I've built two models:
fit <- lme(MathScore~Year+White+Black+Hispanic+Male+Age+EmoHealth+Memory, 
           random=~Year|ID, data=df, na.action="na.omit")

fit2 <- lme(DState ~ Year+White+Black+Hispanic+Male+Age+EmoHealth+Memory, 
            random=~1|Year, data=df, na.action=na.omit, method='ML'))  

What I don't understand is:


*

*Why does Rstudio lme code recommend I add the 1+ to the variables? Do I need it?

*Whether or not fit2 is accounting for Year and Student ID, or if it needs to do so. 

*I don't know how to indicate in the model that emotional health can change each year but being male cannot. 

 A: *

*In the empty model this 1 represents your intercept, but the outcome should be the same with and without the 1+ when you include predictor variables (you could check this to make sure).

*Fit2 specifies a fixed effect for year but I believe the random structure is incorrect. You want to specify a random effect for your grouping variable, which in this case is ID so it should be random = ~1|ID for a random intercept model or random = ~Year|ID for a random slope on year. When working with multiple levels you can use list e.g. random= list(school = ~1, ID = ~ 1) for a random intercept model with three levels or random= list(school = ~1, ID = ~ Year) for a random slope on year for the ID level.

*nlme should automatically see which variables belong to a higher or lower level (i.e. it should recognize variables that can vary by Year and by ID) as long as you correctly specify the random structure. If you use summary(fit2) the output returns the number of groups (ID) and the number of observations at the bottom, this should give you a clue of whether the grouping structure is correctly specified. 
edit: I.m.o this website provides excellent examples of longitudinal data analyses with lme4 or nlme: http://rpsychologist.com/r-guide-longitudinal-lme-lmer should be a big help
