Skip to main content

Orthogonalize interactions by centering residuals

Project description

resmod: a package for creating orthogonalized interaction terms by centering residuals

What is resmod?

resmod is a Python package that provides the ability to quickly create orthogonalized interaction terms by centering residuals. This approach to testing interaction prevents the user from violating basic assumptions of regression -- specificaly that there should be no correlated residuals. Because the interaction term is orthogonalized from the model, you are able to interpret both direct effects and interaction terms in the same model. Not only is this convienient but it reduces the number of test run on your data.

This approach is based on the work of Todd Little. See the citation: Little, T. D., Card, N. A., Bovaird, J. A., Preacher, K. J., & Crandall, C. S. (2007). Structural equation modeling of mediation and moderation with contextual factors. Modeling contextual effects in longitudinal studies, 1, 207-230.

Functions

  • residual_center
    • Two-way orthogonalized interaction that can be use in any regression-based model
  • three_center
    • Three-way orthogonalized interaction that can be used in any regression-based model
  • orthogonalize
    • Multiple orthogonalized interactions to be used in latent SEM interaction modeling

Installation

# git
git clone https://github.com/drewwint/resmod.git
cd resmod
pip install . 

# PyPi
pip install resmod

Dependencies

Usage

# residual_center: Orthogonalizing single interaction between income and education from ducan data
  ## Packages
    from resmod.single import residual_center         # for orthogonalizing using centered residuals
    import statsmodels.formula.api as smf             # for estimation 
    import statsmodels as sms             
    from statsmodels import datasets                  # for importing data
    import numpy as np                                # for data structring
    import pandas as pd                               # for dataframe 

  ## Getting data
    duncan_prestige = sms.datasets.get_rdataset("Duncan", "carData")
    income = duncan_prestige.data.income
    education = duncan_prestige.data.education

  ## Creating dataframe
    v1 = np.array(income)                             # ensure v1 is an array
    v2 = np.array(education)                          # ensure v2 is an array 
    dat = pd.DataFrame({"income": v1, "education" : v2})

  ## Estimation 
    residual_center(dat.income, dat.education)

  ## Returns
    #array([ 63.11264837,    229.8491846,    741.28285426,  -191.61545996,  143.13497759, 
    #       -1522.02012271,  250.49755451,   1222.03876523,  281.50598242,  463.22429449,  
    #       -657.16077574,   951.3190848 ,   923.98157381,  -761.79683046, -500.35610126,  
    #       -798.28161848,  -474.82578368,  -357.03501052,  -457.2861054 ,  585.94123821,
    #       -981.98093767,  -476.50649685,  -312.02816875,  -549.40617942,  165.39170698,  
    #       -458.91783728,  -1052.25086135, -293.40322494,   169.06536061, -372.67648496,   
    #        101.34978524,   1153.8352266,  -337.3613032,    599.90768769,  386.69161908,   
    #        248.37917402,   182.34841689,   117.02343887,   679.23266571,  360.97604371,
    #        115.6538024,    194.02207051,   612.22286945,  -485.36288933,  98.28416593]
    #        )
# orthogonalize: Orthogonalizing two list of variables from Duncan data
   ### Output could be used for multiple orthogonalized interactions or
   ### to create interactions of all observed variables to be used in a latent interaction

 ## Packages
      from resmod.sem import orthogonalize
      import statsmodels.formula.api as smf
      import statsmodels as sms
      from statsmodels import datasets
      import numpy as np
      import pandas as pd

 ## Getting data
      duncan_prestige = sms.datasets.get_rdataset("Duncan", "carData")
      income = duncan_prestige.data.income
      education = duncan_prestige.data.education

 ## Creating dataframe
      income = np.array(duncan_prestige.data.income)
      education = np.array(duncan_prestige.data.education)
      prestige = np.array(duncan_prestige.data.prestige)
      dat = pd.DataFrame({"income": income, "education": education, "prestige": prestige})

 ## Creating lists of column names for interactions 
    ## You could include any number variables in each list for your purposes 
      l1 = ["income"]
      l2 = ["education", "prestige"]


 ## Estimation
      r = orthogonalize(l1, l2, dat)
      r.head()

 ## Returns 
   # Dataframe
   #     income.education  income.prestige
   #  0         63.112648        34.246807
   #  1        229.849185       399.315757
   #  2        741.282854       732.789351
   #  3       -191.615460      -277.473163
   #  4        143.134978       276.041595

Comparative testing

In addition to each functions testing files, we replicated results from functions in r packages including:

Contributing to resmod

All contributions, bug reports, bug fixes, documentation improvements, enhancements, and ideas are welcome.

History

0.1.0 (2022-08-27)

Created the first function residual_center

  • First release on PyPI.

0.1.1 (2022-08-28)

Updated documentation

0.1.2 (2022-08-30)

Added functions for three-way interactions and multiple two-way interactions for specifying latent orthogonalized interaction terms for use in latent varaible SEM

  • for single three-way interaction term three_way
  • for multiple interactions for latent variable modeling orthogonalize

0.1.3 (2022-09-20)

Updated documentation

0.1.4 (2022-11-13)

Updated documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

resmod-0.1.4.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

resmod-0.1.4-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file resmod-0.1.4.tar.gz.

File metadata

  • Download URL: resmod-0.1.4.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.5

File hashes

Hashes for resmod-0.1.4.tar.gz
Algorithm Hash digest
SHA256 8e57cb4cefb4b6d13bb7f3bd131b37ec4229551a1c48245acba5b07237b69d47
MD5 9d156da3608f68c6134307def6a2e54f
BLAKE2b-256 d888093885eb04421708d894c85deb770251c4ff32e57d48627dc31529d7bf43

See more details on using hashes here.

File details

Details for the file resmod-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: resmod-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.5

File hashes

Hashes for resmod-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ababac32fb7bbed3808cdbf064afd270fcff8f3315e246e8ceb6a89fb64d2250
MD5 a56a0258b01981779e1816c266bb75e0
BLAKE2b-256 4457e34469a401905a4de0716d61217d0df2525c71842962e444a7ea804184f0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page