r - Recreate raw probabilities produced by multinom function -
i wanting use coefficients multi-nomial model create related probabilities manually each class of interest. way can score data observation within database using sql. using nnet package , working through example found.
this example uses hsbdemo data set , outcome variable program type attempts classify 3 classes- general/academic/vocation.
library("foreign") library("nnet") ml <- read.dta("http://www.ats.ucla.edu/stat/data/hsbdemo.dta") test <- multinom(prog2 ~ ses + write, data = ml) summary(test)
now fits model , produces following coefficients:
## call: ## multinom(formula = prog2 ~ ses + write, data = ml) ## ## coefficients: ## (intercept) sesmiddle seshigh write ## general 2.852198 -0.5332810 -1.1628226 -0.0579287 ## vocation 5.218260 0.2913859 -0.9826649 -0.1136037 ## ## std. errors: ## (intercept) sesmiddle seshigh write ## general 1.166441 0.4437323 0.5142196 0.02141097 ## vocation 1.163552 0.4763739 0.5955665 0.02221996 ## ## residual deviance: 359.9635 ## aic: 375.9635
now determine associated probability general class have produced following equation using fitted coeffcients:
obs.linkval<- 2.852198+ -0.5332810*ifelse(as.character(ml$ses)=="middle",1,0)+ -1.1628226*ifelse(as.character(ml$ses)=="high",1,0)+ -0.0579287*ml$write obs.prob<- exp(obs.linkval)/(1+exp(obs.linkval))
however, when @ probabilities fitted model not match have calculated manually. below code return predicted probabilities fitted model. has attempted same?
predict(test, ml, "probs")
Comments
Post a Comment