# Formula creation without direct name specification

**URL:** <https://discourse.julialang.org/t/formula-creation-without-direct-name-specification/1448>\
**Category:** Statistics\
**Tags:** question\
**Created:** [January 12, 2017, 7:46pm UTC](https://discourse.julialang.org/t/formula-creation-without-direct-name-specification/1448 "2017-01-12T19:46:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![evoclue](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@evoclue](https://discourse.julialang.org/u/evoclue)\
**Post date:** [January 12, 2017, 7:46pm UTC](https://discourse.julialang.org/t/formula-creation-without-direct-name-specification/1448/1 "2017-01-12T19:46:55Z")

</div>

Say I have a DataFrame called mydata where names(mydata) is Symbol[5001] consisting of :y, :x1, :x2, :x3, :x4, … (the other 4995 symbols) … , :x5000.

I want to fit models with the formula ModelFrame(y ~ x1, mydata), ModelFrame(y ~ x2, mydata), etc. until ModelFrame(y ~ x5000, mydata). ModelFrame can be switched to glm or lmm as I believe they use the same formula.jl code.

How do I create a model such that I can do something like ModelFrame(y ~ names(mydata), mydata) where x is any number? This would allow looping over variables. However, I’m not really sure what type of variable is being given after the tilde because ModelFrame(y ~ names(mydata)[2], mydata), ModelFrame(y ~ Symbol(:x1), mydata), or ModelFrame(y ~“x1”, mydata) don’t work.

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [January 12, 2017, 7:51pm UTC](https://discourse.julialang.org/t/formula-creation-without-direct-name-specification/1448/2 "2017-01-12T19:51:29Z")

</div>

You can use interpolation. Here some minimal examples using Symbols or Strings

```julia
julia> names = [:a,:b,:c]
3-element Array{Symbol,1}:
 :a
 :b
 :c

julia> y ~ $(names[2])
Formula: y ~ b

julia> names_str = ["a","b","c"]
3-element Array{String,1}:
 "a"
 "b"
 "c"

julia> y ~ $(Symbol(names_str[2]))
Formula: y ~ b

```

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [January 12, 2017, 8:06pm UTC](https://discourse.julialang.org/t/formula-creation-without-direct-name-specification/1448/3 "2017-01-12T20:06:21Z")

</div>

A bit of trivia: When you open up Julia and don’t import any package you can see that the syntax still works but throws an error

```julia
julia> y ~ x
ERROR: UndefVarError: @~ not defined

```

So what is happening is that `y ~ x + z` is just a syntax sugar for the macro `@~ y x + z`

```julia
julia> :(y ~ x)
:(@~ y x)

julia> :(y ~ x + y)
:(@~ y x + y)

```

This is the reason why things were unintuitive for you, because the code you typed was not evaluated, but instead just passed as an expression to the macro implementation.
