使用 desctools 包计算 PseudoR2 返回错误

Using the desctools package to clculate PseudoR2 is returning an error

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')

我得到这个错误代码:

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'

但是,m1 是一个 multinom 对象,我认为它的格式都很好,不是吗?

您需要设置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 
>