I'm doing a count data analysis on R and wish to find the best model for negative binomial regression using AIC. Here is the data (under the name "doctor"):
V2 V3 L4 V5
1 1 32 10.866795 1
2 2 104 10.674706 1
3 3 206 10.261581 1
4 4 186 9.446440 1
5 5 102 8.578665 1
6 1 2 9.841080 2
7 2 12 9.275472 2
8 3 28 8.649974 2
9 4 28 7.857481 2
10 5 31 7.287561 2
I first performed a stepwise AIC to find the best model, using the code below:
out0=glm.nb(V3~1,data=doctor)
library(MASS)
stepAIC(out0,V3~V2 L4 V5,direction=c("both"))
As a result, I get this:
Step: AIC=87.91
V3 ~ V5 V2 L4
Df AIC
<none> 87.907
- V5 1 88.587
- L4 1 94.928
- V2 1 97.552
Call: glm.nb(formula = V3 ~ V5 V2 L4, data = doctor, init.theta = 51.92790127,
link = log)
Coefficients:
(Intercept) V5 V2 L4
-24.568 1.434 1.704 2.275
Degrees of Freedom: 9 Total (i.e. Null); 6 Residual
Null Deviance: 286.1
Residual Deviance: 17.46 AIC: 89.91
But when I save this model under model=glm.nb(V3~V2 L4 V5,data=doctor)
and type AIC(model)
, I get an AIC of 89.91. Why is this the case?
CodePudding user response:
As mentioned they are the same. Look at the bottom of the output
df <- data.frame(V2=c(1,2,3,4,5,1,2,3,4,5),V3=c(32,104,206,186,102,2,12,28,28,31),L4=c(10.866795,10.674706,10.261581,9.446440,8.578665,9.841080,9.275472,8.649974,7.857481,7.287561),V5=c(1,1,1,1,1,2,2,2,2,2))
df
V2 V3 L4 V5
1 1 32 10.8668 1
2 2 104 10.6747 1
3 3 206 10.2616 1
4 4 186 9.4464 1
5 5 102 8.5787 1
6 1 2 9.8411 2
7 2 12 9.2755 2
8 3 28 8.6500 2
9 4 28 7.8575 2
10 5 31 7.2876 2
out0=glm.nb(V3~1,data=df)
AICout <- stepAIC(out0,V3~V2 L4 V5,direction=c("both"))
output
AICout$aic
[1] 89.907
Now saving the model
model = glm.nb(V3~V5 V2 L4,data=df)
output
AIC(model)
[1] 89.907