2

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.

2
  • What is the var variable? Commented Jul 18, 2013 at 15:55
  • it is the dependant variable. in my case, it is a concentration Commented Jul 22, 2013 at 6:32

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

There is no such variable shape, so this implementation does not run.
@rjurney it does run. shape actually is not a variable but an attribute of the ndarray object... check if you have such an object...
shape tells you the size of the array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.