When I fit a logistic regression model with glm
, I can specify type = "response"
to get predicted probabilities.
model <- glm(formula= vs ~ wt + disp, data=mtcars, family=binomial)
newdata = data.frame(wt = 2.1, disp = 180)
predict(model, newdata, type="response")
1
0.2361081
I'm experimenting with the logistic regression function in a new package, RSSL
. Below is some sample code (from the documentation)
library(RSSL)
set.seed(1)
df <- generateSlicedCookie(1000,expected=FALSE) %>%
add_missinglabels_mar(Class~.,0.98)
class_lr <- LogisticRegression(Class~.,df,lambda = 0.01)
df_test <- generateSlicedCookie(1000,expected=FALSE)
predict(class_lr,df_test)
Using predict
on the class_lr
object gives me the class labels. And using predict(class_lr,df_test, type = "response")
results in an error. Is there a way to get R to output the predicted probabilities?
Looking at thesource code of LogisticRegression , for predict, it calculates the prediction in log-odds ratio and converts it to probabilities and returns only the class, so there's no option for type="response"
:
setMethod("predict", signature(object="LogisticRegression"), function(object, newdata) {
ModelVariables<-PreProcessingPredict(object@modelform,newdata,scaling=object@scaling,intercept=object@intercept)
X<-ModelVariables$X
w <- matrix(object@w, nrow=ncol(X))
expscore <- exp(cbind(rep(0,nrow(X)), X %*% w))
probabilities <- expscore/rowSums(expscore)
# If we need to return classes
classes <- factor(apply(probabilities,1,which.max),levels=1:length(object@classnames), labels=object@classnames)
return(classes)
})
Another method associated with this class is posterior
, and you can see the code is very similar, and it returns the probabilities in exp form:
setMethod("posterior", signature(object="LogisticRegression"), function(object,newdata) {
ModelVariables<-PreProcessingPredict(modelform=object@modelform,
newdata=newdata,
y=NULL,
scaling=object@scaling,
intercept=object@intercept)
X<-ModelVariables$X
w <- matrix(object@w, nrow=ncol(X))
expscore <- exp(cbind(rep(0,nrow(X)), X %*% w))
posteriors <- expscore/rowSums(expscore)
posteriors <- exp(posteriors)
colnames(posteriors) <- object@classnames
return(posteriors)
})
Apologies for the slightly long answer, if you need the probabilities, you can do:
probs = log(posterior(class_lr,df_test))
The first column is probability of being in the first class, and so on for second column. To check that the labels are similar:
pred_labels = predict(class_lr,df_test)
table(apply(probs,1,which.max) == as.numeric(pred_labels))
TRUE
1000
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.