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

Spline Support like Patsy #74

Closed
petrhrobar opened this issue Apr 26, 2022 · 7 comments
Closed

Spline Support like Patsy #74

petrhrobar opened this issue Apr 26, 2022 · 7 comments

Comments

@petrhrobar
Copy link

Would it be possible to also implement (apart from already implemented interactions and category variable) splines feature:

Patsy library does this really nicely.

Example

@matthewwardrop
Copy link
Owner

Hi @petrhrobar ,

Formulaic already supports poly (orthogonal polynomial) and bs (basis splines) bases. Which additional spline bases do you need?

@petrhrobar
Copy link
Author

Hey There,

COOL!. I was not aware of this. Is there any form of documentation covering the poly and bsfeatures?

@matthewwardrop
Copy link
Owner

Online documentation is a weak point... but we'll get there. Currently the only documentation is the grammar documentation, which indicates that support exists: https://matthewwardrop.github.io/formulaic/concepts/grammar/#transforms.

The API for these transforms is the same as patsy/R, and the inline documentation is thorough (e.g. https://github.com/matthewwardrop/formulaic/blob/main/formulaic/transforms/basis_spline.py#L37). The website documentation needs to be updated, but I already know this so I'll close this out for now :).

@petrhrobar
Copy link
Author

Thanks for that.

Just a follow-up - how to change degrees of freedom in string formula:

import seaborn as sns
from formulaic import Formula, formula
df = sns.load_dataset("tips")


Formula("total_bill ~ bs(size, df = 10)").get_model_matrix(df)

If I call the function itself, it works. however, having it changeable in the string is what I would like:

from formulaic.transforms import basis_spline, poly
pd.DataFrame(basis_spline(df['size'], degree = 10)).values

@matthewwardrop
Copy link
Owner

Hi again!

The bs function in the formula is literally basis_spline... so you would pass the arguments through directly. For example:

from formulaic.transforms import basis_spline, poly
pd.DataFrame(basis_spline(df['size'], degree = 10)).values

would be achieved using:

from formulaic import Formula
Formula("bs(size, degree = 10)").get_model_matrix(df)

Does this not work for you?

@matthewwardrop
Copy link
Owner

Ah... I see. There's a bug in the stateful evaluation that attempts to treat the 10 constant as a column to be evaluated. I'll resolve that ahead of the next release!

@matthewwardrop
Copy link
Owner

matthewwardrop commented Apr 30, 2022

The issue above was due to a bug / issue in astor <0.8. I bumped the dependency, and things should work now on the master branch; soon to be 0.3.4.

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

No branches or pull requests

2 participants