azmyrajab / polars_ols

Polars least squares extension - enables fast linear model polar expressions
MIT License
99 stars 9 forks source link

fit ols with polynomial formula #35

Open danielbaumes opened 1 week ago

danielbaumes commented 1 week ago

Hi, thank you so much for this project!

I have been trying to fit a polynomial of order 2 with a formula, but polars complains about "columnNotFoundError I(x**2) "

formula = "y ~ x + I(x**2)" expr = pls.compute_least_squares_from_formula(formula, mode='coefficients')

Can you advise if you can replicate this issue? Thanks

azmyrajab commented 1 week ago

Hi @danielbaumes, I'm sorry but I haven't yet supported full patsy syntax (it's a little hard w/o killing performance by materializing the dataframe first). I'll try to get the API extended for polynomials

May I suggest the polars expressions syntax in the meantime? x2 = (pl.col("x") ** 2).alias("x2"); pls.compute_least_squares("y", pl.col("x"), x2, add_intercept=True) or pl.col("y").least_squares.ols(pl.col("x"), pl.col("x") ** 2) etc.

danielbaumes commented 1 week ago

No worries, i just wanted to make sure that functionnality was not fully functionning for now. Yes indeed i went for the pre-calculation of the features in the dataframe before using least_squares.ols, works well, thanks!