0
votes

I would like to plot each of the variables that are part of the glm model, where the y axis is the predicted probability and the x axis is the variable levels or values. Here is my code that I tried in order to do it:

The data:

dat <- read.table(text = "target apcalc    admit   num
     0        0        0         21
     0        0        1         24
     0        1        0         55
     0        1        1         72
     1        0        0         5
     1        0        1         31
     1        1        0         11
     1        1        1         3",  header = TRUE)

The glm model:

f<-glm(target ~ apcalc + admit +num, data = dat,family=binomial(link='logit'))

The loop to present the desired plot:

for(i in 1:length(f$var.names)){
          plot(predict(f,i.var.names=i,newdata=dat,type='response'))
      }

I got a strange plot as an output ("Index" in the x axis and "predict(f,i.var.names=i,newdata=dat,type='response')" in the y axis. How can I fix my code in order to get the desired result? (I don't the reputation yet in order to present it here)

2

2 Answers

1
votes

Heres plotting all your variables with the predicted probability,

f<-glm(target ~ apcalc + admit +num, data=dat,family=binomial(link="logit"))

PredProb=predict(f,type='response') #predicting probabilities

par(mfrow=c(2,2))
for(i in names(dat)){
  plot(dat[,i],PredProb,xlab=i)
}
0
votes

On running the f<-glm(.....) part, f$var.names is giving NULL as output. There must be some error there.

f<-glm(target ~ apcalc + admit +num, data=dat,family=binomial("logit"))

f

Call:  glm(formula = target ~ apcalc + admit + num, family = binomial("logit"), 
    data = dat)

Coefficients:
(Intercept)       apcalc        admit          num  
     2.2690       3.1742       2.4406      -0.1721  

Degrees of Freedom: 7 Total (i.e. Null);  4 Residual
Null Deviance:      11.09 
Residual Deviance: 5.172        AIC: 13.17

f$var.names
NULL