How to evaluate a statistical model with given (=not optimised) coefficients?

Viewed 26

I have a very simple statistical model y = a * b.

I know all the variables (a, b, and y) and I do not want to add any optimized coefficients (the intercept should be 0 and the coefficients on a and b are effectively 1).

What I need is the explained variance (R2), but I cannot figure out a simple way to tease it out without calculating all the formulas from scratch. Is there anything similar to statsmodel OLS rsquared but for a model with fixed coefficients?

I mainly use python but happy to use R if it is easier.

1 Answers

You can use scikit-learn's r2_score method, which you can find here. All you have to do is actually to compute your predicted dependent variable y using your coefficients and your data.

Here's a minimal working example:

from sklearn.metrics import r2_score

# here's the truth value of y
y_true = [3, -0.5, 2, 7]

# here's your single featue x
x = [1, 2.3, 3.1, 0.9]

# and here's your coefficient b and your intercept a
i = [0, 0, 0, 0]
b = 0.3

# compute the predicted value of y using your coefficients
y_pred = [n+m for (n, m) in zip(i, [j*b for j in x])]

# display the R2 score using scikit-learn's r2_score method
print(r2_score(y_true, y_pred))

On a side note, computing the R2 from scratch would not be much more complex than this...

Related