In [1]: import numpy as np
In [1]: import statsmodels.api as sm
Create some data
In [1]: nsample = 50
In [1]: sig = 0.25
In [1]: x1 = np.linspace(0, 20, nsample)
In [1]: X = np.c_[x1, np.sin(x1), (x1 - 5)**2, np.ones(nsample)]
In [1]: beta = [0.5, 0.5, -0.02, 5.]
In [1]: y_true = np.dot(X, beta)
In [1]: y = y_true + sig * np.random.normal(size=nsample)
Setup and estimate the model
In [1]: olsmod = sm.OLS(y, X)
In [1]: olsres = olsmod.fit()
In [1]: print olsres.params
In [1]: print olsres.bse
In-sample prediction
In [1]: ypred = olsres.predict(X)
Create a new sample of explanatory variables Xnew, predict and plot
In [1]: x1n = np.linspace(20.5, 25, 10)
In [1]: Xnew = np.c_[x1n, np.sin(x1n), (x1n - 5)**2, np.ones(10)]
In [1]: ynewpred = olsres.predict(Xnew) # predict out of sample
In [1]: print ypred
In [1]: import matplotlib.pyplot as plt
In [1]: plt.figure();
In [1]: plt.plot(x1, y, 'o', x1, y_true, 'b-');
In [1]: plt.plot(np.hstack((x1, x1n)), np.hstack((ypred, ynewpred)), 'r');
In [1]: plt.title('OLS prediction, blue: true and data, fitted/predicted values:red');