0

heyy i have a problem with getting the the below error. I am new to R and not sure how to resolve this!!

I am trying to find the mean of residuals in a multi-linear regression model (testing for the assumptions)

My code is

mean(model1$residuals)

where model1 is

model1 = lmer(corruption ~ Gini.Index + GDP + Democracy.Index + Political.Terror.Index + Economic.Freedom  + Regime + respondentage + 
            respondentgender + Education_Completed + Employment + European.Integration  + (1 | country), NewD, REML = FALSE)

When i run the code for mean i get error--- error in evaluating the argument 'x' in selecting a method for function 'mean': $ operator not defined for this S4 class

Does anyone know what to do???

  • 2
    You can use `mean(resid(model1))` - unlike `lm` objects, the `lmerMod` objects created by `lmer` are S4 objects which don't have members accessed by `$` - they have _slots_ accessed by `@`, but it's better to use the defined methods such as `resid` to get the data out that you need. – Allan Cameron Apr 28 '22 at 20:46
  • Heyyy thank youu soo much!!! it works now – Mihaela Revencu Apr 29 '22 at 21:39

1 Answers1

1

This is because lmer does not return a value of class "lm" as lm does. The return value of lmer (with which I am not familiar) has many slots. Slots in S4 classes are accessed using @. I suggest you try

str(model1)

and find the variable you need to access, e.g. like this

model1@resp$mu
clemenskuehn
  • 116
  • 8