{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# DoWhy example on the Lalonde dataset\n", "\n", "Thanks to [@mizuy](https://github.com/mizuy) for providing this example. Here we use the Lalonde dataset and apply IPW estimator to it. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "R[write to console]: Loading required package: MASS\n", "\n", "R[write to console]: ## \n", "## Matching (Version 4.9-3, Build Date: 2018-05-03)\n", "## See http://sekhon.berkeley.edu/matching for additional documentation.\n", "## Please cite software as:\n", "## Jasjeet S. Sekhon. 2011. ``Multivariate and Propensity Score Matching\n", "## Software with Automated Balance Optimization: The Matching package for R.''\n", "## Journal of Statistical Software, 42(7): 1-52. \n", "##\n", "\n", "\n" ] }, { "data": { "text/plain": [ "array(['Matching', 'MASS', 'tools', 'stats', 'graphics', 'grDevices',\n", " 'utils', 'datasets', 'methods', 'base'], dtype='" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "estimate.interpret(method_name=\"confounder_distribution_interpreter\",var_type='discrete',\n", " var_name='married', fig_size = (10, 7), font_size = 12)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sanity check: compare to manual IPW estimate" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Causal Estimate is 1639.8546888423534\n" ] } ], "source": [ "df = model._data\n", "ps = df['ps']\n", "y = df['re78']\n", "z = df['treat']\n", "\n", "ey1 = z*y/ps / sum(z/ps)\n", "ey0 = (1-z)*y/(1-ps) / sum((1-z)/(1-ps))\n", "ate = ey1.sum()-ey0.sum()\n", "print(\"Causal Estimate is \" + str(ate))\n", "\n", "# correct -> Causal Estimate is 1634.9868359746906" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }