This file contains 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
# OLS | |
OLS = LinearRegression() | |
OLS.fit(X, y.ravel()) | |
# Plot | |
plt.figure(figsize = (10, 6)) | |
plt.scatter( | |
X, | |
y, | |
c = 'teal' |
This file contains 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
# Plot | |
plt.figure(figsize = (10, 6)) | |
plt.scatter( | |
X, | |
y, | |
c = 'teal' | |
) | |
plt.title('Data') | |
plt.show() |
This file contains 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
# Set auto reload | |
%reload_ext autoreload | |
%autoreload 2 | |
# Import libraries | |
import pandas as pd | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from sklearn.datasets import make_regression |
This file contains 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
# HAC Result | |
# 3 | |
lags = None | |
print(f'Number of lags: {lags}') | |
HAC = newResult.get_robustcov_results( | |
cov_type = 'HAC', | |
maxlags = lags | |
) |
This file contains 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
# HAC Result | |
# 2 | |
lags = int(df.shape[0] ** (1 / 4)) | |
print(f'Number of lags: {lags}') | |
HAC = newResult.get_robustcov_results( | |
cov_type = 'HAC', | |
maxlags = lags | |
) |
This file contains 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
# HAC Result | |
# 1 | |
lags = int(4 * (df.shape[0] / 100) ** (2 / 9)) | |
print(f'Number of lags: {lags}') | |
HAC = newResult.get_robustcov_results( | |
cov_type = 'HAC', | |
maxlags = lags | |
) |
This file contains 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
# HAC Adjustment | |
newModel = smf.ols( | |
'logitODR ~ 1 + GDP_C_lg12 + MPI_C_lg12', | |
data = df | |
) | |
newResult = newModel.fit() |
This file contains 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
# Select variables for linear regression model | |
X = sm.add_constant(df[['GDP_C_lg12', 'MPI_C_lg12']]) #Add intercept | |
y = df['logitODR'] | |
# Linear regression model | |
model = sm.OLS(y, X) | |
result = model.fit() | |
print(result.summary()) |
This file contains 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
# Set auto reload | |
%reload_ext autoreload | |
%autoreload 2 | |
# Import libraries | |
import warnings | |
import pandas as pd | |
import numpy as np | |
import statsmodels.api as sm | |
import statsmodels.formula.api as smf |
This file contains 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
# Average spending price per period | |
df['TotalSale'] = df['Price'] * df['Quantity'] #Calculation total sale | |
dfPrice = df.groupby( | |
['cohortMonth', 'cohortIndex'], | |
as_index = False | |
)['TotalSale'].mean() | |
dfPrice = pd.pivot_table( | |
dfPrice, | |
values = 'TotalSale', |