I am running multilevel models ( two level and three levels) in r using nlme and lme4 packages but I need to test for linearity. Are there any recommendations of practical resources to test for linearity in r or stata?
do you mean the linearity of the effect? If yes: I am certainly not an expert (yet) but you could run a GAMM (generalized additive model) in which you compare a model with a linear effect with a model with a smooth function) and then compare both (although the nonlinear model will already tell you whether the effect is linear).
I did that last week with a sample of 35 startups surveyed weekly of the first 3 months of the Corona crisis. The code (using the mgcv) package was
Linearity versus what, and is this for a single predictor? If you mean versus models with 2 or 3 (or more degrees of freedom), which do you mean? Also why are you interested? Do you want a hypothesis test?
I am doing research for the first time hence responses are of great help.
I have 20 independent variables to be run in 6 multilevel regression models. But I need to check the violation of the assumptions e.g. linearity, heteroscedasticity, multicolinearity etc. So I wanna test whether each of the models suit linear or non linear model.
I appreciate for the materials so far but if there is something that can guide me on how to carry out the process in R or stata then much better.
I would caution against using 20 predictor variables. You will need a lot of data to estimate these simultaneously. Much better to have a few focused research questions. It would help if you can explain why you are using multilevel models. What is the structure of you data?
That being said I have used such complex models, but then I had clear research questions, lots of observations (9,000) and I built my model sequentially so I could evaluate the contribution of different sets of conceptually distinct variables ; see as an example
Article Death and deprivation: an exploratory analysis of deaths in ...
Essentially you can use the same procedures for evaluating assumptions of the fixed part of the model as you would use for standard regression; eg VIFs for collinearity. The residual diagnostic chapter in this considers residuals at each level. Book Developing multilevel models for analysing contextuality, he...
The final chapter in the volume discusses and illustrates a modelling strategy which I characterize as theoretically informed bottom-up. As always the key is the amount of theory and conceptualization that you can bring to the task; the more the better for validating your argument.
There is a strong tendency in epidemiological work these days not to ask what produces the response (a very difficult ask) but rather does this specific exposure relate to the outcome, is it affected by confounders that you need to control; are there moderators that modify the relation (that is interactions) and are there mediators on the pathway between the exposure and the outcome? All a long way from such procedures such as stepwise regression. Paying attention to assumptions is really good but we all need to remember that modelling is a means to and end and not an end in itself. It is about answering research questions in the most valid manner. Sorry for going on!