DoWhyProductivityExerciseSolutions

py

School

Boston University *

*We aren’t endorsed by this school

Course

BA472

Subject

Computer Science

Date

Jan 9, 2024

Type

py

Pages

3

Uploaded by MagistrateHyena2457

Report
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed Oct 11 15:27:06 2023 @author: nobaycik """ import pandas as pd import numpy as np IncomeData = pd.read_csv("/Users/shunnosuke/Desktop/BA472/EmployeeWorkFromHome Data.txt", delimiter = "\t") print(IncomeData) #TransportationDown Introversion NumberOfKids WorkFromHome ProductivityScore print(IncomeData) import dowhy from dowhy import CausalModel import warnings warnings.filterwarnings('ignore') #Create a causal diagram causal_graph = """ digraph { TransportationDown; Introversion; NumberOfKids; WorkFromHome; Introversion -> WorkFromHome; WorkFromHome -> ProductivityScore; Introversion -> ProductivityScore; NumberOfKids -> WorkFromHome; NumberOfKids -> ProductivityScore; TransportationDown -> WorkFromHome; } """ from dowhy import CausalModel #Build the model in DoWhy model= CausalModel( data = IncomeData, graph=causal_graph.replace("\n", " "), treatment='WorkFromHome',
outcome='ProductivityScore') model.view_model() #Identify the causal effect. Pearl's calculations in the background estimands = model.identify_effect() #print(estimands) #----------------------------------------------BACKDOOR LINEAR REGRESSION---------------------------------------------- #Causal Effect Estimation. There are few methods to Estimate the Causal Effect causal_estimate_reg = model.estimate_effect(estimands, method_name="backdoor.linear_regression", test_significance=True) #Validation of assumptions. Add a placebo treatment. Replace the true treatment variable with an independent random variable; #If the assumption was correct, the estimate should go close to zero. placebo_refute_blr = model.refute_estimate(estimands, causal_estimate_reg, method_name="placebo_treatment_refuter", num_simulations=20) #Validation of assumptions. Data Subset Refuter replace the given dataset with a randomly selected subset; #If the assumption was correct, the estimation should not change that much. #Validation of assumptions. Add a placebo treatment. Replace the true treatment variable with an independent random variable; #If the assumption was correct, the estimate should go close to zero. subset_refute_bpsw = model.refute_estimate(estimands,causal_estimate_ipw, method_name = "data_subset_refuter", num_simulations=20)
placebo_refute_bpsw = model.refute_estimate(estimands,causal_estimate_ipw, method_name = "placebo_treatment_refuter", num_simulations=20) print("BACKDOOR PROPENSITY SCORE WEIGHTING RESULTS") print("Results with placebo treatment refuter:") print(placebo_refute_bpsw) print("Results with data subset refuter:") print(subset_refute_bpsw) #---------------------------------------------------------------- ----------------------------------------- #For a refutation test, the p-value denotes whether the test has found a problem with the estimate. #If p-value <0.05, this means that the estimate has a problem. If not, then we cannot conclude anything. #Since both p-values are >0.05, it means that the two tests are unable to find a problem in the estimate. #Note that refutations test the correctness of the estimating procedure, but not whether the effect is significantly away from zero. #---------------------------------------------------------------- -----------------------------------------
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help