Home > other >  Error message when making QQ plots of residuals (lmer)
Error message when making QQ plots of residuals (lmer)

Time:02-24

Does anyone know how to fix this error when trying to make QQ plots of residuals, data and code below;

head(final_data)
              Country             Continent Life_Expectancy 
1         Afghanistan Eastern Mediterranean        62.68935                         
2             Albania                Europe        76.37373                         
3             Algeria                Africa        76.36365                      
4              Angola                Africa        62.63262                       
5 Antigua and Barbuda              Americas        74.99754                       
6           Argentina              Americas        76.94621                        

mod6 = lmer(Life_Expectancy ~ Concentration_of_PM2.5   
            (1|Continent),
     data=final_data)

res5 = data.frame(Residuals=resid(mod5),
              Continent=final_data$Continent,
              Fitted=fitted(mod5))

qq_plot6 = ggplot(final_data, aes(sample = mod6$residuals))   
  geom_qq()  
  geom_qq_line(col = 'red')  
  xlab('Theoretical residuals')  
  ylab('Sample residuals')  
  ggtitle('QQ plot for residual mod6')  
  theme(plot.title = element_text(hjust = 0.5))

I get this error when making the QQ plot here:

Error in mod6$residuals : $ operator not defined for this S4 class

CodePudding user response:

merMod objects (the results of an lmer fit) aren't lists (they're S4 objects) and don't have a $residuals element. Try residuals(mod6) instead; it's always better practice to use accessor methods (which don't depend on the internal structure of model objects) anyway.

See also: broom.mixed::augment(), lattice::qqmath(mod6), performance::check_model(mod6).

  • Related