Last active
February 18, 2020 20:23
-
-
Save samcarlos/15d4e31feabfa6881de4d2596a674237 to your computer and use it in GitHub Desktop.
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
import numpy as np | |
from sklearn.linear_model import LinearRegression | |
from sklearn.ensemble import RandomForestRegressor | |
num_obs = 1000 | |
t = np.random.binomial(1, .5, num_obs) | |
x = np.random.binomial(1, .5, num_obs) | |
y = 10 + 2*t*x - t + np.random.normal(0, .1, num_obs) | |
#setting up data matrix with interaction | |
x_data = np.concatenate([q.reshape(-1,1) for q in [x, t, x*t]], axis = 1) | |
#inspect data | |
print(x_data[:5]) | |
#fit regression | |
reg = LinearRegression() | |
reg.fit(x_data, y) | |
#look at coefficients | |
print(np.round(reg.coef_, 2)) | |
print(np.round(reg.intercept_, 2)) | |
###.fit() -> .predict() Method | |
x_data_no_interaction = np.concatenate([q.reshape(-1,1) for q in [x, t]], | |
axis = 1) | |
#inspect data | |
print(x_data_no_interaction[:5]) | |
rf = RandomForestRegressor() | |
rf.fit(x_data_no_interaction, y.reshape(-1,1)) | |
x_data_no_interaction_0 = x_data_no_interaction.copy() | |
x_data_no_interaction_0[:,1] = 0 | |
x_data_no_interaction_1 = x_data_no_interaction.copy() | |
x_data_no_interaction_1[:,1] = 1 | |
pred_y_0 = rf.predict(x_data_no_interaction_0) | |
pred_y_1 = rf.predict(x_data_no_interaction_1) | |
ite = pred_y_1 - pred_y_0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment