I am interested in examining the relationship between yearly income and student success, taking into account the hierarchical structure of the data. The data includes schools as random effects, with classrooms nested within schools, and there may be correlations among observations within the same school and classroom.
To address this, my objective is to develop a generalized additive model that considers student success as the response variable, with yearly income as a fixed effect, and includes a random effect for schools and also for classrooms nested within schools.
Here is the proposed code for fitting the model using the mgcv package in R:
library(mgcv) gam_model <- mgcv::gam(success ~ s(incomeperyear) + s(classroom, bs = "re", by = school), data = my_data, method = "REML", family = gaussian()) summary(gam_model)
Furthermore, considering that schools and classrooms have distinct numeric values, I am uncertain whether it is required to convert them into factors prior to executing the provided code. I am unsure whether a random effect for a continuous variable is statistically applicable. Hence, I am unclear whether the mgcv package will automatically treat each numeric level as a distinct category if I do not specify the classroom and school variables as factors.
I would appreciate any insights or recommendations regarding the accuracy of the provided code and the appropriate handling of numeric variables as factors in this context.