Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Introduced pm.TidyData class #4447

Closed
wants to merge 2 commits into from
Closed

[WIP] Introduced pm.TidyData class #4447

wants to merge 2 commits into from

Conversation

kc611
Copy link
Contributor

@kc611 kc611 commented Jan 29, 2021

Link to Issue: #3953

As mentioned in the issue, added a pm.TidyData class for automatically translating strings from a xarray DataSet into integer arrays for indexing in models. The input (currently) is expected to be a xarray.Dataset.

This is just a basic implementation to see if it's going in the right direction. New feature suggestions are welcome. The expected usage is as follows :

import pandas as pd
import pymc3 as pm
import numpy as np
import arviz as az
import theano 
from pymc3.data import TidyData
df = pd.DataFrame({
    'treatment':['Carboplatin']*3 + ['Adriamycin']*4 + ['Fluorouracil']*3,
    'age':np.random.randint(40,80,size=10),
    'location':['Berlin']*6 + ['London']*4,
    'outcome':np.random.randn(10),
})
df.index.name = 'patient'

xdf = df.to_xarray()

with pm.Model() as model:
    data = TidyData(xdf,import_dims=['treatment','location','patient','outcome'])

    intercept = pm.Flat('intercept')
    treatment = pm.Normal('b_treat',sd=0.2,mu=0,dims='treatment')
    location = pm.Normal('b_location',sd=0.2,mu=0,dims='location')
    age = pm.Normal('b_age',sd=0.2,mu=0)
    mu = (intercept
        + treatment[data.get_indexed('treatment')]
        + location[data.get_indexed('location')]
        + age*data['age']
        )
    pm.Normal('y',mu=mu,sd=1,dims='patient',observed=data['outcome'])
    trace = pm.sample()
    tr = az.from_pymc3(trace=trace,model=model)

@kc611
Copy link
Contributor Author

kc611 commented Jan 29, 2021

@AlexAndorra Have a look.

@kc611 kc611 closed this Jun 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant