来自nnet :: multinom的预测

时间:2019-07-15 17:06:02

标签: r prediction nnet

在这里我可能只是想念一些愚蠢的东西,但是我似乎无法手动复制此模型中的预测值。我正在关注this example

library('foreign')
library('nnet')
library('tidyverse')
ml <- read.dta("https://stats.idre.ucla.edu/stat/data/hsbdemo.dta")
ml = ml %>% 
  mutate(prog2 = fct_relevel(prog, "academic"))

# Fit a very basic model of the students choice of program 
# as a function of their socioeconmic status and writing score:
test <- multinom(prog2 ~ ses + write, data = ml)
summary(test)

# If we wanted to calculate the probability of a high SES student
# with a median writing score picking a vocational program,
# we should be able to do this:
coef = summary(test)$coefficients[2, c(1, 3:4)]
log_odds = sum(coef * c(1, 1, median(ml$write)))
prob = exp(log_odds)/(1 + exp(log_odds))
prob

# from preditions:
ml %>% 
  bind_cols(as_tibble(predict(test, type = 'probs'))) %>% 
  filter(ses == 'high', write == median(write))

我从手动计算中得到13.0%,而预测函数得到10.8%。我想念什么?

0 个答案:

没有答案
相关问题