如何使用多项式逻辑回归模型来预测未来的观察结果

How to use a multinomial logistic regression model to predict future observations

我的问题似乎有点含糊,所以我将提供背景上下文和我的可重现代码来尝试澄清。

我有兴趣class根据每个社区的社会经济指标来确定城市各个社区的犯罪事件。我的最终目标是能够生成一个相当准确的预测,该预测将建议下一次犯罪最有可能发生的街区。我选择拟合多项式回归模型,但我很难解释其结果。

我的数据如下:

> str(df)
'data.frame':   1796 obs. of  12 variables:
$ Time           : chr  "14:37:00" "14:37:00" "16:23:00" "00:10:00" ...
$ Neighbourhood  : chr  "Grand Boulevard" "Grand Boulevard" "West Town" "West Englewood" ...
$ Population     : num  22209 22209 84698 26346 24976 ...
$ Area           : num  1.74 1.74 4.58 3.15 2.55 2.95 3.15 1.04 7.15 1.28 ...
$ Density        : chr  "12,763.79" "12,763.79" "18,493.01" "8,363.81" ...
$ Crowded.Housing: num  3.3 3.3 2.3 4.8 2.7 3.3 4.8 2.4 6.3 9.4 ...
$ Poverty        : num  29.3 29.3 14.7 34.4 8.9 27.8 34.4 21.7 28.6 41.7 ...
$ Unemployment   : num  24.3 24.3 6.6 35.9 9.5 24 35.9 15.7 22.6 25.8 ...
$ Education      : num  15.9 15.9 12.9 26.3 18.8 14.5 26.3 11.3 24.4 24.5 ...
$ Age            : num  39.5 39.5 21.7 40.7 37.6 40.3 40.7 35.4 37.9 43.6 ...
$ Income         : num  23472 23472 43198 11317 25113 ...
$ Hardship       : num  57 57 10 89 29 60 89 26 73 92 ...

这是我的模型的代码:

c.nnet = nnet::multinom(Neighbourhood ~
                         Crowded.Housing +
                         Poverty +
                         Unemployment +
                         Education +
                         Income +
                         Hardship,
                         data = df,
                         MaxNWts = 100000) 

以下是一些class化准确度指标:

> odds <- c.nnet[["fitted.values"]]
> pd = predict(c.nnet,type="class")
> table = table(df$Neighbourhood, pd); classAgreement(table)
$diag
[1] 0.6631403

$kappa
[1] 0.6451884

$rand
[1] 0.9560459

$crand
[1] 0.6035169

> sum(diag(table))/sum(table) 
[1] 0.6631403

最后,这里是预测的 classes 和关联的 class 概率的输出。

>head(pd)
[1] Chatham        Chatham        West Town      West Englewood New City       Chatham       
72 Levels: Albany Park Archer Heights Armour Square Ashburn Auburn Gresham Austin Avalon Park Avondale Belmont Cragin Bridgeport Brighton Park ... Woodlaw

> head(odds)
   Albany Park Archer Heights Armour Square      Ashburn Auburn Gresham       Austin  Avalon Park     Avondale Belmont Cragin   Bridgeport Brighton Park
1 8.293444e-04   3.078169e-04  3.394213e-04 5.070003e-04   0.0333699087 8.205015e-03 0.0140058699 3.519157e-04   0.0005199967 3.962345e-04  1.796575e-05
2 8.293444e-04   3.078169e-04  3.394213e-04 5.070003e-04   0.0333699087 8.205015e-03 0.0140058699 3.519157e-04   0.0005199967 3.962345e-04  1.796575e-05
3 7.276802e-04   2.796196e-06  1.540627e-03 9.642981e-03   0.0001623333 4.575838e-05 0.0004173684 1.229428e-03   0.0007718075 2.308536e-02  9.021844e-03
4 7.168266e-05   7.869570e-04  1.743114e-05 3.519012e-05   0.0473000895 9.256728e-02 0.0058524740 4.373425e-05   0.0002943829 4.752441e-06  6.214005e-07
5 2.376865e-03   3.647976e-04  3.261888e-03 5.958128e-02   0.0090540446 4.103546e-02 0.0028125946 9.329274e-03   0.0339153709 1.394973e-02  9.034131e-02
6 7.735586e-04   5.958576e-04  2.345032e-04 4.058962e-04   0.0833015893 2.374063e-02 0.0169124221 3.038695e-04   0.0005576943 2.163316e-04  1.263609e-05

就我的理解而言,后一个输出 (odds) 表示属于我数据中 72 个独特社区中每一个的每个犯罪发生的概率,而前者 (pd) 表示预测 classes 基于我的数据集。这引出了我的具体问题;我如何使用这些预测的 classes 来生成关于下一次犯罪可能发生的位置的某种预测(即类似于提前 1 步的时间序列预测)?

您可以使用要预测的值创建一个 newdata 数据框,然后使用 predict 函数获取每个 class 的预测概率。例如,

# estimate model
library(nnet)
dat <- mtcars
dat$gear <- factor(dat$gear)
mod <- multinom(gear ~ mpg + hp, data = dat)

# what values we want predictions for
out_of_sample_data <- data.frame(mpg = c(19, 20), hp = c(130, 140))

# generate predicted probabilities
predict(mod, newdata = out_of_sample_data, type = "probs")
#>           3         4          5
#> 1 0.6993027 0.2777716 0.02292562
#> 2 0.6217686 0.2750779 0.10315351

显然,您需要使用您认为将来会出现的值来填充样本外数据,这可能很棘手(至少可以说)。