Using the desctools package to clculate PseudoR2 is returning an error

1.1k views Asked by At
var1<-sample(c('A', 'B', 'C'), replace = T, size=100)
var2<-sample(c(0,1), size=100, replace=T)
var3<-rnorm(100, mean=10, sd=2)
df<-data.frame(var1, var2, var3)
library(nnet)
m1<-multinom(var1~var2, data=df)
m2<-multinom(var1~var3, data=df)
m3<-multinom(var1~var2+var3, data=df)
#install.packages('DescTools')
library(DescTools)

PseudoR2(m1, which='all')

I get this error code:

Error in as.data.frame.default(data, optional = TRUE) : cannot coerce class ‘"function"’ to a data.frame In addition: Warning message: In PseudoR2(m1, which = "all") : Could not find model or data element of multinom object for evaluating PseudoR2 null model. Will fit null model with new evaluation of 'df'. Ensure object has not changed since initial call, or try running multinom with 'model = TRUE'

However, m1 is a multinom object and I think it's all well formatted, no?

1

There are 1 answers

0
LocoGris On BEST ANSWER

You need to set model=T:

var1<-sample(c('A', 'B', 'C'), replace = T, size=100)
var2<-sample(c(0,1), size=100, replace=T)
var3<-rnorm(100, mean=10, sd=2)
df<-data.frame(var1, var2, var3)
library(nnet)
m1<-multinom(var1~var2, data=df, model=T)
m2<-multinom(var1~var3, data=df)
m3<-multinom(var1~var2+var3, data=df)
#install.packages('DescTools')
library(DescTools)

PseudoR2(m1, which='all')

PseudoR2(m1, which='all')
       McFadden     McFaddenAdj        CoxSnell      Nagelkerke   AldrichNelson VeallZimmermann           Efron McKelveyZavoina            Tjur 
     0.00623116     -0.03030106      0.01355262      0.01526082              NA              NA              NA              NA              NA 
            AIC             BIC          logLik         logLik0              G2 
   225.62024056    236.04092130   -108.81012028   -109.49238485      1.36452914 
>