Is it possible to apply one schema to different DataFrames?

Hi!

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

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:

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

Expectedly an error occurred:

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

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

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

Sorry, I was mistaken. Seems work fine:

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…