使用序数包中装有 clmm 的模型平均累积链接混合模型进行概率预测

Joe*_*Joe 6 r ordinal predict mixed-models

我发现该predict函数目前尚未在使用R包clmm中的函数拟合的累积链接混合模型中实现ordinal。虽然在同一个包中predict实现clmm2,但我选择应用clmm,因为后者允许多个随机效果。此外,我还安装了几个clmm模型并使用包model.avg中的函数执行模型平均MuMIn。理想情况下,我想使用平均模型来预测概率。然而,虽然MuMIn支持clmm模型,predict也不适用于普通模型。

有没有办法破解该predict函数,以便该函数不仅可以预测clmm模型的概率,还可以使用模型平均系数clmm(即“平均”类的对象)进行预测?例如:

require(ordinal)
require(MuMIn)

mm1 <- clmm(SURENESS ~ PROD + (1|RESP) + (1|RESP:PROD), data = soup,
        link = "probit", threshold = "equidistant")

## test random effect:
mm2 <- clmm(SURENESS ~ PROD + (1|RESP) + (1|RESP:PROD), data = soup,
        link = "logistic", threshold = "equidistant")

#create a model selection object
mm.sel<-model.sel(mm1,mm2)

##perform a model average
mm.avg<-model.avg(mm.sel)


#create new data and predict
new.data<-soup

##predict with indivindual model
predict(mm1, new.data)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息: In UseMethod("predict") : 没有适用的方法应用于predict类“clmm”的对象

 ##predict with model average
 predict(mm.avg, new.data)
Run Code Online (Sandbox Code Playgroud)

返回另一个错误: Predict.averaging(mm.avg, new.data) 中的错误: predict对于模型 'mm1' 和 'mm2' 导致错误

小智 1

我找到了一个潜在的解决方案(粘贴在下面),但无法为我的数据工作。

解决方案在这里:https://gist.github.com/mainambui/c803aaf857e54a5c9089ea05f91473bc

我认为问题在于我正在使用的系数数量,但我没有足够的经验来弄清楚。希望这可以帮助某人。

这是我正在使用的模型和新数据,尽管它实际上是模型平均版本。但预测因子相同。

ma10 <- clmm(Location3 ~ Sex * Grass3 + Sex * Forb3 + (1|Tag_ID), data = 
IP_all_dunes)
ma_1 <- model.avg(ma10, ma8, ma5)##top 3 models
new_ma<- data.frame(Sex = c("m","f","m","f","m","f","m","f"),
                Grass3 = c("1","1","1","1","0","0","0","0"),
                Forb3 = c("0","0","1","1","0","0","1","1"))


# Arguments:
#  - model = a clmm model
#  - modelAvg = a clmm model average (object of class averaging)
#  - newdata = a dataframe of new data to apply the model to
# Returns a dataframe of predicted probabilities for each row and response level
fake.predict.clmm <- function(modelAvg, newdata) {
  # Actual prediction function
  pred <- function(eta, theta, cat = 1:(length(theta) + 1), inv.link = plogis) {
    Theta <- c(-1000, theta, 1000)
    sapply(cat, function(j) inv.link(Theta[j + 1] - eta) - inv.link(Theta[j] - 
eta))
  }

  # Multiply each row by the coefficients
  #coefs <- c(model$beta, unlist(model$ST))##turn off if a model average is used
  beta <- modelAvg$coefficients[2,3:12]
  coefs <- c(beta, unlist(modelAvg$ST))

  xbetas <- sweep(newdata, MARGIN=2, coefs, `*`)

  # Make predictions
  Theta<-modelAvg$coefficients[2,1:2]
  #pred.mat <- data.frame(pred(eta=rowSums(xbetas), theta=model$Theta))
  pred.mat <- data.frame(pred(eta=rowSums(xbetas), theta=Theta))
  #colnames(pred.mat) <- levels(model$model[,1])
  a<-attr(modelAvg, "modelList")
  colnames(pred.mat) <- levels(a[[1]]$model[,1])

  pred.mat
}
Run Code Online (Sandbox Code Playgroud)