Skip to content

Conversation

@has2k1
Copy link
Owner

@has2k1 has2k1 commented Sep 10, 2019

@has2k1
Copy link
Owner Author

has2k1 commented Sep 10, 2019

@MartinGell

With this you would be able to use fomulae to define models for linear regressions. A good (orthogonal) poly method stagnated is in the patsy PR queue, but for non analytical work a basic one would do.

import numpy as np
import pandas as pd
from plotine import *

n = 100
np.random.seed(123)
mu = 0
sigma = 15
noise = np.random.randn(n)*sigma + mu

def poly(x, degree=1):
    """
    Fit Polynomial

    These are non orthogonal factors, but it may not matter if
    we only need this for predictions (without interpreting the
    coefficients) or visualisation.
    """
    d = {}
    for i in range(degree+1):
        if i == 1:
            d['x'] = x
        else:
            d[f'x**{i}'] = np.power(x, i)
    return pd.DataFrame(d)

x = np.linspace(-np.pi, np.pi, n)
df = pd.DataFrame({
    'x': x,
    'y': (x + x**2 + 7*x**3) + noise
})

(ggplot(df, aes('x', 'y'))
 + geom_point()
 + stat_smooth(method='lm', formula='y ~ poly(x, degree=3)', fill='red', se=True)
)

basic-poly

@krassowski
Copy link
Contributor

@has2k1 I see that your PR to statsmodels (6152) has been merged and statsmodels 0.11 released. Is there anything else holding this one out? Could we help in any way?

@has2k1
Copy link
Owner Author

has2k1 commented Apr 11, 2020

@krassowski, with the release of statsmodels v0.11 then this PR will get merged.

- Bump patsy to v0.5.1
- Bump pandas to latest version (v1.0.3)

Ref: #314
@has2k1 has2k1 merged commit e6d0c11 into master Apr 11, 2020
@has2k1 has2k1 deleted the smooth-formulae branch April 18, 2020 16:53
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.

Polynomial regression line

3 participants