I am trying to make a share prediction using the model that I created (called model3 and using mlogit
) for the following cars:
-Car 1: 4 seats, automatic transmission, convertible, 40
-Car 2: 2 seats, manual transmission, not convertible, 35
Everytime I run it, the code passes through until the last function (the shares one). The following error keeps popping up:
"Error in data.model %*% model$coef: non-conformable arguments"
Does anyone know how to rid of this error? I have provided my code below in case something else is off.
price <- c(40, 35)
seat <- factor(c(4, 2), levels=c(2,4,5))
trans <- factor(c("auto", "manual"), levels=c("auto", "manual"))
convert <- factor(c("yes", "no"), levels=c("no", "yes"))
segment <- factor(c("basic", "basic"), levels=c("basic", "fun", "racer"))
prod <- data.frame(seat, trans, convert, price, segment)
prod
predict_mnl <- function(model, products) {
data.model <- model.matrix(update(model$formula, 0 ~ .),
data = products)[,-1]
utility <- data.model%*%model$coef
share <- exp(utility)/sum(exp(utility))
cbind(share, prod)
}
shares <- predict_mnl(model3, prod)