订阅分配中不允许使用NA:预测概率时出错

时间:2017-07-06 23:49:00

标签: r na predict nnet

当在预测选择一组二元互斥结果的概率的模型上使用预测时,我收到以下错误。使用nnet包的multinom功能。

  

predict.multinom(model_name,df.predict,“probs”)出错:   订阅分配中不允许使用NA另外:警告   消息:'newdata'有5行,但找到的变量有100行

这是一个可重复的例子:

require(nnet)

response1 <- sample(runif(100))
response2 <- 1-response1
responses <- as.matrix(data.frame(response1 = response1, response2 = response2))

train <- data.matrix(data.frame(var1 = runif(100), var2 = runif(100)))

multinom.mod <- multinom(responses ~ train)

test.df <- data.frame(var1 = runif(5), var2 = runif(5))
predict.vec <- predict(multinom.mod, test.df)

正如您所看到的,问题是我的回答包含2个变量。它似乎比我预测的行数少于训练集中的行数时,该函数尝试将训练集中的响应变量与测试集连接起来。

更新:

以下适用于新的预测集。但是,响应变量被视为分类变量,因此预测不正确:

require(nnet)

train <- data.frame(response1 = sample(runif(100)), response2 = 1-response1, var1 = runif(100), var2 = runif(100))

multinom.mod <- multinom(response1 + response2 ~ ., train, type = "probs")

test.df <- data.frame(var1 = runif(5), var2 = runif(5))

predict.vec <- predict(multinom.mod, test.df)

1 个答案:

答案 0 :(得分:1)

如果您想预测每类响应的概率,您应该使用:

predict.vec <- predict(multinom.mod, test.df, type = "probs")

否则,预测默认为课程type = class

更新,完整用法(培训和预测)应如下所示:

require(nnet)

response1 <- sample(runif(100))
response2 <- 1 - response1

train <- data.frame(var1 = runif(100), var2 = runif(100))
# train with matrix
responses <- cbind(response1, response2)
multinom.mod <- multinom(responses ~ var1 + var2, train, type = "probs")
# train with category
train$response <- ifelse(response1 > response2, "response1", "response2")
multinom.mod1 <- multinom(response ~ var1 + var2, train)

test.df <- data.frame(var1 = runif(5), var2 = runif(5))
# no matter which training method you use,
# you can predict class (default) or probability
predict.cvec <- predict(multinom.mod, test.df, type = "class")
predict.pvec <- predict(multinom.mod, test.df, type = "probs")

predict.cvec1 <- predict(multinom.mod1, test.df, type = "class")
predict.pvec1 <- predict(multinom.mod1, test.df, type = "probs")