GithubHelp home page GithubHelp logo

quentinandre / pyprocessmacro Goto Github PK

View Code? Open in Web Editor NEW
91.0 91.0 28.0 6.12 MB

A Python library for moderation, mediation and conditional process analysis.

License: MIT License

Python 100.00%
data-analysis data-science data-visualization mediation-analysis plotting process regression-models statistics

pyprocessmacro's People

Contributors

billmanh avatar fran4 avatar joonro avatar quentinandre avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pyprocessmacro's Issues

Cropped name of first outcome model

Hi Quentin,
first of all thanks for this great library that includes the complete process macro from Hayes!
And found just small bug in one dictionary.
It seems that the first entry in "outcome_models" dictionary is saved only with first character.
Tried it for few models and with different variables. It occured in all tested cases.
Example is attached.
Thank you!

example.docx

Implement better tests

Right now, the tests are pretty minimal:

  • Compare the direct and indirect effects for all OLS models to the actual output of Process.
  • Does the same for Logistic models.

[feature] Automatically cast categorical variable as integer

Here is my code:

from pyprocessmacro import Process

df["present_narration_bin"] = (
    df["present_narration"] == "high present narration"
).astype(float)
p = Process(data=df, model=4, x="present_narration_bin", y="attitude", m=["credible"])

df.present_narration has type:

Name: present_narration, Length: 505, dtype: category
Categories (2, object): ['high present narration', 'low present narration']

statsmodels automatically casts it to float so I thought it would be nice to be able to write:

p = Process(data=df, model=4, x="present_narration", y="attitude", m=["credible"])

pyprocessmacro crash on newer versions of numpy

When running the original script I was getting the error:
No loop matching the specified signature and casting was found for ufunc inv
Looking through the stack I found that this error was happening in the numpy.linalg function and not in the pyprocessmacro module.

After looking around I found that many of the numpy functions expect a dtype float instead of object. I was able to fix the issue by going into the pyprocessmacro.models and casting them as float types:

from:

y = self._endog
x = self._exog

to

y = self._endog.astype(float)
x = self._exog.astype(float)

I had to do this in a couple of places. Once I did this the module worked perfectly.

Thanks for this awesome application. If you make me a contributor I'd love to share this modification. I don't think it will work with current versions of numpy.

displaying total effect

Dear Quentin,

is there a way to display or get values of total effect from Process object?

dtype error

When I try model 4 similar to the examples, I get the following error:
UFuncTypeError: Cannot cast ufunc 'inv' input from dtype('O') to dtype('float64') with casting rule 'same_kind'

All of the variables I'm using are floats, so I'm not sure what could be causing this. I tried dropping na and installing the latest version, just in case those helped, but I'm still seeing the error.

Thanks for your help and for creating this invaluable resource for python users!

Bootstrap estimates

I am doing a simple model of A --> B --> C.
When the get_bootstratp_estimates function is run, it returns the bootstrap estimates for the model of A and B predicting C. However, these estimates are not the ones needed to estimate the bootstrap estimates for the indirect effect. We would also need the estimates for the A to B model.

p.summary() makes the script crash..

Hi. I got this problem that my script crashes when running p.summary(). I use model 1 and 4, and the same thing happens in both cases. I've asked a question on StackOverflow, but hasn't got any relevant response yet. Do you have a chance to have look at my question on StackOverflow, or would you rather have me reproduce it her? To see my question on StackOverflow, see link: (https://stackoverflow.com/questions/71250868/why-is-p-summary-causing-my-script-crash-when-running-pyprocessmacro-i-use-mo)
I really hope you can help me crack this problem!! Kind regards, Christian

Bug for model58,59

Hey QuentinAndre,
Thanks for your great work.
When I run this model58 "Process(data=df, model=58, x="x1", m=["m1"], w="m2",y="y")", the same as model59, and the results as following,
"mod1, mod2 = self._moderators_symb # Only two moderators
ValueError: not enough values to unpack (expected 2, got 1) ".
However, Only one "w" is enough for model58 and model 59. So some bugs here?

Great thanks once more.

Values of total effect

Dear Quentin,
Although the question has been asked before, #18 . I still want to ask if there is a proper way to get values of total effect from Process object?And if I have to use an other method, is there a suitable example for me to learn from?

Model 4 producing a strange key error

Hello,

I'm using this module to perform simple mediation analysis and simple moderation analysis. Model 1 simple moderation works without issue, however, when I try to call model 4 I get the following traceback :

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-35-6bc20e06037d> in <module>
----> 1 p = Process(data=df, model=4, x="cov_transformed", y="h_motive", m="ov_transformed")

~/Desktop/his_factor_analysis/fac_an/lib/python3.7/site-packages/pyprocessmacro/process.py in __init__(self, data, model, modval, cluster, boot, seed, mc, conf, effsize, jn, hc3, controls, controls_in, total, contrast, center, quantile, detail, percent, logit, iterate, convergence, precision, suppr_init, **kwargs)
    759 
    760         # Generate the statistical models that will be estimated
--> 761         self.outcome_models = self._gen_outcome_models()
    762 
    763         # Rename the dictionary of custom spotlight values, and generating the spotlight values.

~/Desktop/his_factor_analysis/fac_an/lib/python3.7/site-packages/pyprocessmacro/process.py in _gen_outcome_models(self)
   1079                 self._symb_to_ind,
   1080                 self._symb_to_var,
-> 1081                 self.options,
   1082             )
   1083         models[self.iv] = model_yfull

