Question

I would like to order the variables from the summary table of the glm function by the p-value. any Ideas how to do it?

here is my example data frame:

 dat <- read.table(text = " TargetVar  Var1    Var2       Var3
 0        0        0         7
 0        0        1         1
 0        1        0         3
 0        1        1         7
 1        0        0         5
 1        0        1         1
 1        1        0         0
 1        1        1         6
 0        0        0         8
 0        0        1         5
 1        1        1         4
 0        0        1         2
 1        0        0         9
 1        1        1         2  ", header = TRUE)

The model code is: o<-glm(TargetVar~Var1+Var2+Var3, data=dat,family='binomial') and I used summary(o) to get the list of the variables with their statistics.In this case I got (among other stats):

Coefficients:
            Estimate Std. Error z value Pr(>|z|)
(Intercept)   0.2177     1.5985   0.136    0.892
Var1          1.1932     1.1775   1.013    0.311
Var2         -0.4337     1.2725  -0.341    0.733
Var3         -0.1086     0.2253  -0.482    0.630

How can I find a way to order them by Pr(>|z|) ? Thanks

Was it helpful?

Solution

summary(o)$coefficients[order(summary(o)$coefficients[,4]),]

#              Estimate Std. Error    z value  Pr(>|z|)
#Var1         1.1931750  1.1774564  1.0133497 0.3108932
#Var3        -0.1085742  0.2252867 -0.4819379 0.6298501
#Var2        -0.4337253  1.2724925 -0.3408470 0.7332187
#(Intercept)  0.2177110  1.5984713  0.1361995 0.8916635
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top