rashf
rashf

Reputation: 37

Predict mean response for a logistic regression model in R

I trained a logistic regression model in R using the glm function

model<-glm(df1$deny~df1$dir+df1$hir+df1$lvr+df1$ccs+df1$mcs+df1$pbcr+df1$dmi+df1$self+df1$single+df1$uria+df1$condominium+df1$black,data=df1,family='binomial')

Now i want to get the mean response for a data point

test<-c(0.59,0.24,0.941177,3,2,0,1,0,0,10.6,1,1)

the test data points are the respective predictors as in the model. i.e. dir = 0.59, hir = 0.24...

How to obtain the mean response in this case?

Upvotes: 1

Views: 286

Answers (2)

rashf
rashf

Reputation: 37

Sorted. I did

df.test<- df1[0,-13]
head(df.test)
test<- c(0.59,0.24,0.941177,3,2,0,1,0,0,10.6,1,1)
df.test[nrow(df.test)+1,]=test
pred<- model.1 %>% predict(df.test,type='response')
pred

Upvotes: 1

Ben Bolker
Ben Bolker

Reputation: 226027

model <- glm(deny~dir+hir+lvr+ccs+mcs+pbcr+dmi+
                self+single+uria+condominium+black,
              data=df1,family='binomial')
test <- c(0.59,0.24,0.941177,3,2,0,1,0,0,10.6,1,1)

You can either use the model definition:

X <- matrix(c(1, test), nrow = 1)
beta <- coef(model)
drop(plogis(X %*% beta))

or

dftest <- as.data.frame(X)
names(dftest) <- c("dir", "hir", "lvr", "ccs", ...)

(you need to complete the list of names yourself, I'm lazy)

or possibly

names(dftest) <- setdiff(names(df1), "deny")

if the model variables match the order etc. of the data frame

Then:

predict(model, newdata = dftest, type = "response")

Upvotes: 1

Related Questions