# Problem using GLM

**URL:** <https://discourse.julialang.org/t/problem-using-glm/42576>\
**Category:** New to Julia\
**Tags:** glm\
**Created:** [July 5, 2020, 10:12pm UTC](https://discourse.julialang.org/t/problem-using-glm/42576 "2020-07-05T22:12:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [July 5, 2020, 10:12pm UTC](https://discourse.julialang.org/t/problem-using-glm/42576/1 "2020-07-05T22:12:10Z")

</div>

The following is throwing an error:

```julia
using DataFrames, GLM
data = DataFrame(X=1:1:length(event[1]), Y=event[1])
ols = lm(@formula(Y ~ X), data)

```

The reason appears to be to do with the type of data I define as X and Y:

```
typeof(X) StepRange{Int64,Int64}

typeof(Y) Vector{Any}

```

What am I missing?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 5, 2020, 10:20pm UTC](https://discourse.julialang.org/t/problem-using-glm/42576/2 "2020-07-05T22:20:20Z")

</div>

first you don’t need the middle `:1`, and if the issue is about GLM doesn’t like `StepRange` then you can do `X=collect(1:length(event[1]))` instead in the second line to materialize an array from this iterator

---

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [July 5, 2020, 10:28pm UTC](https://discourse.julialang.org/t/problem-using-glm/42576/3 "2020-07-05T22:28:34Z")

</div>

> [@jling](#):
>
> X=collect(1:length(event[1]))

The problem seems to be deeper:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/f/0ff14b9d16af0b03ab17f4cea84b4e86ecd1107e.png)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 5, 2020, 11:09pm UTC](https://discourse.julialang.org/t/problem-using-glm/42576/4 "2020-07-05T23:09:41Z")

</div>

why does your array have a shape of `2`? They need to be vectors ( `Array{Float64,1}`)

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [July 5, 2020, 11:12pm UTC](https://discourse.julialang.org/t/problem-using-glm/42576/5 "2020-07-05T23:12:28Z")

</div>

Yea, what is `typeof(event)`? Your LHS variable needs to be a vector not a matrix. Somehow `event[1]` is producing a matrix for you. If it truly is a vector that is being misappropriated as a matrix then you can just wrap it in `vec` … `vec(event[1])`

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 6, 2020, 6:18am UTC](https://discourse.julialang.org/t/problem-using-glm/42576/6 "2020-07-06T06:18:22Z")

</div>

Just to add that you don’t have to `collect` the range when constructing the DataFrame, as this happens automatically:

```julia
julia> using GLM, DataFrames

julia> df = DataFrame(x = 1:10, y = 0.5 .* (1:10) .+ rand.())
10×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ Float64 │
├─────┼───────┼──────────┤
│ 1 │ 1 │ 0.874401 │
│ 2 │ 2 │ 1.07089 │
│ 3 │ 3 │ 2.08023 │
│ 4 │ 4 │ 2.89661 │
│ 5 │ 5 │ 3.22647 │
│ 6 │ 6 │ 3.0337 │
│ 7 │ 7 │ 3.81535 │
│ 8 │ 8 │ 4.19433 │
│ 9 │ 9 │ 4.69071 │
│ 10 │ 10 │ 5.7383 │

julia> df.x == collect(1:10)
true

julia> lm(@formula(y ~ x), df)
StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}

y ~ 1 + x

Coefficients:
────────────────────────────────────────────────────────────────────────────
             Estimate Std. Error t value Pr(>|t|) Lower 95% Upper 95%
────────────────────────────────────────────────────────────────────────────
(Intercept) 0.420504 0.220686 1.90544 0.0932 -0.0883982 0.929406
x 0.498472 0.0355667 14.0151 <1e-6 0.416455 0.580489
────────────────────────────────────────────────────────────────────────────

```

---

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [July 6, 2020, 9:59am UTC](https://discourse.julialang.org/t/problem-using-glm/42576/7 "2020-07-06T09:59:38Z")

</div>

My problem was solved by converting event[1] to a float:  
convert(Array{Float64,1}, event[1])
