0

I am to test the normality of a set of residuals in a model

glm(Failure ~ Lead ,data=Exercise2, family="binomial")
summary(glm(Failure ~ Lead ,data=Exercise2, family="binomial"))

hist(summary(glm(Failure ~ Lead ,data=Exercise2, family="binomial"))$resi)

I have added the is.numeric to confirm my data is numeric

is.numeric(Exercise2$Lead)
is.numeric(Exercise2$Failure)

The output i am getting is below

Error in hist.default(summary(glm(Failure ~ Lead, data = Exercise2, family = "binomial"))$resi) : 'x' must be numeric

> is.numeric(Exercise2$Lead)
[1] TRUE
> is.numeric(Exercise2$Failure)
[1] TRUE

Can anyone advise why i am getting an error that data must be numeric when the data is numeric ?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
kiely90
  • 21
  • 2

2 Answers2

1

Try

m1 <- glm(Failure ~ Lead ,data=Exercise2, family="binomial")
hist(residuals(m1, type = "pearson"))

Or plot(m1, which = 2) to get a Q-Q plot (which is a better way to judge normality of residuals)

Your problem is that the result of glm(...) is not a numeric vector - it is a complicated object of class glm (try str(m1))

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
0

Shapiro-Wilk Test

shapiro.test(residual)

Kolmogorov-Smirnov Test

ks.test(residual, 'pnorm')

mod.2    <- glm(Failure ~ Lead ,data=Exercise2, family="binomial")
residual <- resid(mod.2,type="working")
Jim
  • 191
  • 6
  • The use of p-value based tests to check normality of residuals is no longer recommended for several reasons. (1) the tests are not powerful enough for small samples when normality might be important, (2) they are too powerful for large samples, when normality is less imprtant due to the central limit theorem, (3) the KS-test is not adequately used in the example above, (4) pre-tests are conceptually questionable anyway. Therefore: think whether normality could at all be possiblea by the way your data are generated, and **use graphical methods** as recommended by @Ben Bolker. – tpetzoldt Nov 26 '21 at 07:03
  • 1
    Totally agree with tpetzoldt. My understanding it that they can be used in an automated fashion (which is difficult for graphical methods), but only if you understand their limitations in your circumstance and only to test for "outrageousness" – Jim Nov 26 '21 at 18:28