I'm trying to calculate predicted probabilities using specific values, but R shows the following error:
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
This is what I was trying to do: x1 is a factor with 12 levels, and x2 is also a factor with 3 levels.
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)
Any help?
The predict() function in R is used to predict the values based on the input data. All the modeling aspects in the R program will make use of the predict() function in their own way, but note that the functionality of the predict() function remains the same irrespective of the case.
Predicting the target values for new observations is implemented the same way as most of the other predict methods in R. In general, all you need to do is call predict ( predict. WrappedModel() ) on the object returned by train() and pass the data you want predictions for.
The predict() function can be used to predict the probability that the market will go up, given values of the predictors. The type="response" option tells R to output probabilities of the form P(Y = 1|X) , as opposed to other information such as the logit .
The multinom function accepts a model formula where the outcome is a vector with a factor representing the response categories, or a matrix with the counts in the various categories, which is the case for us. This is a direct generalization of the way logit models work in R.
You nd
data.frame
should have nine variables, one for each of your x's.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With