# JuliaDB & OnlineStats syntax for linear regression

**URL:** https://discourse.julialang.org/t/juliadb-onlinestats-syntax-for-linear-regression/11643
**Category:** General Usage
**Tags:** package
**Created:** [June 13, 2018, 1:02pm UTC](https://discourse.julialang.org/t/juliadb-onlinestats-syntax-for-linear-regression/11643 "2018-06-13T13:02:38Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![MaximilianJHuber](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maximilianjhuber/32/2579_2.png) [@MaximilianJHuber](https://discourse.julialang.org/u/MaximilianJHuber)
#### Post date: [June 13, 2018, 1:02pm UTC](https://discourse.julialang.org/t/juliadb-onlinestats-syntax-for-linear-regression/11643/1 "2018-06-13T13:02:38Z")

</div>

I cannot seem to figure out how to run a `LinReg` Onlinestat on my JuliaDB table. Especially, how to tell the `reduce` function what the left-hand-side and right-hand-side variables are. The following runs a regression of `y` on `x` and `z` is ignored:

```julia
t = table(@NT(x = randn(1000), y = randn(1000), z = rand(1000)))
reduce(LinReg(), t, select = (:x, :y, :z))

```

---

<div class="post-metadata">

### Author: ![MaximilianJHuber](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maximilianjhuber/32/2579_2.png) [@MaximilianJHuber](https://discourse.julialang.org/u/MaximilianJHuber)
#### Post date: [June 14, 2018, 12:47pm UTC](https://discourse.julialang.org/t/juliadb-onlinestats-syntax-for-linear-regression/11643/2 "2018-06-14T12:47:15Z")

</div>

Found it [here](https://youtu.be/d5SzUh2_ono?t=1h21m23s).

---

<div class="post-metadata">

### Author: ![joshday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshday/32/368_2.png) [@joshday](https://discourse.julialang.org/u/joshday)
#### Post date: [June 15, 2018, 12:04am UTC](https://discourse.julialang.org/t/juliadb-onlinestats-syntax-for-linear-regression/11643/3 "2018-06-15T00:04:41Z")

</div>

You can also use [`LinRegBuilder`](http://joshday.github.io/OnlineStats.jl/latest/api.html#OnlineStats.LinRegBuilder), which lets you fit any regression model on the data after a single pass.

```julia
o = reduce(LinRegBuilder(), t, select = (:x, :y, :z))

# y ~ x + z
coef(o, x=[1,3], y=2, bias=false)

```

---

<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: [June 15, 2018, 1:01am UTC](https://discourse.julialang.org/t/juliadb-onlinestats-syntax-for-linear-regression/11643/4 "2018-06-15T01:01:40Z")

</div>

LinRegBuilder seem cool. Can it be used to create any arbitrary model compatible with `StatsModels`?

---

<div class="post-metadata">

### Author: ![MaximilianJHuber](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maximilianjhuber/32/2579_2.png) [@MaximilianJHuber](https://discourse.julialang.org/u/MaximilianJHuber)
#### Post date: [June 15, 2018, 3:15am UTC](https://discourse.julialang.org/t/juliadb-onlinestats-syntax-for-linear-regression/11643/5 "2018-06-15T03:15:45Z")

</div>

Can it do categorical variables/dummy variables?

---

<div class="post-metadata">

### Author: ![joshday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshday/32/368_2.png) [@joshday](https://discourse.julialang.org/u/joshday)
#### Post date: [June 15, 2018, 11:51am UTC](https://discourse.julialang.org/t/juliadb-onlinestats-syntax-for-linear-regression/11643/6 "2018-06-15T11:51:35Z")

</div>

In order to fit any given term (dummy variable, interaction term, etc.), you would need to specifically `select` it. There’s no support (yet) for formulas.

Here’s an example of making an interaction term between `:x` and `:z`, which I’ll admit isn’t the cleanest syntax.

```julia
reduce(LinRegBuilder(), t, select=(:x, :y, :z, (:x, :z) => xz -> *(xz...)))

```
