1

Using the debt data set from the package faraway. So basically firstly I was asked to create a model using multinom function and use ccarduse as the response and prodebt as the single covariate. Then I had to create a plot of predicted probabilities of belonging to each credit card use category against prodebt score.

mod1 <- multinom(ccarduse ~ prodebt, debt)

inclevels <- 0:464
debt$ccarduse <- as.factor(debt$ccarduse)
debt$prodebt <- as.factor(debt$prodebt)

preds <- predict(mod1, data.frame(income=inclevels), type="probs")
# Warning message:
#   'newdata' had 465 rows but variables found have 464 rows

Plot of predicted probabilities of belonging to each credit card use category against prodebt score

plot(inclevels, preds[, 1], type="l", ylim=c(.15, .6), xlab="prodebt", 
     ylab="Probabilities of credit card use", cex.lab=1.3, cex.axis=1.3)
lines(inclevels, preds[, 2], lty=2)
lines(inclevels, preds[, 3], lty=3)
legend(x="topright", c("1", "2", "3"), lty=c(3, 2, 1), cex=1.5)

As seen I got a warning message and the plot looks really creepy. Is there anything I did wrong or I could fix to make it look neat.

enter image description here

Marco
  • 2,368
  • 6
  • 22
  • 48

1 Answers1

0

Your problem is, that income was not used in the model (or you posted the wrong code?). Apart from that your code actually looks fine for me, and the plot nice. See example using a variable that's actually in the model:

## load data
data('debt', package='faraway')

## calc model
mod1 <- nnet::multinom(ccarduse ~ prodebt, debt)

## predict
prodebtlv <- 1:6
pred <- predict(mod1, data.frame(prodebt=prodebtlv), type="probs")

## plot
plot(prodebtlv, pred[, 1], type="l", ylim=range(pred), 
     xlab="prodebt", ylab="Probabilities of credit card use", 
     cex.lab=1.3, cex.axis=1.3)
lines(prodebtlv, pred[, 2], lty=2)
lines(prodebtlv, pred[, 3], lty=3)
legend(x = "topright", c("1", "2", "3"), lty=c(3, 2, 1))

enter image description here

jay.sf
  • 60,139
  • 8
  • 53
  • 110