0

I am trying to build a binomial logistic regression model. I firstly build the equation, then applied stepwise method to choose the best variables. In the end I am trying to use the function logistic.display to get the coefficients and odds ratio because I want to demonstrate to effect of the variables on the model. when I run the logistic.display function I get error "$ operator is invalid for atomic vectors". Can anyone help? Thank you

model <- glm(decision ~., data = train.data,family = "binomial")

step.model <- stepAIC(model, direction = "both", 
                      trace = FALSE)

logistic.display(step.model)
summary(step.model)

Call:
glm(formula = decision ~ Exposure + VehAge + BonusMalus, family = "binomial", 
    data = train.data)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-0.6911  -0.3210  -0.2710  -0.2261   2.9227  

Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept) -4.66465    0.81457  -5.727 1.03e-08 ***
Exposure     1.16920    0.54048   2.163   0.0305 *  
VehAge      -0.05930    0.03710  -1.599   0.1099    
BonusMalus   0.01961    0.01008   1.946   0.0517 .  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 275.12  on 800  degrees of freedom
Residual deviance: 267.24  on 797  degrees of freedom
AIC: 275.24

Number of Fisher Scoring iterations: 6
class(step.model)
[1] "glm" "lm" 

This is a sample of my train.data

structure(list(id = c(1, 3, 5, 10, 11, 13, 15, 17, 18, 21), 
    var1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), var2 = c(0.1, 
    0.77, 0.75, 0.09, 0.84, 0.52, 0.45, 0.27, 0.71, 0.15), var3 = c("D", 
    "D", "B", "B", "B", "E", "E", "C", "C", "B"), var4 = c(5L, 
    5L, 6L, 7L, 7L, 6L, 6L, 7L, 7L, 7L), var5 = c(0L, 0L, 2L, 
    0L, 0L, 2L, 2L, 0L, 0L, 0L), var6 = c(55L, 55L, 52L, 46L, 
    46L, 38L, 38L, 33L, 33L, 41L), var7 = c(50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 68L, 68L, 50L), var8 = c("B12", "B12", 
    "B12", "B12", "B12", "B12", "B12", "B12", "B12", "B12"), 
    var9 = c("Regular", "Regular", "Diesel", "Diesel", "Diesel", 
    "Regular", "Regular", "Diesel", "Diesel", "Diesel"), var10 = c(1217L, 
    1217L, 54L, 76L, 76L, 3003L, 3003L, 137L, 137L, 60L), var11 = c("R82", 
    "R82", "R22", "R72", "R72", "R31", "R31", "R91", "R91", "R52"
    ), decision = c(1, 1, 1, 1, 0, 1, 1, 0, 1, 1)), row.names = c(NA, 
10L), class = "data.frame")
pipts
  • 87
  • 7
  • `step.model` is probably a list object, try `lapply(step.model, logistic.display)` – jay.sf Feb 25 '22 at 18:46
  • Where is the `logistic.display` function from? It's not part of base R (nor of any packages I have installed) – Ben Bolker Feb 25 '22 at 19:07
  • @jay.sf Now i am getting this error "$ operator is invalid for atomic vectors" I am not sure what else to try . I also tried this for the coefficients ```exp(cbind(coef(step.model), confint(step.model)))``` still get an error Error in UseMethod("vcov") : no applicable method for 'vcov' applied to an object of class "list" – pipts Feb 25 '22 at 19:23
  • @BenBolker I installed this package ```install.packages("epiDisplay")``` and the relevant library – pipts Feb 25 '22 at 19:25
  • @pipts I just ran your code on a dummy data set and it worked as expected. Can you share your data? What do you see when you do `summary(step.model)`? – Allan Cameron Feb 25 '22 at 19:36
  • @pipts No clue, make a [reproducible example](https://stackoverflow.com/a/5963610/6574038). – jay.sf Feb 25 '22 at 19:37
  • @AllanCameron I just add the ```summary(step.model)``` – pipts Feb 25 '22 at 19:50
  • @pipts that is not the output you should get from a glm. What is `class(step.model)`? – Allan Cameron Feb 25 '22 at 19:53
  • @jay.sf i also add a sample of my data, feel free to take a look – pipts Feb 25 '22 at 19:55
  • @AllanCameron this is the output, ```class(step.model) [1] "list"``` I knew that this is a list , and that is the reason that I can calculate the odd ratio and the coefficients. But how am I going to transform it in a form that will allow the ```logistic.display``` run? I have seen other examples of other logistic regression models that were built in the same way but none had similar problems – pipts Feb 25 '22 at 19:59
  • @pipts the data you supplied doesn't run because var8 has only 1 level in this sample. But if you remove it and run your code, you don't get the error you described – Allan Cameron Feb 25 '22 at 19:59
  • I don't think you're showing us everything here. stepAIC on a glm doesn't return a list. It returns a glm object. Have you altered the model or the output of stepAIC in some way that you haven't shown us? – Allan Cameron Feb 25 '22 at 20:02
  • @AllanCameron I ran the model and the lines i posted again and now my results changes I update the summary and the class, feel free to check please. Also I ran the ```logistic.display(model) Error in dimnames(x) <- dn : length of 'dimnames' [1] not equal to array extent In addition: Warning messages: 1: glm.fit: fitted probabilities numerically 0 or 1 occurred 2: glm.fit: fitted probabilities numerically 0 or 1 occurred ``` Also this produced an error```lapply(step.model, logistic.display) Error: $ operator is invalid for atomic vectors``` – pipts Feb 25 '22 at 20:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/242417/discussion-between-pipts-and-allan-cameron). – pipts Feb 25 '22 at 20:48

0 Answers0