# Getting model parameter names from DataFrame

**URL:** <https://discourse.julialang.org/t/getting-model-parameter-names-from-dataframe/50008>\
**Category:** General Usage\
**Tags:** turing\
**Created:** [November 11, 2020, 11:03pm UTC](https://discourse.julialang.org/t/getting-model-parameter-names-from-dataframe/50008 "2020-11-11T23:03:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [November 11, 2020, 11:03pm UTC](https://discourse.julialang.org/t/getting-model-parameter-names-from-dataframe/50008/1 "2020-11-11T23:03:16Z")

</div>

Suppose I have

```julia
df = DataFrame(
    age=[20, 30, 40], 
    height=[80, 130, 200], 
)
weight = [100, 120, 200]

```

I want to predict `weight` from the other columns using linear regression. There are two options. Explicitly write each variable into the model, or build a covariate matrix X and write it into the model.

The linear regression tutorial uses a covariate matrix `x`:

```julia
# Bayesian linear regression.
@model function linear_regression(x, y)
    # Set variance prior.
    σ₂ ~ truncated(Normal(0, 100), 0, Inf)
    
    # Set intercept prior.
    intercept ~ Normal(0, sqrt(3))
    
    # Set the priors on our coefficients.
    nfeatures = size(x, 2)
    coefficients ~ MvNormal(nfeatures, sqrt(10))
    
    # Calculate all the mu terms.
    mu = intercept .+ x * coefficients
    y ~ MvNormal(mu, sqrt(σ₂))
end

```

It is easy to just use `Array(df)` as my covariate matrix, but that means all my coefficients have opaque names like ` coefficients[2]` in the output.

```julia
Summary Statistics
        parameters mean std naive_se mcse ess r_hat
  ──────────────── ─────── ────── ──────── ────── ──────── ──────
   coefficients[1] -0.0413 0.5648 0.0126 0.0389 265.1907 1.0010
   coefficients[2] 0.2770 0.6994 0.0156 0.0401 375.2777 1.0067
         intercept 0.0058 0.1179 0.0026 0.0044 580.0222 0.9995
                σ₂ 0.3017 0.1955 0.0044 0.0132 227.2322 1.0005

```

Is it possible to use the `names()` from the dataframe to create the coefficient names? This could happen (1) during model building or (2) after the trace is constructed. I think the idea of doing it during model building is most flexible, so every part of the analysis will automatically include the names.

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [November 12, 2020, 6:43pm UTC](https://discourse.julialang.org/t/getting-model-parameter-names-from-dataframe/50008/2 "2020-11-12T18:43:14Z")

</div>

The [`StatsModels` package](https://github.com/JuliaStats/StatsModels.jl) provides a formula language to convert from a symbolic description of a regression-like model to the model matrix.

---

<div class="post-metadata">

**Author:** ![dave.f.kleinschmidt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dave.f.kleinschmidt/32/55_2.png) [@dave.f.kleinschmidt](https://discourse.julialang.org/u/dave.f.kleinschmidt)\
**Post date:** [November 12, 2020, 7:13pm UTC](https://discourse.julialang.org/t/getting-model-parameter-names-from-dataframe/50008/3 "2020-11-12T19:13:19Z")

</div>

…and @cpfiffer at some point had mocked up a brms-style integration with Turing, I think it was here: [https://github.com/cpfiffer/BayesModels](https://github.com/cpfiffer/BayesModels)

If you want to use StatsModels directly, then you could do something like

```julia
using StatsModels

f = @formula(weight ~ 1 + age + height)
f_concrete = apply_schema(f, schema(df))

y, x = modelcols(f, df)
# ... turing magic

respname, prednames = coefnames(f)

```

Note that this way, the intercept would be included in `x` so you’d have to modify your model, OR force no intercept by using `@formula(weight ~ 0 + age + height)`.

---

<div class="post-metadata">

**Author:** ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)\
**Post date:** [November 13, 2020, 5:41pm UTC](https://discourse.julialang.org/t/getting-model-parameter-names-from-dataframe/50008/4 "2020-11-13T17:41:53Z")

</div>

One of these days I may have to turn that from a mockup into a real package.

---

<div class="post-metadata">

**Author:** ![dave.f.kleinschmidt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dave.f.kleinschmidt/32/55_2.png) [@dave.f.kleinschmidt](https://discourse.julialang.org/u/dave.f.kleinschmidt)\
**Post date:** [November 18, 2020, 9:44pm UTC](https://discourse.julialang.org/t/getting-model-parameter-names-from-dataframe/50008/5 "2020-11-18T21:44:54Z")

</div>

it’d make a great GSOC project actually…pretty clear scope and just need someone to do it…

---

<div class="post-metadata">

**Author:** ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)\
**Post date:** [November 19, 2020, 3:15am UTC](https://discourse.julialang.org/t/getting-model-parameter-names-from-dataframe/50008/6 "2020-11-19T03:15:30Z")

</div>

Oh, very true! I’ll keep that in mind when we’re adding projects for Turing.
