# Is it possible to apply one schema to different DataFrames?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-apply-one-schema-to-different-dataframes/73345>\
**Category:** General Usage\
**Tags:** dataframes, statsmodels\
**Created:** [December 19, 2021, 7:37pm UTC](https://discourse.julialang.org/t/is-it-possible-to-apply-one-schema-to-different-dataframes/73345 "2021-12-19T19:37:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PharmCat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pharmcat/32/6953_2.png) [@PharmCat](https://discourse.julialang.org/u/PharmCat)\
**Post date:** [December 19, 2021, 7:37pm UTC](https://discourse.julialang.org/t/is-it-possible-to-apply-one-schema-to-different-dataframes/73345/1 "2021-12-19T19:37:20Z")

</div>

Hi!

I have an external ‘ModelFrame’ that was created like this:

```julia
sch = schema(model, data, contrasts)
   f = apply_schema(model, sch, StatisticalModel)
 mf = ModelFrame(f, sch, data, StatisticalModel)

```

I know nothing about model, data and contrasts. I try to apply the same schema to other dataframe:

```julia
mf = ModelFrame(mf.f, mf.schema, data2, StatisticalModel)
mm = ModelMatrix(mf)

```

Expectedly an error occurred:

```julia
ERROR: KeyError: key "5" not found ...

```

Is any way to do this some way? (main problem - no information about contrasts)

---

<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:** [December 19, 2021, 7:38pm UTC](https://discourse.julialang.org/t/is-it-possible-to-apply-one-schema-to-different-dataframes/73345/2 "2021-12-19T19:38:55Z")

</div>

Can you make an MWE for this? I can’t follow what’s going on and what the desired behavior ir.

---

<div class="post-metadata">

**Author:** ![PharmCat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pharmcat/32/6953_2.png) [@PharmCat](https://discourse.julialang.org/u/PharmCat)\
**Post date:** [December 19, 2021, 7:59pm UTC](https://discourse.julialang.org/t/is-it-possible-to-apply-one-schema-to-different-dataframes/73345/3 "2021-12-19T19:59:45Z")

</div>

Sorry, I was mistaken. Seems work fine:

```julia
using DataFrames, StatsModels, Tables
import StatsModels: missing_omit, termvars, StatisticalModel
df1 = DataFrame(a = [1,2,3,4,5,6], b = [3,4,5,6,7,7], c = ["1","1","1","2","2", "1"])
df0 = DataFrame(a = [1,2,3,4,5], c = ["1","1","1","2","2"])
model = @formula(a~c)
tv = termvars(model)
#hidden part
contrasts=Dict{Symbol,Any}()
data, data_ = StatsModels.missing_omit(NamedTuple{tuple(tv...)}(Tables.columntable(df0)))
sch = schema(model, data, contrasts)
f = apply_schema(model, sch, StatisticalModel)
mf = ModelFrame(f, sch, data, StatisticalModel)
# get only mf

#I try this:
data, data_ = StatsModels.missing_omit(NamedTuple{tuple(tv...)}(Tables.columntable(df1)))
mf2 = ModelFrame(mf.f, mf.schema, data, StatisticalModel)
mm = ModelMatrix(mf2) #this work

```

In this example work, but in the project - not… try to find the problem…
