Calculating f-statistic in sklearn

Viewed 1767

I've been googling fervently and cannot find an answer.

How do I calculate the f-statistic using sklearn? Do I really have to compute it by hand, given the formula:

f-statistic formula

(where is the number of observations and is the number of variables).

And... if I calculate it by hand, how do I get the associated p-value?

1 Answers

I hope this helps! To find the f-statistic try:

import sklearn
from sklearn.linear_model import LinearRegression

X, y = df[['x1','x2']], df[['y']]

model=LinearRegression().fit(X, y)

Rsq = model.score

fstat = (Rsq/(1-Rsq))*((N-K-1)/K) #you should find N and K yourself

To find the p-value, you can use the python package symbulate

import symbulate as sm

dfN = 5 -1 #degrees of freedom in the numerator of F-statistic
dfD = 2 -1 #degrees of freedom in the denominator of F-statistic

pVal = 1-sm.F(dfN,dfD).cdf(fstat)
Related