tlamadon / pytwoway

Two way models in python
MIT License
24 stars 7 forks source link

Issues importing pytwoway #9

Closed SMaric93 closed 2 years ago

SMaric93 commented 2 years ago

Hi, I'm having issues using pytwoway. I use Spyder as my IDE, although the same results are in Jupyter/Xcode, all running on lates MacOS. More specifically, when I try running "import pytwoway as tw" I get the following error:

runfile('/Users/PhD/Papers/AKM/Code/blm/code/untitled0.py', wdir='/Users/PhD/Papers/AKM/Code/blm/code') Traceback (most recent call last):

File ~/PhD/Papers/AKM/Code/blm/code/untitled0.py:9 in import pytwoway as tw

File ~/opt/anaconda3/envs/blm/lib/python3.9/site-packages/pytwoway/init.py:3 in from .twoway import TwoWay

File ~/opt/anaconda3/envs/blm/lib/python3.9/site-packages/pytwoway/twoway.py:8 in class TwoWay():

File ~/opt/anaconda3/envs/blm/lib/python3.9/site-packages/pytwoway/twoway.py:104 in TwoWay def cluster(self, measures=bpd.measures.cdfs(), grouping=bpd.grouping.kmeans(), stayers_movers=None, t=None, weighted=True, dropna=False):

AttributeError: module 'bipartitepandas.measures.measures' has no attribute 'cdfs'

I appreciate any help! Thanks!

adamoppenheimer commented 2 years ago

I apologize for this issue.

This is because I have been working on some major changes to pytwoway and the code uploaded to pip, as well as the documentation, are both out of date.

Here is how to get the code working for now:

The following is some code that should get the code running for the BLM estimator (please let me know if it's not working for you):

# Add PyTwoWay to system path (SET THIS TO WHERE YOU HAVE THE PACKAGE REPOSITORY CLONED)
import sys
sys.path.append('../../..')

###########################
##### Import packages #####
###########################

import numpy as np
import bipartitepandas as bpd
import pytwoway as tw
from matplotlib import pyplot as plt

#######################################
##### Check out parameter options #####
#######################################

print('----- BLM -----')
tw.blm_params().describe_all()
print('----- Clustering -----')
bpd.cluster_params().describe_all()
print('----- Cleaning -----')
bpd.clean_params().describe_all()
print('----- Simulation -----')
bpd.sim_params().describe_all()

#################################
##### Set parameter choices #####
#################################

nl = 2 # Number of worker types
nk = 3 # Number of firm types
blm_params = tw.blm_params({'nl': nl, 'nk': nk})
cluster_params = bpd.cluster_params({'measures': bpd.measures.CDFs(), 'grouping': bpd.grouping.KMeans(n_clusters=nk), 'is_sorted': True, 'copy': False})
clean_params = bpd.clean_params({'drop_returns': 'returners', 'copy': False})
sim_params = bpd.sim_params({'nl': nl, 'nk': nk})

#########################
##### Simulate data #####
#########################

sim_data = bpd.BipartiteDataFrame(bpd.SimBipartite(sim_params).simulate()[['i', 'j', 'y', 't', 'l']]).clean(clean_params).cluster(cluster_params).collapse(is_sorted=True, copy=False).to_eventstudy(is_sorted=True, copy=False)
print('Movers data')
display(sim_data[sim_data['m'] > 0])
print('Stayers data')
display(sim_data[sim_data['m'] == 0])

###########################################
##### Initialize and run BLMEstimator #####
###########################################

# Initialize BLM estimator
blm_fit = tw.BLMEstimator(blm_params)
# Fit BLM estimator
blm_fit.fit(jdata=sim_data[sim_data['m'] > 0], sdata=sim_data[sim_data['m'] == 0], n_init=40, n_best=5, ncore=1)

###############################
##### Investigate results #####
###############################

# Plot likelihood vs. connectedness
blm_fit.plot_liks_connectedness()

# Plot estimated worker-firm pair means for first period
blm_fit.plot_A1()
SMaric93 commented 2 years ago

Hi Adam, It works, thanks so much! Stefan