# Extract @formula / model formula from GLM object

**URL:** <https://discourse.julialang.org/t/extract-formula-model-formula-from-glm-object/66610>\
**Category:** General Usage\
**Tags:** glm\
**Created:** [August 18, 2021, 4:09pm UTC](https://discourse.julialang.org/t/extract-formula-model-formula-from-glm-object/66610 "2021-08-18T16:09:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![guysutton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guysutton/32/22386_2.png) [@guysutton](https://discourse.julialang.org/u/guysutton)\
**Post date:** [August 18, 2021, 4:09pm UTC](https://discourse.julialang.org/t/extract-formula-model-formula-from-glm-object/66610/1 "2021-08-18T16:09:32Z")

</div>

I am trying to extract the model formula from a fitted GLM object, with little success. In the dummy example below, I would like to get the following back, for example: `Y ~ X` [i.e. the information stored inside @formula( … )]

```julia
using DataFrames, GLM, Distributions

# Simulate some fake data 
data = DataFrame(X=[1,2,3,4,5,6,7,8,9,10], Y=[2,4,7,11,10,6,4,5,7,8])

# Fit GLM 
m1 = fit(GeneralizedLinearModel,
         @formula(Y ~ X),
         data,
         Normal(),
         GLM.IdentityLink())

```

I would like to store the formula in another variable (e.g. `mod_formula = Y ~ X`), and then be able to pass this to another model (e.g. lm1 = fit(LinearModel, @formula(mod\_formula)), programatically.

---

<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 18, 2021, 4:15pm UTC](https://discourse.julialang.org/t/extract-formula-model-formula-from-glm-object/66610/2 "2021-08-18T16:15:44Z")

</div>

You can do `formula(m1)`

Note that you can just store the formula as an object `f = @formula(y ~ x)`

You can also construct a formula programmatically. See [here](https://juliastats.org/StatsModels.jl/latest/formula/#Constructing-a-formula-programmatically).

---

<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:** [October 8, 2021, 3:28pm UTC](https://discourse.julialang.org/t/extract-formula-model-formula-from-glm-object/66610/3 "2021-10-08T15:28:56Z")

</div>

To expand a bit on @pdeffebach answer, the best way to approach this is going do depend on what you want to do with that formula. If you want to fit another model to the same data (or data with the same structure, e.g. all teh columns have the same types/unique values for categorical columns) then using `f1 = formula(m1)` is great. However, if you have different types of data or different levels of categorical values, then you’ll want to first generate the formula and the hold onto the “non-concrete” form, like

```julia
f = @formula(Y ~ X)
m1 = fit(..., f, data, ...)
m2 = fit(..., f, data2, ...)

```

…or construct programmatically, like

```julia
f = term(:Y) ~ term(:X)
...

```
