predict probability in r

I want to predict the probability in glm ,but the console reported that unexpected symbol in AA which could not be find. How can I solve it?
this is my code.

model2 <- glm(survived ~age+sibsp+parch+pclass+embarked+fare+sex,family=binomial(link='logit'),data=mdata)

head(predict(model2, type = "link"))
#predict probability
head(predict(model2, type = "response"))
AA <- data.frame(sibsp = 2,age =21,parch =2,pclass = 1,embarked ="c", fare = 26.55 sex ="female"))
predict(model2,type = "response",newdata = AA)

The code to create AA is missing a comma after 26.55. So it should be:

AA <- data.frame(sibsp = 2,age =21,parch =2,pclass = 1,embarked ="c", 
                 fare = 26.55, sex ="female"))

But now I have a new question. After i fixed it ,the console reported this for me.

Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) :
factor embarked has new level c

How can i solve it?

You should understand the factor levels of your data, so that when producing new data to predict on that they are factors with the same levels.


Use the levels command to find out the levels that were present when the model was built

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.