# Help with GLM logistic regression

**URL:** <https://discourse.julialang.org/t/help-with-glm-logistic-regression/56899>\
**Category:** General Usage\
**Tags:** glm\
**Created:** [March 10, 2021, 4:21pm UTC](https://discourse.julialang.org/t/help-with-glm-logistic-regression/56899 "2021-03-10T16:21:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [March 10, 2021, 4:21pm UTC](https://discourse.julialang.org/t/help-with-glm-logistic-regression/56899/1 "2021-03-10T16:21:33Z")

</div>

Can someone help me with using GLM for logistic regression.

I’m testing this out on the iris dataset.

```julia
using RDatasets
 iris = dataset("datasets","iris")

```

I’ve worked with this before and know that the PetalLength is a perfect predictor/classifier for the Setosa class

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/5/55490a621f5fc0fe506188d3392a6d89765edba7.png)

So I created a one hot encoded column called IsSetosa and then used logistic regression to model the input factors on the IsSetosa class

```julia
iris[:,:IsSetosa] = iris.Species.=="setosa"
logit = glm( @formula(IsSetosa ~ SepalLength+SepalWidth+PetalLength+PetalWidth), iris, Binomial(), ProbitLink())

Coefficients:
────────────────────────────────────────────────────────────────────────
                Coef. Std. Error z Pr(>|z|) Lower 95% Upper 95%
────────────────────────────────────────────────────────────────────────
(Intercept) -3.88972 7989.87 -0.00 0.9996 -15663.7 15656.0
SepalLength 2.88224 2279.99 0.00 0.9990 -4465.82 4471.59
SepalWidth 1.81596 1038.46 0.00 0.9986 -2033.52 2037.15
PetalLength -4.91615 1903.18 -0.00 0.9979 -3735.08 3725.25
PetalWidth -5.33176 2689.12 -0.00 0.9984 -5275.92 5265.25
────────────────────────────────────────────────────────────────────────

```

Two questions

1. how can I get access each value from this response per independent variable. For example, how can I get the pvalue for the PetalLength ?
2. From JMP, there is also a result from a “Whole Model Test” which is the overall result of the system on the output. Is there such an equivalent in the Julia GLM package (or other packages?)  
[JMP Help](https://www.jmp.com/support/help/en/15.2/index.shtml#page/jmp/whole-model-test-3.shtml)

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 10, 2021, 5:17pm UTC](https://discourse.julialang.org/t/help-with-glm-logistic-regression/56899/2 "2021-03-10T17:17:10Z")

</div>

With regards to your first point, this is a bit of a limitation at the moment.

Here is a workaround to have everything in a more easy-to-access structure

```julia
julia> using StatsBase 

julia> function make_named_array(c::CoefTable)
           n_mat = reduce(hcat, c.cols) |> NamedArray
           setnames!(n_mat, c.rownms, 1)
           setnames!(n_mat, c.colnms, 2)
           return n_mat
       end;

julia> make_named_array(c)
2×6 Named Array{Float64,2}
      A ╲ B │ Coef. Std. Error z Pr(>|z|) Lower 95% Upper 95%
────────────┼───────────────────────────────────────────────────────────────────────
(Intercept) │ 0.223037 0.127292 1.75216 0.0797464 -0.0264519 0.472525
x │ -0.189126 0.133999 -1.4114 0.158127 -0.451759 0.0735072

```

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [March 10, 2021, 5:35pm UTC](https://discourse.julialang.org/t/help-with-glm-logistic-regression/56899/3 "2021-03-10T17:35:42Z")

</div>

> [@pdeffebach](#):
>
> ```julia
> function make_named_array(c::CoefTable)
> n_mat = reduce(hcat, c.cols) |> NamedArray
> setnames!(n_mat, c.rownms, 1)
> setnames!(n_mat, c.colnms, 2)
> return n_mat
> end;
> 
> ```

Thanks for the help. I tried to use this function and passed the coef table from the output of logistic regression, but get this error

```julia
julia> make_named_array(coef(logit))
ERROR: MethodError: no method matching make_named_array(::Vector{Float64})

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 10, 2021, 6:51pm UTC](https://discourse.julialang.org/t/help-with-glm-logistic-regression/56899/4 "2021-03-10T18:51:28Z")

</div>

Ah `coef` returns a vector of the coefficients. You want `make_named_array(coeftable(logit))`.

Also make sure you have

```julia
using NamedArrays

```

at the top as well.

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [March 10, 2021, 7:01pm UTC](https://discourse.julialang.org/t/help-with-glm-logistic-regression/56899/5 "2021-03-10T19:01:21Z")

</div>

Thanks, that works.
