How to plot glm model coefficients with abline in R? -


i'm struggling plot cofficients of glm model using abline. lets take simple 2d example:

d <- iris[51:150, c(3:4,5)] d[,3] <- factor(d[,3]) plot(d[,1:2], col=d[,3]) 

the data

the glm model yields 4 coefficients:

m <- glm(formula = species~petal.length*petal.width, data = d, family = "binomial") m$coefficients # (intercept)             petal.length              petal.width petal.length:petal.width  #  -131.23813                 22.93553                 63.63527                -10.63606  

how plot simple abline?

binomial models not set this. have single 0|1 response variable (i.e. predict whether sample in single species). maybe because have 2 species included in model, still seems work (this not case when 3 spp included). second trick predict type="response" , round these values discrete predictions:

d$pred <- factor(levels(d[,3])[round(predict(m, type="response"))+1]) plot(d[,1:2], col=d[,3]) points(d[,1:2], col=d$pred, pch=4) 

enter image description here

here i've added "x" predictions. if color same, prediction correct. count 5 samples prediction incorrect.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -