r/pythonhelp • • Nov 23 '22

Calculating R squared of a poly function

def polynom(x, p):
    r = np.array(x)
    s = np.array(x)
    for i in range(1, p):
        r = r*s
        jepps = np.column_stack((x, r))
    return jepps

def lollispoly(x,y, plot=False, vif = False):
    x = polynom(x, p)
    b_hatt = np.array(inv(x.T@x)@x.T@y)
    
    hattematrise = x@inv(x.T@x)@x.T
    y_hatt = hattematrise@y
    
    TSS = np.sum((y - np.mean(y))**2)
    ESS = np.sum((y_hatt - np.mean(y))**2)
    RSS = (TSS - ESS)
    
    R2 = ESS/TSS
    
    R2_tilpasset = 1-((RSS/x-p-1))/(TSS/(x-1))
    print(R2_tilpasset)

So I am trying to calculate the r squared of the poly function which is the first function (polynom). My error is that p is not defined, but I am not sure where to put it in, and everywhere I try gives me new errors. And I feel like my code is kinda unneccesary, is it also possible to short it down?All help is appreciated

Regards

2 Upvotes

3 comments sorted by

View all comments

1

u/Goobyalus Nov 24 '22
        x = polynom(x, p)

What do you expect this line to do? p does not exist here - you need to pass something to polynom that does exist.

1

u/SaudSaudesen Nov 24 '22

Yeah thats my problem, i dont know ho to define. P is supposed to be a input or which square of polyfunction this is

1

u/Goobyalus Nov 24 '22

If I want to call a function subtract(a, b), I don't define variables a and b, I pass in the values that I want to be assigned to a and b in the function.

E.g. I might call subtract(10, 5), so inside subtract, a would be 10, and b would be 5.


So you need to pass in what you want p to be, not a variable called p. Does that make sense?