gpt4 book ai didi

r - 如何在 R 中使用 multinom() 进行预测

转载 作者:行者123 更新时间:2023-12-03 21:27:51 24 4
gpt4 key购买 nike

我正在尝试使用特定值计算预测概率,但 R 显示以下错误:

Error in model.frame.default(Terms, newdata, na.action = na.omit, xlev = object$xlevels) : 
variable lengths differ (found for 'x')
In addition: Warning message:
'newdata' had 1 rows but variable(s) found have 513 rows

这就是我想要做的:x1 是一个有 12 个级别的因子,而 x2 也是一个有 3 个级别的因子。
res4 <- multinom(y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 - 1, data=dta, Hess=T)

nd <- data.frame(x11=0.10331384, x12=0.07992203, x13=0.06237817, x14=0.03313840, x15=0.12280702, x16=0.07407407, x17=0.07407407, x18=0.10331384, x19=0.08966862, x110=0.07017544, x111=0.15009747, x112=0.03703704, x22=1, x23=0, x3=1, x4=1, x5=mean(x5), x6=mean(x6, na.rm=T), x7=mean(x7), x8=mean(x8), x9=mean(x9))

predict(res4, type="probs", newdata=nd)

有什么帮助吗?

最佳答案

nd data.frame应该有九个变量,每个 x 一个。

library(nnet)
dta=data.frame(replicate(10,runif(10)))
names(dta)=c('y',paste0('x',1:9))
res4 <- multinom(y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 - 1, data=dta, Hess=T)
nd <- data.frame(x1=0.10331384, x2=0.07992203, x3=0.06237817, x4=0.03313840, x5=0.12280702, x6=0.07407407, x7=0.07407407, x8=0.10331384, x9=0.08966862)
predict(res4, type="probs", newdata=nd)

关于r - 如何在 R 中使用 multinom() 进行预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11729547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com