matthewwardrop / formulaic

A high-performance implementation of Wilkinson formulas for Python.
MIT License
346 stars 25 forks source link

Feature Request: circular transform #118

Closed huangziwei closed 2 years ago

huangziwei commented 2 years ago

Hello. Thank you for this great project!

I am doing some simple circular regression recently and would love to see this can be handled directly by formulaic:

y ~ circ(theta)

where circ(theta) transforms theta into two columns: cos(theta) and sin(theta).

huangziwei commented 2 years ago

oops. just found out that I can just write Numpy functions within the formula!

y ~ np.cos(theta) + np.sin(theta) does the job.

Great features!

huangziwei commented 2 years ago

I just hit another problem...

Formulaic raises an FactorEvaluationError when using numpy in formula with model_spec.get_model_matrix():

import numpy as np
import pandas as pd
from formulaic import model_matrix

theta = np.linspace(-np.pi, np.pi, 100)
y = 1 + theta * 2
df = pd.DataFrame(np.vstack([y, theta]).T, columns=['y', 'theta'])

Y, X = model_matrix('y ~ np.cos(theta) + np.sin(theta) - 1', data=df)
Xnew = pd.DataFrame(np.linspace(-np.pi, np.pi, 50), columns=['theta'])
X.model_spec.get_model_matrix(Xnew)

FactorEvaluationError: Unable to evaluate factor np.cos(direction). [NameError: name 'np' is not defined]

maybe a dedicated transform for circular data can handle this better?

huangziwei commented 2 years ago

Sorry for spamming ... upgrading to 0.5.2 solved the issue...