Frage

I have a logistic regression model with the coefficients already determined and I want to deploy in R.

I know it is extremely simple to just write my own function to do it, but I'm curious if there is some existing functionality that I am missing that's even simpler?

Basically I am looking to use something like the predict() functionality of glm with my own coefficients rather than fitting the model in R.

War es hilfreich?

Lösung

You can use the matrix form to do this:

logitp_est <- sum(c(1, values) * coefficients)

If you want the probability,

prob_est <- 1 / (1 + exp(-1 * logitp_est))

or the built in plogis():

prob_est <- plogis(logitp_est)

If you want the classification:

class_est <- logitp_est > 0

Andere Tipps

It's not recommended, but you could always change the coefficients by hand.

iris2 <- iris[, 1:4]; iris2$dep_var <- as.integer(ifelse(iris2$Sepal.Length > 5, 1, 0))
x <- glm(dep_var ~ ., family = binomial(link = logit), iris2)
x$coefficients
# (Intercept)  Sepal.Length   Sepal.Width  Petal.Length   Petal.Width
# -1990.9311682   392.5953392     2.0776581     0.5389770     0.9594286
predict(x, iris2[1, ])
#          1
#   19.52332
x$coefficients['Sepal.Length'] <- 393
predict(x, iris2[1, ])
#        1
# 21.58709

Note this will likely screw with things like summary(x).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top