~/Desktop/his_factor_analysis/fac_an/lib/python3.7/site-packages/pyprocessmacro/models.py in __init__(self, data, endogvar, exogvars, symb_to_ind, symb_to_var, options)
    247         self, data, endogvar, exogvars, symb_to_ind, symb_to_var, options=None
    248     ):
--> 249         super().__init__(data, endogvar, exogvars, symb_to_ind, symb_to_var, options)
    250 
    251     def _estimate(self):

~/Desktop/his_factor_analysis/fac_an/lib/python3.7/site-packages/pyprocessmacro/models.py in __init__(self, data, endogvar, exogvars, symb_to_ind, symb_to_var, options)
    142 
    143         endog_ind = self._symb_to_ind[self._endogvar]
--> 144         exog_ind = [self._symb_to_ind[var] for var in self._exogvars]
    145         self._endog = data[:, endog_ind]
    146         self._exog = data[:, exog_ind]

~/Desktop/his_factor_analysis/fac_an/lib/python3.7/site-packages/pyprocessmacro/models.py in <listcomp>(.0)
    142 
    143         endog_ind = self._symb_to_ind[self._endogvar]
--> 144         exog_ind = [self._symb_to_ind[var] for var in self._exogvars]
    145         self._endog = data[:, endog_ind]
    146         self._exog = data[:, exog_ind]

KeyError: 'm2'




I'm working in a jupyter notebook on a mac running catalina. Any help would be appreciated. 

Conda and pyprocessmacro

Hi.

Is there a way to install pyprocessmacro with Anaconda?

When trying to use conda install pyprocessmacro on my Windows machine I get this output:

--
_**Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.

PackagesNotFoundError: The following packages are not available from current channels:

  • pyprocessmacro

Current channels:

To search for alternate channels that may provide the conda package you're
looking for, navigate to

https://anaconda.org

and use the search bar at the top of the page._

PS: I get no results for my search for pyprocessmacro on anaconda.org.

Kind regards, Christian Ruge

controls_in="both" does not seem to work

Hi -

Both controls_in="x_to_m" and controls_in="all_to_y" seem to work, but when I use controls_in="both" option, it seems controls are not being added at all. It does say variable names in Statistical Controls:, but the outputs do not list any of these variables in both x_to_m or all_to_y cases. (I tested with model 4 and 7).

Also I verified that it shows the same results when I don't use any control, so it seems like the control variables are ignored when controls_in="both" is used.

MM_index_summary

Is there a way to get the moderation mediation index summary into a readable dataframe for model 7?

seaborn should be specified as a dependency?

Hi,

When I did pip install pyprocessmacro, I had it complained about seaborn. When I install seaborn with pip install seaborn I could install the module successfully - I wonder if seaborn should be specified as a dependency?

Plotting: show the mean values of the mediator instead of the effect

Hi there,

first of all, thank you very much for your great work.

I have a little question regarding the plotting capabilities.

At the moment it is possible to plot the conditional direct and indirect effects at various levels of the moderator.

I wondered, if it is also possible to show the mean values of the mediator instead of the effect like it is possible with the process macro for SPSS.

Capture

Kind regards,
Raphael

negative binomial regression

I use model4, where x to mediator m is an ols regression, and x, m to y is a negative binomial regression. How can I modify the code?

I really hope to receive a reply! Thank~

Install fails with syntax error

pip install pyprocessmacro
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting pyprocessmacro
Using cached https://files.pythonhosted.org/packages/65/20/f34a67260cc0aaff0de675871089fd3eeccf995c1d738a6444e6b926dffc/PyProcessMacro-0.9.7.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "", line 1, in
File "/tmp/pip-install-8NXIVg/pyprocessmacro/setup.py", line 3, in
import pyprocessmacro
File "pyprocessmacro/init.py", line 6, in
from .process import Process
File "pyprocessmacro/process.py", line 4, in
from .models import OLSOutcomeModel, DirectEffectModel, ParallelMediationModel, LogitOutcomeModel
File "pyprocessmacro/models.py", line 365
e[i], be[i], *_ = self._indirect_effect_at(i, {})
^
SyntaxError: invalid syntax

----------------------------------------

Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-8NXIVg/pyprocessmacro/

Analyzing a 3 column pandas data frame

Dear Quentin,

First of all, thank you for creating this library. Apparently this Hayes method is very popular among the humanities and I had no idea how to transfer the algorithms from SPSS/R to Python. Thank FSM that you have already done it :)

I'm trying to follow this example where there are just 3 variables: independent, outcome and moderator. The data can be downloaded from here and the CSV file is in ⁨hayes2018data⁩/disaster⁩/disaster⁩.csv.

                      

I looked among the examples you have provided in the README.md and I can't find an example with only three variables. My attempt to ignore all other parameters m, z, model also caused an error:

ValueError: The variables supplied do not match the definition of Model 3 Expected variable(s) not supplied: m

I would appreciate it if you could help me understand what is the problem and how I can resolve it. Thanks in advance and looking forward to hearing back.

Best,
Foad

Missing test_models_accuracy.py

Hi -

Thank you so much for implementing this. It seems test_models_accuracy.py that is mentioned in README is missing - could you please add this so I can run tests when making changes? Thank you so much.

Need help

I just came across your implementation of PROCESS macro. When I tried installing it using the command 'pip install pyprocessmacro', I get the following error:
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-oebhO2/pyprocessmacro/

Not sure what I am doing wrong. Request your help.

Regards
SBS

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.