概率预测与模型平均累积链接混合模型在顺序包中配置clmm

时间:2017-02-10 01:43:49

标签: r predict mixed-models ordinal

我发现predict函数目前尚未在使用clmm R包中的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)

我收到以下错误消息:  在UseMethod(“预测”)中:  predict适用于“clmm”类对象的适用方法

 ##predict with model average
 predict(mm.avg, new.data)

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

1 个答案:

答案 0 :(得分:0)

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

这里的解决方案: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
}