以计数矩阵作为响应的多项式 [英] Multinom with Matrix of Counts as Response
问题描述
在multinom
软件包nnet
的帮助下,响应应该是一个因子或具有K列的矩阵,这将被解释为K个类中每个类的计数."我尝试在第二种情况下使用此功能,但出现错误.
According to the help of multinom
, package nnet
, "The response should be a factor or a matrix with K columns, which will be interpreted as counts for each of K classes." I tried to use this function in the second case, obtaining an error.
这是我的工作的示例代码:
Here is a sample code of what I do:
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
我该如何解决这个问题?
How can I solve this problem?
奖金问题:我还注意到,我们可以将多项式与因子的预测因子结合使用,例如predictor <- factor(c(1,2,2,3,1,2,3,3,1,2))
.鉴于多项式线性logit回归仅适用于连续或二分预测变量,因此我不知道这在数学上是怎么可能的.
Bonus question: I also noticed that we can use multinom with a predictor of factors, e.g. predictor <- factor(c(1,2,2,3,1,2,3,3,1,2))
. I cannot understand how this is mathematically possible, given that a multinomial linear logit regression should work only with continuous or dichotomous predictors.
推荐答案
获得新变量预测的最简单方法是将新数据定义为data.frame.
The easiest method for obtaining the predictions for a new variable is to define the new data as a 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
这篇关于以计数矩阵作为响应的多项式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!