ATMathew
ATMathew

Reputation: 12856

Plotting a Probability Equation

I ran a logit model and am trying to plot the probability curve. I'm posting the question here and not the stats board because it's more an R question than a stats on, or at least I think so.

My model looks like:

mod1 = glm(factor(status1) ~ our_bid1 + factor(state) + factor(type),
           data=mydat, family=binomial(link="logit"))
print(summary(mod1))

Status1 is a factor with two levels, our_bid ranges from 0 to 20, state is 11 levels (top 10 populous and one which is other), and type has three levels.

To get the predicted probabilities, I ran the following code

all.x1 <- expand.grid(status1=unique(status1), our_bid1=unique(our_bid1),
                      state=unique(state), type=unique(type))

y.hat.new1 <- predict(mod1, newdata=all.x1, type="response")

The problem happens when I am trying to plot the curve. I'm trying to have a general curve for change in our bid given the model.

plot(our_bid1<-000:1600, 
     predict(mod1, newdata=data.frame(our_bid1<-c(000:1600)), type="response"),
     lwd=5, col="blue", type="l")

Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : 
  variable lengths differ (found for 'factor(state)')
In addition: Warning message:
'newdata' had 1601 rows but variable(s) found have 29532 rows 

Do I have to specify all the independent variables in the plot command? What am I doing wrong?

Upvotes: 0

Views: 434

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226587

  • this is not reproducible, it would be a little easier if it were (i.e. give us a working example of mydata)
  • you do need to specify values of all the independent (I prefer "predictor") variables
  • your specification confuses <- and = (which are not quite interchangeable, although they are in the context of assignment): you want something like

(I need some text here so SO will format the code properly)

bidvec <- 0:1600
plot(bidvec,predict(mod1,
    newdata=data.frame(our_bid1=bidvec,
                       state=ref_state,type=ref_type),
                        type="response"), 
lwd=5, col="blue", type="l")

Upvotes: 3

Related Questions