Search code examples
pythonregressionnon-linear-regression

Fitting sine cosine curves in Python


I am trying to transition from R to Python for my time series analysis - but am finding it quite hard. The code below is what I would have used in R - to regress a sine curve onto some data with a known period.

year <- c(0:100)
lm(data~sin(2*pi*year/15)+cos(2*pi*year/15))

Now I want to do the same in Python I am coming across very long methods involving making initial guesses then optimising etc. What is the simplest way to achieve the comparable result?


Solution

  • I did not get exactly what you are looking for, lm mean linear model, you could try linear regression in sklearn as follows:

        import numpy as np
        from sklearn.linear_model import LinearRegression
        import matplotlib.pyplot as plt
    
        year        = np.arange(0, 100, 1)
        year = np.reshape(year, (1, -1))
        year_predict        = np.arange(100, 200, 1)
        year_predict        = np.reshape(year_predict, (1, -1))
    
        y   = np.sin(2*np.pi*year/15)+np.cos(2*np.pi*year/15)
        lm = LinearRegression()
        lm.fit(year, y)
    
        y_pred = lm.predict(year_predict)
    
        plt.plot(year[0,:], y[0,:])
        plt.plot(year_predict[0,:], y_pred[0,:])
        plt.ylabel('np.sin(2*pi*year/15)+np.cos(2*pi*year/15)')
        plt.show()
    

    More info you can find here: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html

    If you have something unclear, write here. We could help you