Plot from multinomial regression does not work

Hey there!

I try to generate a graph from my multinomial regression model as on this website (https://www.politikwissenschaften.ch/blog.php?id=17) However, something seems to be wrong as I always get error messages.

This is my code:

voxit_destill_1996$revenu <- as.factor(voxit_destill_1996$revenu)

dc = predicts(mod.fak_diff_1996.7,"F", position = 1 )

mod.fak_diff_1996.7 <- multinom(factor(fac.num)~ revenu, data=voxit_destill_1996)
pred.probs <- predict(mod.fak_diff_1996.7, type = "probs")
pred.probs <-as.data.frame(pred.probs)

plot.probs.1996 <- ggplot(pred.probs, aes(x = revenu ,color = level)) + geom_point(aes(y=mean)) +
geom_errorbar(aes(ymin = lower, ymax = upper)) + theme_minimal() +
ylab("Vorausgesagte Wahrscheinlichkeit") + xlab("Einkommen") +
scale_y_continuous(labels = percent)

Would be very greateful if someone could help me.

Thanks,
Johanna

Hi Johanna, hopefully the forum users can help you.
Please tell us the error messages you see.

The error message is "Fehler in FUN(X[[i]], ...) : Objekt 'revenu' nicht gefunden" - "object 'revenu' not found" as soon as I want to look at the generated plot. :confused:

I'm finding it hard to understand your code.
The first line, recasts revenu as a factor variable, ok.

the line after would seem to want to predict something using a model that is not yet declared (because there seems to be a declaration the line below.), but also dc doesnt seem to be referenced elsewhere. so can this simply be removed?

Then you fit a model (you implicitly cast fac.num as factor, in contrast to your more explicit approach for revenu, I personally would prefer a consistent approach, though its not an issue technically.)
However pred.probs, would only contain the predictions, so when you ggplot you fail to have any data aside from the predictions. revenu is not a name of a prediction level. you can add/bind revenu back in so that its there.

However then your ggplot also tries to plot other things that are not present (and its not clear where they would come from) i.e 'mean' and 'level'.

Perhaps there is a relevant different between your use of predict and the tutorials use of glm.predicts predicts functions.

Here is a version of your code that gets you up to where plot creation might begin, but I would pause thinking about what to plot before addressing the model's apparent strategy of always predicting class 2 for fac.num.

library(nnet)
library(haven)
library(tidyverse)
voxit_destill_1996<-haven::read_spss("voxit_destill_1996.sav")

input_data <- data.frame(
  revenu=as.factor(voxit_destill_1996$revenu),
fac.num=as.factor(voxit_destill_1996$fac.num)
)

input_data_no_na <- na.omit(input_data)
# dc = predicts(mod.fak_diff_1996.7,"F", position = 1 )

mod.fak_diff_1996.7 <- multinom(fac.num ~ revenu, data=input_data_no_na)
pred.probs <- predict(mod.fak_diff_1996.7, type = "probs")
pred.probs <-as.data.frame(pred.probs)
pred.probs$prediction <- apply(pred.probs, 1, which.is.max)
table(pred.probs$prediction) ## unfortunately your model only predicts '2' so probably isnt a great model...
(pred.probs <- bind_cols(pred.probs,input_data_no_na))

Well, I really did the same thing as here: https://www.politikwissenschaften.ch/blog.php?id=17

I don't know what to do with your response.
Sorry if what I told you wasn't helpful for you.
Its the best I could do for you, and I think I gave useful information and code

2 Likes

Aha, know, I know why there was an error message: Unfortunately, R saved "revenu" as "factor(revenu" I don't know why... anyway, as soon as I changed that, the plot worked :slight_smile:

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