user2050187
user2050187

Reputation: 175

Multiple linear regression python

I use multiple linear regression, I have one dependant variable (var) and several independant variables (varM1, varM2,...) I use this code in python:

z=array([varM1, varM2, varM3],int32)
n=max(shape(var))
X = vstack([np.ones(n), z]).T
a = np.linalg.lstsq(X, var)[0]

How can I calculate the R-square change for every variable with python ? I would like to see how the regression changes if I add or remove predictor variables.

Upvotes: 2

Views: 5675

Answers (1)

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 59005

If the broadcasting is correct along the way the following should give you the correlation coefficient R:

R = np.sqrt( ((var - X.dot(a))**2).sum() )

One full example of multi-variate regression:

import numpy as np

x1 = np.array([1,2,3,4,5,6])
x2 = np.array([1,1.5,2,2.5,3.5,6])
x3 = np.array([6,5,4,3,2,1])
y = np.random.random(6)

nvar = 3
one = np.ones(x1.shape)
A = np.vstack((x1,one,x2,one,x3,one)).T.reshape(nvar,x1.shape[0],2)

for i,Ai in enumerate(A):
    a = np.linalg.lstsq(Ai,y)[0]
    R = np.sqrt( ((y - Ai.dot(a))**2).sum() )
    print R

Upvotes: 2

Related Questions