函数 lipsitz.test {generalhoslem} 不适用于对象 clm {ordinal}

Function lipsitz.test {generalhoslem} is not working for object clm{ordinal}

我现在正在尝试使用 lipsitz.test {generalhoslem} 测试序数模型的拟合优度。根据document,该函数可以同时处理polr 和clm。但是,当我尝试在 lipsitz.test 函数中使用 clm 时,出现错误。这是一个例子

library("ordinal")
library(generalhoslem)
data("wine")
fm1 <- clm(rating ~ temp * contact, data = wine)
lipsitz.test(fm1)

Error in names(LRstat) <- "LR statistic" : 
'names' attribute [1] must be the same length as the vector [0]
In addition: Warning message:
In lipsitz.test(fm1) :
n/5c < 6. Running this test when n/5c < 6 is not recommended.

有解决办法吗?非常感谢。

我不确定这是否离题并且应该在 CrossValidated 上。这部分是测试编码的问题,部分是测试本身的统计数据。

有两个问题。我刚刚在使用 clm 时发现了代码中的错误,并将修复 CRAN(下面更正的代码)。

然而,示例数据似乎确实存在更根本的问题。基本上,Lipsitz 检验需要使用分组的虚拟变量来拟合新模型。使用此示例拟合新模型时,模型失败,因此未计算某些系数。如果使用 polr,新模型会收到排名不足的警告;如果使用 clm,新模型会收到一条消息,指出两个系数由于奇异性而未拟合。我认为这个示例数据集不适合这种分析。

更正后的代码如下,我使用了一个更大的示例数据集来运行测试。

lipsitz.test <- function (model, g = NULL)  {
  oldmodel <- model
  if (class(oldmodel) == "polr") {
    yhat <- as.data.frame(fitted(oldmodel))
  } else if (class(oldmodel) == "clm") {
    predprob <- oldmodel$model[, 2:ncol(oldmodel$model)]
    yhat <- predict(oldmodel, newdata = predprob, type = "prob")$fit
  } else warning("Model is not of class polr or clm. Test may fail.")
  formula <- formula(oldmodel$terms)
  DNAME <- paste("formula: ", deparse(formula))
  METHOD <- "Lipsitz goodness of fit test for ordinal response models"
  obs <- oldmodel$model[1]
  if (is.null(g)) {
    g <- round(nrow(obs)/(5 * ncol(yhat)))
    if (g < 6) 
      warning("n/5c < 6. Running this test when n/5c < 6 is not recommended.")
  }
  qq <- unique(quantile(1 - yhat[, 1], probs = seq(0, 1, 1/g)))
  cutyhats <- cut(1 - yhat[, 1], breaks = qq, include.lowest = TRUE)
  dfobs <- data.frame(obs, cutyhats)
  dfobsmelt <- melt(dfobs, id.vars = 2)
  observed <- cast(dfobsmelt, cutyhats ~ value, length)
  if (g != nrow(observed)) {
    warning(paste("Not possible to compute", g, "rows. There might be too few observations."))
  }
  oldmodel$model <- cbind(oldmodel$model, cutyhats = dfobs$cutyhats)
  oldmodel$model$grp <- as.factor(vapply(oldmodel$model$cutyhats, 
                                         function(x) which(observed[, 1] == x), 1))
  newmodel <- update(oldmodel, . ~ . + grp, data = oldmodel$model)
  if (class(oldmodel) == "polr") {
    LRstat <- oldmodel$deviance - newmodel$deviance
  } else if (class(oldmodel) == "clm") {
    LRstat <- abs(-2 * (newmodel$logLik - oldmodel$logLik))
  }
  PARAMETER <- g - 1
  PVAL <- 1 - pchisq(LRstat, PARAMETER)
  names(LRstat) <- "LR statistic"
  names(PARAMETER) <- "df"
  structure(list(statistic = LRstat, parameter = PARAMETER, 
                 p.value = PVAL, method = METHOD, data.name = DNAME, newmoddata = oldmodel$model, 
                 predictedprobs = yhat), class = "htest")
}

library(foreign)
dt <- read.dta("http://www.ats.ucla.edu/stat/data/hsbdemo.dta")
fm3 <- clm(ses ~ female + read + write, data = dt)
lipsitz.test(fm3)
fm4 <- polr(ses ~ female + read + write, data = dt)
lipsitz.test(fm4)