My data is like this: I have N
students from different courses (variable course_id
) that where asked about household income (variable income
) and had to take part in two math tests; one test was considered "income fair" and the other was not (variable test_type
). The results of the math test are in the variable test_results
. Each student took both of the tests. And students are grouped in courses. Students have an ID variable student_id
.
My general question is whether the effect of the income on test results varies depending on the test type, i.e. is smaller for the income fair test. My challenge is now to model this because students are nested in courses (each course has multiple students; each student in just one course) and there is repeated measures (each student takes two tests: the fair and the normal test). I want to take into account that there can be a random intercept and slope of income on test results for for the courses. How to do this?
I've read and tried so many thing like this and am quite confused now.. What I do in R is this:
my_fit <- lme4::lmer(test_results ~ income + (1|test_type) + (1|course_id) +
(income|course_id), data= anna_long)
summary(my_fit)
But I am not sure whether this is correct. And there is no p value for the income predictor... I am lost..
Before I also wanted to take into account that there can also be a random intercept and slope for each student and what I did was...
my_fit <- lme4::lmer(test_results~ income + (1|test_type) +
(1|course_id/student_id) + (income|course_id/student_id),
data= anna_long)
summary(my_fit)
... but this resulted in the error
number of observations (=361) <= number of random effects (=362)
... Therefore I discarded the random effects associated with single students and focused on random effects of courses as shown above.