2012-07-30 78 views
2

我試圖用具體數值預測概率來計算multinom()預測,但-r顯示以下錯誤:如何使用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) 

任何幫助?

+0

你可以給我們一個'dta'的小樣本,裏面有'dput(head(dta))'。 – nograpes 2012-07-30 21:20:53

+0

此外,它看起來像適合九'x's的模型,但是您試圖預測超過九個變量。你應該只在你的'nd'變量中有9個變量。 – nograpes 2012-07-30 21:30:45

回答

2

nddata.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)