Use jupyter notebook online how to#
We have demonstrated how to access Stata from Python, but you can also access Mata from Python using the mata magic see The mata magic and Example 4: Work with Mata. # set ticks and labels for x, y, and z axesĪx.set_xticks(np.arange(20, 90, step=10))Īx.set_yticks(np.arange(40, 200, step=40))Īx.set_zticks(np.arange( 0, 1.2, step=0.2))Īx.set_zlabel( "Probability of Hypertension") Lastly we store the dataset in Stata as a pandas dataframe named preddata in Python by specifying the -doutd argument.Īx.plot_trisurf(preddata, preddata, preddata,cmap=plt.cm.Spectral_r) We rename those variables to age, weight, and pr_highbp respectively. The dataset also contains the variable _margin, which is the marginal prediction of the probability of high blood pressure. The dataset predictions.dta contains the variables _at1 and _at2, which correspond to the values of age and weight that we specified in the at() option. Our goal is to use Python to create a three-dimensional surface plot of those predictions.
The option saving(predictions, replace) saves the predictions to a dataset named predictions.dta. We use margins to estimate the predicted probability of hypertension for all combinations of age and weight for values of age ranging from 20 to 80 years in increments of 5 and for values of weight ranging from 40 to 180 kilograms in increments of 5.
Variables that uniquely identify margins: age We can access e(b) and e(V) by typing myeret and myeret, respectively, in Python. 0004335Į(cmdline) : "logistic highbp c.age#c.weight"Į(marginsnotok) : "stdp DBeta DEviance DX2 DDeviance Hat Number Resi." Logistic regression Number of obs = 10,351 We also push Stata's estimation results displayed by ereturn list, including the coefficient vector e(b) and variance–covariance matrix e(V), into a Python dictionary called myeret by specifying the -eret argument. We load the dataframe into Stata by specifying the -d argument of the %%stata magic, and then within Stata, we fit a logistic regression model using age, weight, and their interaction as predictors of the probability of hypertension. The Stata output is displayed underneath the cell. The following commands load the auto dataset and summarize the mpg variable. In a notebook cell, we put Stata commands underneath the %%stata cell magic to direct the cell to call Stata. The stata magic is used to execute Stata commands in an IPython environment. If you get output similar to what is shown above for your edition of Stata, it means that everything is configured properly see Configuration for more ways to configure pystata. Maximum number of variables is set to 5,000 see help set_maxvar. More than 2 billion observations are allowed see help obs_advice.ģ. Unicode is supported see help unicode_advice.Ģ.
Statistics and Data Science Copyright 1985-2021 StataCorp LLCĩ7 license: 10-user 4-core network perpetualġ. Stata_nfig( "C:/Program Files/Stata17", "mp") Suppose you have Stata installed in C:\Program Files\Stata17\ and you use the Stata/MP edition.