{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Stata Kernel Example\n", "\n", "- Author: Wenzhi Ding\n", "- Date: 2022-07-06\n", "\n", "This notebook is to provide an example of using [Stata Kernel](https://kylebarron.dev/stata_kernel/) in Jupyter.\n", "\n", "Actually, this is nothing different from the way we usually use Stata, except for a few iPython Magic Commands (e.g. `%head`)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1978 automobile data)\n" ] } ], "source": [ "sysuse auto, clear" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
makepricempgrep78headroomtrunkweightlengthturndisplacementgear_ratioforeign
1AMC Concord40992232.5112930186401213.5799999Domestic
2AMC Pacer47491733113350173402582.53Domestic
3AMC Spirit379922.3122640168351213.0799999Domestic
4Buick Century48162034.5163250196401962.9300001Domestic
5Buick Electra78271544204080222433502.4100001Domestic
\n", "
" ], "text/plain": [ "\n", " +-----------------------------------------------------------------+\n", " 1. | make | price | mpg | rep78 | headroom | trunk | weight |\n", " | AMC Concord | 4,099 | 22 | 3 | 2.5 | 11 | 2,930 |\n", " |-----------------------------------------------------------------|\n", " | length | turn | displa~t | gear_r~o | foreign |\n", " | 186 | 40 | 121 | 3.58 | Domestic |\n", " +-----------------------------------------------------------------+\n", "\n", " +-----------------------------------------------------------------+\n", " 2. | make | price | mpg | rep78 | headroom | trunk | weight |\n", " | AMC Pacer | 4,749 | 17 | 3 | 3.0 | 11 | 3,350 |\n", " |-----------------------------------------------------------------|\n", " | length | turn | displa~t | gear_r~o | foreign |\n", " | 173 | 40 | 258 | 2.53 | Domestic |\n", " +-----------------------------------------------------------------+\n", "\n", " +-----------------------------------------------------------------+\n", " 3. | make | price | mpg | rep78 | headroom | trunk | weight |\n", " | AMC Spirit | 3,799 | 22 | . | 3.0 | 12 | 2,640 |\n", " |-----------------------------------------------------------------|\n", " | length | turn | displa~t | gear_r~o | foreign |\n", " | 168 | 35 | 121 | 3.08 | Domestic |\n", " +-----------------------------------------------------------------+\n", "\n", " +-----------------------------------------------------------------+\n", " 4. | make | price | mpg | rep78 | headroom | trunk | weight |\n", " | Buick Century | 4,816 | 20 | 3 | 4.5 | 16 | 3,250 |\n", " |-----------------------------------------------------------------|\n", " | length | turn | displa~t | gear_r~o | foreign |\n", " | 196 | 40 | 196 | 2.93 | Domestic |\n", " +-----------------------------------------------------------------+\n", "\n", " +-----------------------------------------------------------------+\n", " 5. | make | price | mpg | rep78 | headroom | trunk | weight |\n", " | Buick Electra | 7,827 | 15 | 4 | 4.0 | 20 | 4,080 |\n", " |-----------------------------------------------------------------|\n", " | length | turn | displa~t | gear_r~o | foreign |\n", " | 222 | 43 | 350 | 2.41 | Domestic |\n", " +-----------------------------------------------------------------+\n", "\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%head 5" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
makepricempgrep78headroomtrunkweightlengthturndisplacementgear_ratioforeign
5Buick Electra78271544204080222433502.4100001Domestic
6Buick LeSabre57881834213670218432312.73Domestic
11Cad. Deville113851434204330221444252.28Domestic
15Chev. Impala57051644203690212432502.5599999Domestic
23Dodge St. Regis63421724.5213740220462252.9400001Domestic
\n", "
" ], "text/plain": [ "\n", " +--------------------------------------------------------------------+\n", " 5. | make | price | mpg | rep78 | headroom | trunk | weight |\n", " | Buick Electra | 7,827 | 15 | 4 | 4.0 | 20 | 4,080 |\n", " |--------------------------------------------------------------------|\n", " | length | turn | displa~t | gear_r~o | foreign |\n", " | 222 | 43 | 350 | 2.41 | Domestic |\n", " +--------------------------------------------------------------------+\n", "\n", " +--------------------------------------------------------------------+\n", " 6. | make | price | mpg | rep78 | headroom | trunk | weight |\n", " | Buick LeSabre | 5,788 | 18 | 3 | 4.0 | 21 | 3,670 |\n", " |--------------------------------------------------------------------|\n", " | length | turn | displa~t | gear_r~o | foreign |\n", " | 218 | 43 | 231 | 2.73 | Domestic |\n", " +--------------------------------------------------------------------+\n", "\n", " +--------------------------------------------------------------------+\n", " 11. | make | price | mpg | rep78 | headroom | trunk | weight |\n", " | Cad. Deville | 11,385 | 14 | 3 | 4.0 | 20 | 4,330 |\n", " |--------------------------------------------------------------------|\n", " | length | turn | displa~t | gear_r~o | foreign |\n", " | 221 | 44 | 425 | 2.28 | Domestic |\n", " +--------------------------------------------------------------------+\n", "\n", " +--------------------------------------------------------------------+\n", " 15. | make | price | mpg | rep78 | headroom | trunk | weight |\n", " | Chev. Impala | 5,705 | 16 | 4 | 4.0 | 20 | 3,690 |\n", " |--------------------------------------------------------------------|\n", " | length | turn | displa~t | gear_r~o | foreign |\n", " | 212 | 43 | 250 | 2.56 | Domestic |\n", " +--------------------------------------------------------------------+\n", "\n", " +--------------------------------------------------------------------+\n", " 23. | make | price | mpg | rep78 | headroom | trunk | weight |\n", " | Dodge St. Regis | 6,342 | 17 | 2 | 4.5 | 21 | 3,740 |\n", " |--------------------------------------------------------------------|\n", " | length | turn | displa~t | gear_r~o | foreign |\n", " | 220 | 46 | 225 | 2.94 | Domestic |\n", " +--------------------------------------------------------------------+\n", "\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%head 5 if trunk >= 20" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " Source | SS df MS Number of obs = 74\n", "-------------+---------------------------------- F(3, 70) = 7.46\n", " Model | 153861671 3 51287223.7 Prob > F = 0.0002\n", " Residual | 481203725 70 6874338.93 R-squared = 0.2423\n", "-------------+---------------------------------- Adj R-squared = 0.2098\n", " Total | 635065396 73 8699525.97 Root MSE = 2621.9\n", "\n", "------------------------------------------------------------------------------\n", " price | Coefficient Std. err. t P>|t| [95% conf. interval]\n", "-------------+----------------------------------------------------------------\n", " mpg | -224.3597 65.27511 -3.44 0.001 -354.5468 -94.17263\n", " headroom | -659.463 484.5101 -1.36 0.178 -1625.788 306.8619\n", " trunk | 126.6049 107.2399 1.18 0.242 -87.27846 340.4882\n", " _cons | 11175.77 2431.134 4.60 0.000 6327.029 16024.52\n", "------------------------------------------------------------------------------\n" ] } ], "source": [ "reg price mpg headroom trunk" ] } ], "metadata": { "kernelspec": { "display_name": "Stata", "language": "stata", "name": "stata" }, "language_info": { "codemirror_mode": "stata", "file_extension": ".do", "mimetype": "text/x-stata", "name": "stata", "version": "15.1" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }