使用nnet软件包评估R中多项式logit的拟合优度 [英] Assesing the goodness of fit for the multinomial logit in R with the nnet package

查看:480
本文介绍了使用nnet软件包评估R中多项式logit的拟合优度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用nnet包中的multinom()函数在R中运行多项式逻辑回归.nnet包不包括p值计算和t统计计算.我找到了一种使用此页面.举一个为多项式logit计算测试统计量的例子(实际上不是t-stat,而是等效的),我计算Wald的统计量:

I use the multinom() function from the nnet package to run the multinomial logistic regression in R. The nnet package does not include p-value calculation and t-statistic calculation. I found a way to calculate the p-values using the two tailed z-test from this page. To give one example of calculating a test statistic for a multinom logit (not really a t-stat, but an equivalent) I calculate the Wald's statistic:

mm<-multinom(Empst ~ Agegroup + Marst + Education + State, 
             data = temp,weight=Weight)
W <- (summary(mm1)$coefficients)^2/(summary(mm1)$standard.errors)^2

我取一个系数的平方,然后除以该系数的标准误差的平方.但是,似然比检验是对逻辑回归拟合优度的较好度量.由于对似然函数的不完全了解,我不知道如何编写用于计算每个系数的似然比统计的代码.使用multinom()函数的输出为每个系数计算似然比统计量的方法是什么?感谢您的帮助.

I take the square of a coefficient and divide by the square of the coefficient's standard error. However, the likelihood-ratio test is the preferable measure of a goodness of fit for the logistic regressions. I do not know how to write code that will calculate the likelihood ratio statistic for each coefficient due to the incomplete understanding of the likelihood function. What would be the way to calculate the likelihood-ratio statistic for each coefficient using the output from the multinom() function? Thanks for your help.

推荐答案

让我们看一下使用Species(分类变量)和Petal.Length(连续变量)从虹膜数据集中预测Sepal.Length的方法.让我们开始使用model.matrix将因子变量转换为多个二进制变量并构建我们的神经网络:

Let's look at predicting Sepal.Length from the iris dataset using Species (a categorical variable) and Petal.Length (a continuous variable). Let's start by converting our factor variable into multiple binary variables using model.matrix and building our neural network:

library(nnet)
data(iris)
mat <- as.data.frame(model.matrix(~Species+Petal.Length+Sepal.Length, data=iris))
mm <- multinom(Sepal.Length~.+0, data=mat, trace=F)

现在,我们可以对模型中的变量进行似然比检验:

Now we can run a likelihood ratio test for a variable in our model:

library(lmtest)
lrtest(mm, "Speciesversicolor")
# Likelihood ratio test
# 
# Model 1: Sepal.Length ~ `(Intercept)` + Speciesversicolor + Speciesvirginica + 
#     Petal.Length + 0
# Model 2: Sepal.Length ~ `(Intercept)` + Speciesvirginica + Petal.Length - 
#     1
#   #Df  LogLik  Df  Chisq Pr(>Chisq)
# 1 136 -342.02                      
# 2 102 -346.75 -34 9.4592          1

要对所有变量运行似然比测试,我想您可以使用循环并为每个变量名称运行.在此循环中,我仅提取了p值.

To run the likelihood ratio test for all your variables, I guess you could just use a loop and run for each variable name. I've extracted just the p-values in this loop.

for (var in mm$coefnames[-1]) {
  print(paste(var, "--", lrtest(mm, var)[[5]][2]))
}
# [1] "Speciesversicolor -- 0.999990077592342"
# [1] "Speciesvirginica -- 0.998742545590864"
# [1] "Petal.Length -- 3.36995663002528e-14"

这篇关于使用nnet软件包评估R中多项式logit的拟合优度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