r/pythonhelp • u/SaudSaudesen • 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
1
u/Goobyalus Nov 24 '22
What do you expect this line to do?
pdoes not exist here - you need to pass something topolynomthat does exist.