Last active
March 10, 2025 09:18
-
-
Save jmquintana79/4ec64e5f9860b8d413284e19690c1742 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import statsmodels.api as sm | |
def analysis_lr(df:pd.DataFrame, l_columns_X:list, column_Y:str, alpha:float = .01)->pd.DataFrame: | |
# validate arguments | |
assert isinstance(df, pd.DataFrame) and len(df) > 0 | |
for c in l_columns_X + [column_Y]: | |
assert c in df.columns.tolist() | |
# data preparation | |
X = df[l_columns_X] | |
X = sm.add_constant(X) # Agrega la constante | |
Y = df[column_Y] | |
# fit | |
modelo = sm.OLS(Y, X).fit() | |
# collect analysis results | |
coeficientes = modelo.params | |
std_err = modelo.bse | |
p_values = modelo.pvalues | |
conf_int = modelo.conf_int() | |
r_squared = modelo.rsquared | |
# storing results | |
conf_int.columns = ["ci_i", "ci_f"] | |
dfr = pd.concat([ | |
pd.DataFrame(coeficientes, columns = ["coefs"]), | |
pd.DataFrame(std_err, columns = ["std_err"]), | |
pd.DataFrame(p_values, columns = ["p_values"]), | |
conf_int | |
], axis = 1) | |
dfr["r_squared"] = np.full(len(dfr), r_squared) | |
dfr["is_reliable"] = dfr["p_values"] < alpha | |
#return | |
return dfr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment