import statsmodels.api as sm
OLS (Ordinary Least Squares) Functions
from statsmodels.formula.api import ols
One-Way ANOVA
model = ols(formula = "y ~ C(x)", data = df).fit()
sm.stats.anova_lm(model, typ = 2)
Model statistics are given in the order of args. Models must have been fit using the formula api.
One-Way ANOVA Post hoc test
from statsmodels.stats.multicomp import pairwise_tukeyhsd
tukey_oneway = pairwise_tukeyhsd(endog = df["y"], groups = df["x"], alpha = 0.05)
tukey_oneway.summary()
Two-Way ANOVA
model = ols(formula = "y ~ C(x1) + C(x2) + C(x1):C(x2)", data = df).fit()
sm.stats.anova_lm(model, typ = 2)
Binomial Logistic Regression
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size=0.3)
#Logicstic Regression Classifier
clf = LogisticRegression().fit(X_train,Y_train)
OR
clf = LogisticRegression(penalty='none').fit(X_train,Y_train) #for unscaled predictors
clf.coef_
clf.intercept_
Plotting Logistic Regression
sns.regplot(x="x_col", y="y_col", data=df, logistic=True)
clf.predict(X_test)
clf.predict_proba(X_test)