如何获得防风草多项式Lo​​gistic回归模型的系数?

时间:2020-05-11 07:02:25

标签: r statistics classification data-analysis tidymodels

我拟合了多项逻辑回归模型,以使用tidymodels框架预测虹膜数据集中的物种。

//h3[contains(translate(text(),' ',' ' ), 'App-1 Playground Login')]

然后,我想查看模型的系数并写出公式。我的理解是,我应该从iris.fit获得此信息。

iris.fit的输出有一个100行表,其中包含Df,%Dev和Lambda。虹膜数据集只有4个预测变量。如何将此输出转换为系数?

2 个答案:

答案 0 :(得分:2)

您可以使用tidy()函数获取数据框中的所有系数(针对每个测试的lambda)。

library(tidymodels)
#> ── Attaching packages ────────────────────────────────────────── tidymodels 0.1.0 ──
#> ✓ broom     0.5.6      ✓ recipes   0.1.12
#> ✓ dials     0.0.6      ✓ rsample   0.0.6 
#> ✓ dplyr     0.8.5      ✓ tibble    3.0.1 
#> ✓ ggplot2   3.3.0      ✓ tune      0.1.0 
#> ✓ infer     0.5.1      ✓ workflows 0.1.1 
#> ✓ parsnip   0.1.1      ✓ yardstick 0.0.6 
#> ✓ purrr     0.3.4
#> ── Conflicts ───────────────────────────────────────────── tidymodels_conflicts() ──
#> x purrr::discard()  masks scales::discard()
#> x dplyr::filter()   masks stats::filter()
#> x dplyr::lag()      masks stats::lag()
#> x ggplot2::margin() masks dials::margin()
#> x recipes::step()   masks stats::step()

iris_lr <- multinom_reg(
  mode = "classification",
  penalty = NULL,
  mixture = NULL
) %>%
  set_engine("glmnet")

iris_fit = iris_lr %>%
  fit(Species ~ . , data = iris)

library(broom)

tidy(iris_fit)
#> # A tibble: 839 x 6
#>    class      term            step  estimate lambda dev.ratio
#>    <chr>      <chr>          <dbl>     <dbl>  <dbl>     <dbl>
#>  1 setosa     ""                 1  6.41e-16  0.435 -1.21e-15
#>  2 versicolor ""                 1 -1.62e-15  0.435 -1.21e-15
#>  3 virginica  ""                 1  9.81e-16  0.435 -1.21e-15
#>  4 setosa     ""                 2  2.44e- 1  0.396  6.56e- 2
#>  5 setosa     "Petal.Length"     2 -9.85e- 2  0.396  6.56e- 2
#>  6 versicolor ""                 2 -1.22e- 1  0.396  6.56e- 2
#>  7 virginica  ""                 2 -1.22e- 1  0.396  6.56e- 2
#>  8 setosa     ""                 3  4.62e- 1  0.361  1.20e- 1
#>  9 setosa     "Petal.Length"     3 -1.89e- 1  0.361  1.20e- 1
#> 10 versicolor ""                 3 -2.31e- 1  0.361  1.20e- 1
#> # … with 829 more rows

reprex package(v0.3.0)于2020-05-14创建

答案 1 :(得分:0)

系数在:

    names(iris.fit$fit$beta)
[1] "setosa"     "versicolor" "virginica"

iris.fit$fit$beta$setosa
4 x 100 sparse Matrix of class "dgCMatrix"
   [[ suppressing 100 column names ‘s0’, ‘s1’, ‘s2’ ... ]]

Sepal.Length .  .           .          .          .          .        
Sepal.Width  .  .           .          .          .          .        
Petal.Length . -0.09849722 -0.1890163 -0.2737859 -0.3542876 -0.4108527
Petal.Width  .  .           .          .          .          .  

每个测试的(iris.fit$fit$lambda)的一列。您可以在此tutorial中查看glmnet,这里没有专门介绍多项式的部分,但其中有很多输出说明。