Skip to content

Instantly share code, notes, and snippets.

@jaganadhg
Created October 9, 2013 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaganadhg/6901914 to your computer and use it in GitHub Desktop.
Save jaganadhg/6901914 to your computer and use it in GitHub Desktop.
multivariate linear regression in python
{
"metadata": {
"name": "multivariate linear regression in python"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"#From http://stackoverflow.com/questions/11479064/multivariate-linear-regression-in-python\n",
"import numpy as np\n",
"import statsmodels.api as sm\n",
"\n",
"y = [1,2,3,4,3,4,5,4,5,5,4,5,4,5,4,5,6,5,4,5,4,3,4]\n",
"\n",
"x = [\n",
" [4,2,3,4,5,4,5,6,7,4,8,9,8,8,6,6,5,5,5,5,5,5,5],\n",
" [4,1,2,3,4,5,6,7,5,8,7,8,7,8,7,8,7,7,7,7,7,6,5],\n",
" [4,1,2,5,6,7,8,9,7,8,7,8,7,7,7,7,7,7,6,6,4,4,4]\n",
" ]\n",
"\n",
"def reg_m(y, x):\n",
" ones = np.ones(len(x[0]))\n",
" X = sm.add_constant(np.column_stack((x[0], ones)))\n",
" for ele in x[1:]:\n",
" X = sm.add_constant(np.column_stack((ele, X)))\n",
" results = sm.OLS(y, X).fit()\n",
" return results\n",
"print reg_m(y, x).summary()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" OLS Regression Results \n",
"==============================================================================\n",
"Dep. Variable: y R-squared: 0.535\n",
"Model: OLS Adj. R-squared: 0.461\n",
"Method: Least Squares F-statistic: 7.281\n",
"Date: Wed, 09 Oct 2013 Prob (F-statistic): 0.00191\n",
"Time: 18:29:48 Log-Likelihood: -26.025\n",
"No. Observations: 23 AIC: 60.05\n",
"Df Residuals: 19 BIC: 64.59\n",
"Df Model: 3 \n",
"==============================================================================\n",
" coef std err t P>|t| [95.0% Conf. Int.]\n",
"------------------------------------------------------------------------------\n",
"x1 0.2424 0.139 1.739 0.098 -0.049 0.534\n",
"x2 0.2360 0.149 1.587 0.129 -0.075 0.547\n",
"x3 -0.0618 0.145 -0.427 0.674 -0.365 0.241\n",
"const 1.5704 0.633 2.481 0.023 0.245 2.895\n",
"==============================================================================\n",
"Omnibus: 6.904 Durbin-Watson: 1.905\n",
"Prob(Omnibus): 0.032 Jarque-Bera (JB): 4.708\n",
"Skew: -0.849 Prob(JB): 0.0950\n",
"Kurtosis: 4.426 Cond. No. 38.6\n",
"==============================================================================\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment