data.model %*% model$coef 中的错误:不符合要求的参数

Error in data.model %*% model$coef : non-conformable arguments

提问人:Constanza Avalos 提问时间:8/29/2023 更新时间:8/29/2023 访问量:10

问:

我希望你一切安好。我正在对 logit 回归进行建模,但我的代码中存在错误。每次运行模型时,我都会得到以下信息:data.model %*% model$coef 中的错误:不合格的参数。你能帮我解决这个问题吗?提前致谢。

请看我的代码:

library(devtools)
library(mlogit)
library(dfidx)# install and add a column with unique question numbers, as needed in mlogit 1.1+
cbc.df$chid <- rep(1:(nrow(cbc.df)/3), each=3)
cbc.mlogit <- dfidx(cbc.df, choice="choice", idx=list(c("chid", "resp.id"), "alt" )) # shape the data for mlogit

m1 <- mlogit(choice ~ 0 + cereal_label + bill + quantity + price, data = cbc.mlogit)
summary(m1) 
m2 <- mlogit(choice ~ cereal_label + bill + quantity + price, data = cbc.mlogit)
summary(m2) 
m3 <- mlogit(choice ~ cereal_label + bill + quantity + as.numeric(as.character(price)), data = cbc.mlogit)
summary(m2)

lrtest(m1, m2, m3)

predict.mnl <- function(model, data) {
  data.model <- model.matrix(update(model$formula, 0 ~ .), data = data) [, -1]
  utility <- data.model %*% model$coef
  share <- exp(utility) / sum(exp(utility))
  cbind(share, data)
}

(new.data <- expand.grid(attrib) [c(8, 1, 3, 41, 49, 26), ])
predict.mnl(m3, new.data)
Error in data.model %*% model$coef : non-conformable arguments

我必须找到解决此错误的方法。

R mlogit choicefield

评论


答: 暂无答案