# How to create a linear regression model for each of the group

**URL:** <https://discourse.julialang.org/t/how-to-create-a-linear-regression-model-for-each-of-the-group/63592>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 26, 2021, 7:08am UTC](https://discourse.julialang.org/t/how-to-create-a-linear-regression-model-for-each-of-the-group/63592 "2021-06-26T07:08:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tiger\_Zheng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tiger_zheng/32/26511_2.png) [@Tiger\_Zheng](https://discourse.julialang.org/u/Tiger_Zheng)\
**Post date:** [June 26, 2021, 7:08am UTC](https://discourse.julialang.org/t/how-to-create-a-linear-regression-model-for-each-of-the-group/63592/1 "2021-06-26T07:08:29Z")

</div>

![Screen Shot 2021-06-26 at 3.00.23 AM](https://global.discourse-cdn.com/julialang/original/3X/e/6/e62d014bc9181d23d57bfba1456ae1c175f516af.png)

I want to do this code in Julia, is there any way to achieve it? Thank you for your help!

---

<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:** [June 26, 2021, 10:31am UTC](https://discourse.julialang.org/t/how-to-create-a-linear-regression-model-for-each-of-the-group/63592/2 "2021-06-26T10:31:51Z")

</div>

If I understand your screenshot correctly it’s something like this (slightly hacky way of accessing the group name, there’s probably a better way):

```julia
julia> using DataFrames, GLM

julia> df = DataFrame(G = rand('A':'D', 50), x1 = randn(50), y = randn(50));

julia> [(first(first(gdf).G), coef(lm(@formula(y ~ x1), gdf))) for gdf ∈ groupby(df, :G)]
4-element Vector{Tuple{Char, Vector{Float64}}}:
 ('D', [0.2590468147076215, 0.18017049456177228])
 ('B', [0.06914829093952116, 0.8148962459039837])
 ('C', [-0.20806955860375123, 0.15624480441068453])
 ('A', [-0.19031169433664966, 0.0535534286170727])

```

although note that this is the same as just interacting `x1` and the group indicator (allowing a different slope and intercept per group):

```julia
ulia> lm(@formula(y ~ x1*G), df)
StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Vector{Float64}}, GLM.DensePredChol{Float64, LinearAlgebra.CholeskyPivoted{Float64, Matrix{Float64}}}}, Matrix{Float64}}

y ~ 1 + x1 + G + x1 & G

Coefficients:
──────────────────────────────────────────────────────────────────────────
                  Coef. Std. Error t Pr(>|t|) Lower 95% Upper 95%
──────────────────────────────────────────────────────────────────────────
(Intercept) -0.190312 0.328665 -0.58 0.5657 -0.853585 0.472961
x1 0.0535534 0.284165 0.19 0.8514 -0.519915 0.627022
G: B 0.25946 0.424046 0.61 0.5439 -0.5963 1.11522
G: C -0.0177579 0.456611 -0.04 0.9692 -0.939235 0.90372
G: D 0.449359 0.429051 1.05 0.3009 -0.416501 1.31522
x1 & G: B 0.761343 0.48832 1.56 0.1265 -0.224127 1.74681
x1 & G: C 0.102691 0.401754 0.26 0.7995 -0.708082 0.913464
x1 & G: D 0.126617 0.362336 0.35 0.7285 -0.604606 0.85784
──────────────────────────────────────────────────────────────────────────

```

(here `A` is the base category, so intercept and `x1` coefficient are the same as for `A` above, and e.g. `B`’s intercept is given by -0.19+0.259 = 0.069 and slope by 0.05 + 0.76 = 0.81)

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [June 26, 2021, 1:04pm UTC](https://discourse.julialang.org/t/how-to-create-a-linear-regression-model-for-each-of-the-group/63592/3 "2021-06-26T13:04:33Z")

</div>

For the future, don’t screenshot text, paste it. Easier to read, copy-and-run, search and quote. If that seems like extra work for you, consider that you are expecting people to help _you_, and putting in the extra effort increases the chance someone will.

---

<div class="post-metadata">

**Author:** ![Tiger\_Zheng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tiger_zheng/32/26511_2.png) [@Tiger\_Zheng](https://discourse.julialang.org/u/Tiger_Zheng)\
**Post date:** [June 26, 2021, 3:32pm UTC](https://discourse.julialang.org/t/how-to-create-a-linear-regression-model-for-each-of-the-group/63592/4 "2021-06-26T15:32:52Z")

</div>

Thank you sooooo much!!! 😃  
really helps me a lot  
have a great day!

---

<div class="post-metadata">

**Author:** ![Tiger\_Zheng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tiger_zheng/32/26511_2.png) [@Tiger\_Zheng](https://discourse.julialang.org/u/Tiger_Zheng)\
**Post date:** [June 26, 2021, 3:33pm UTC](https://discourse.julialang.org/t/how-to-create-a-linear-regression-model-for-each-of-the-group/63592/5 "2021-06-26T15:33:52Z")

</div>

got it, thanks man 😀
