Model.add_regressions#
- Model.add_regressions(
- formulas: str | Mapping[str | tuple[str, str] | tuple[str, str, str], Regression],
- *,
- person_data: pd.DataFrame | None = None,
- statement_data: pd.DataFrame | None = None,
- item_data: pd.DataFrame | None = None,
- pair_data: pd.DataFrame | None = None,
- id_columns: Mapping[str, str | tuple[str, ...]] | None = None,
- options: Mapping[str, RegressionOptions] | None = None,
Add fixed-effect predictors to a prepared model before estimation.
Parameters#
formulas: str or mappingOne
target ~ predictorsline per target, or a mapping from targets toRegressionobjects. In mapping form, each regression supplies its own table, IDs and options. Use a tuple such as("fc", "TIRT", "cutpoints")to select one measurement model in a mixed design.person_data,statement_data,item_data: pandas.DataFrame or NoneFeature tables keyed by
person_id,statement_idoritem_id. Provide one row per used entity.pair_data: pandas.DataFrame or NoneFeatures keyed by
(block_id, statement_1, statement_2), one row per comparison. Reversed pairs align to design order.id_columns: mapping or NoneAlternative key names by entity:
person,statement,itemorpair.options: mapping or NoneRegressionOptionsper target for string formulas. For a model-specific coefficient prior, add a qualified key such asfc.TIRT.loading_magnitudeunderoptions, alongside the ordinaryfc.loading_magnitudeformula.
Returns#
- None
Updates an already prepared, unfitted model. A later declaration for the same target replaces that regression and preserves other targets and fixed references. The feature encoding is retained for scoring.
Notes#
Use available_covariate_targets() to find the regression targets and predictor tables for the current design.
Use theta with a person table, fc.loading_magnitude with a statement
table, fc.cutpoints with a pair table, and item parameters with an item
table. The supported targets
depend on the response model. Missing values in used predictor columns raise
an error. Formulae support main effects and interactions.
A model-qualified option may override only coefficient priors. A qualified
term-specific prior takes precedence over its common slope prior, then
the unqualified option, then the model default. The qualified prior shares
the training feature encoding with the unqualified formula.
Examples#
String formula#
Four people have a continuous predictor and a categorical predictor. After preparing the model, look up the person-level target before writing the formula.
import pandas as pd
import pyfcirt as fc
bundle = fc.load_example("simulated_mirt", model="DichotomousIRT", n_persons=4)
ids = bundle.data.responses.frame["person_id"].drop_duplicates().tolist()
people = pd.DataFrame({"person_id": ids, "study_hours": [1.0, 3.0, 2.0, 6.0]})
people["site"] = ["A", "A", "B", "B"]
model = fc.Model(bundle.data)
print(model.available_covariate_targets(level="person").to_string(index=False))
target model level kind predictor_level
theta None person regression person
Use theta on the left side of the formula and supply the indicated person table.
The fitted predictor axis includes the interaction.
model.add_regressions("theta ~ study_hours * site", person_data=people)
print(model.prepared.coords["theta_predictor"].tolist())
['study_hours', 'site[B]', 'study_hours:site[B]']
Explicit regression declaration#
A Regression record keeps a coefficient prior and the input table with its formula.
import pymc as pm
model = fc.Model(bundle.data)
effect = fc.Regression(
"study_hours",
people,
id="person_id",
priors={"study_hours": pm.Normal.dist(mu=0, sigma=0.3)},
)
model.add_regressions({"theta": effect})
print(model.prepared.coords["theta_predictor"].tolist())
['study_hours']