3

根据multinom, package的帮助nnet,“响应应该是具有 K 列的因子或矩阵,这将被解释为每个 K 类的计数。” 我尝试在第二种情况下使用这个函数,得到一个错误。

这是我所做的示例代码:

response  <- matrix(round(runif(200,0,1)*100),ncol=20) # 10x20 matrix of counts
predictor <- runif(10,0,1)
fit1 <- multinom(response ~ predictor)
weights1 <- predict(fit1, newdata = 0.5, "probs")

在这里我得到:

'newdata' had 1 row but variables found have 10 rows

我怎么解决这个问题?

额外的问题:我还注意到我们可以使用带有因子预测器的多项式,例如predictor <- factor(c(1,2,2,3,1,2,3,3,1,2)). 我无法理解这在数学上是如何可能的,因为多项式线性 logit 回归应该只适用于连续或二分预测变量。

4

1 回答 1

0

获得新变量预测的最简单方法是将新数据定义为 data.frame。

使用示例代码

> predict(fit1, newdata = data.frame(predictor = 0.5), type = "probs")
 [1] 0.07231972 0.05604055 0.05932186 0.07318140 0.03980245 0.06785690 0.03951593 0.02663618
 [9] 0.04490844 0.04683919 0.02298260 0.04801870 0.05559221 0.04209283 0.03799946 0.06406533
[17] 0.04509723 0.02197840 0.06686314 0.06888748
于 2014-10-27T22:18:04.237 回答