# How to define a formula with a vector of string names for GLM.jl?

**URL:** https://discourse.julialang.org/t/how-to-define-a-formula-with-a-vector-of-string-names-for-glm-jl/44930
**Category:** General Usage
**Created:** [August 14, 2020, 12:44pm UTC](https://discourse.julialang.org/t/how-to-define-a-formula-with-a-vector-of-string-names-for-glm-jl/44930 "2020-08-14T12:44:28Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [August 14, 2020, 12:44pm UTC](https://discourse.julialang.org/t/how-to-define-a-formula-with-a-vector-of-string-names-for-glm-jl/44930/1 "2020-08-14T12:44:28Z")

</div>

I got a `DataFrame` that I wanted to fit a GLM model with but I would like to be able to build a formula (with hundreds of variables) programmatically without having to write it out by hand. So the best I can think of is via `eval`. Is there a better way?

```julia
vars = ["x1", "x2", "x3"]

terms = eval(Meta.parse("@formula(y~"*reduce((x,y)->x*"+"*y, vars)*")"))

glm(terms, data = data)

```

---

<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: [August 14, 2020, 12:46pm UTC](https://discourse.julialang.org/t/how-to-define-a-formula-with-a-vector-of-string-names-for-glm-jl/44930/2 "2020-08-14T12:46:56Z")

</div>

See [Constructing a formula programmatically](https://juliastats.org/StatsModels.jl/stable/formula/#Constructing-a-formula-programmatically-1) in the docs.

Not 100% sure it works with strings, but it definitely works with `Symbols`.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [August 14, 2020, 12:57pm UTC](https://discourse.julialang.org/t/how-to-define-a-formula-with-a-vector-of-string-names-for-glm-jl/44930/3 "2020-08-14T12:57:27Z")

</div>

If you have a string but need a symbol use:

```julia
julia> Symbol("x")
:x

```

---

<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: [August 14, 2020, 1:20pm UTC](https://discourse.julialang.org/t/how-to-define-a-formula-with-a-vector-of-string-names-for-glm-jl/44930/4 "2020-08-14T13:20:11Z")

</div>

See my answer here: [Using all independent variables with @formula in a multiple linear model - #2 by nilshg](https://discourse.julialang.org/t/using-all-independent-variables-with-formula-in-a-multiple-linear-model/43691/2)

Basically what the preceding posts say - you want to construct `Term` objects from symbols, in the other thread I’m doing this for all columns `names(df)` but that of course easily generalizes to a vector holding whatever subset of columns you’re interested in.
