# Simple linear regression question

**URL:** https://discourse.julialang.org/t/simple-linear-regression-question/22749
**Category:** Statistics
**Created:** [April 4, 2019, 1:26pm UTC](https://discourse.julialang.org/t/simple-linear-regression-question/22749 "2019-04-04T13:26:15Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![corwin](https://avatars.discourse-cdn.com/v4/letter/c/dc4da7/32.png) [@corwin](https://discourse.julialang.org/u/corwin)
#### Post date: [April 4, 2019, 1:26pm UTC](https://discourse.julialang.org/t/simple-linear-regression-question/22749/1 "2019-04-04T13:26:15Z")

</div>

Hello!  
I apologize for possibly a simple question. I’m new to Julia, and maybe I didn’t find something in the description of StatsModels and TimeSeries.

I have DataFrame data2 with time (yyyy-mm-dd) and 2 column with data Y1 and Y2

```julia
julia> data2
26×3 DataFrame
│ Row │ Time │ Y1 │ Y2 │
│ │ Date⍰ │ Float64 │ Float64 │
├─────┼────────────┼─────────┼─────────┤
│ 1 │ 2016-12-01 │ 72.0 │ 19.0 │
│ 2 │ 2017-01-01 │ 21.0 │ 63.0 │
...

```

And I want to make the approximation function:

```julia
Y1 = a*Time + b and Y2 = a*Time + b

```

How can i do this? Who can tell?  
Thank you for the answers

---

<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: [April 4, 2019, 1:37pm UTC](https://discourse.julialang.org/t/simple-linear-regression-question/22749/2 "2019-04-04T13:37:34Z")

</div>

Something like this?

```julia
using DataFrames, GLM

y₁ = [rand()*0.5i for i ∈ 1:1_000]
y₂ = [rand()*1.5i for i ∈ 1:1_000]

df = DataFrame(t = 1:1_000, y₁ = y₁, y₂ = y₂)

lm(@formula(y₁ ~ t), df)
lm(@formula(y₂ ~ t), df)

```

---

<div class="post-metadata">

### Author: ![corwin](https://avatars.discourse-cdn.com/v4/letter/c/dc4da7/32.png) [@corwin](https://discourse.julialang.org/u/corwin)
#### Post date: [April 4, 2019, 2:15pm UTC](https://discourse.julialang.org/t/simple-linear-regression-question/22749/3 "2019-04-04T14:15:43Z")

</div>

Hello @nilshg  
Thank you for your response and example.

I try

```julia
julia> lm(@formula(Y1 ~ Time), data2)

```

and error

```julia
Error showing value of type StatsModels.DataFrameRegressionModel{LinearModel{LmResp{Array{Float64,1}},DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}:
ERROR: ArgumentError: FDist: the condition ν1 > zero(ν1) && ν2 > zero(ν2) is not satisfied.

```

---

<div class="post-metadata">

### Author: ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)
#### Post date: [April 5, 2019, 8:56pm UTC](https://discourse.julialang.org/t/simple-linear-regression-question/22749/4 "2019-04-05T20:56:31Z")

</div>

I think you just need to convert your `Date` column to a numeric variable, e.g. using `df.Time2 = datetime2rata.(df.Time)`. By default non-numeric variables are treated as categorical.
