Dear all,
I just had access to the Stata 16.0 version and I am really excited because of it. It's just because since I read the post made by Dr. Chuck Huber regarding three-dimensional surface plots of marginal predictions (https://blog.stata.com/2020/09/14/st...l-predictions/) I am waiting for using that information. I tried to use the commands suggested by Dr. Chuck Huber, but unfortunately, it didn't work. As recommended the first step after getting access to the Stata 16 version, I downloaded the Python program. In the next step, a just tried to use the example.do describe by Dr. Chuck Huber, but after running a few lines, it stopped.
The command suggested by Dr. Chuck Huber is the follows:
// Fit the model and estimate the marginal predicted probabilities with Stata
webuse nhanes2, clear
logistic highbp c.age##c.weight
quietly margins, at(age=(20(5)80) weight=(40(5)180)) ///
saving(predictions, replace)
use predictions, clear
rename _at1 age
rename _at2 weight
rename _margin pr_highbp
save predictions, replace
// Create the three-dimensional surface plot with Python
python:
# Import the necessary Python packages
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')
# Read (import) the Stata dataset “predictions.dta”
# into a pandas data frame named “data”
data = pd.read_stata(“predictions.dta”)
# Define a 3-D graph named “ax”
ax = plt.axes(projection=’3d’)
# Render the graph
ax.plot_trisurf(data[‘age’], data[‘weight’], data[‘pr_highbp’],
cmap=plt.cm.Spectral_r)
# Specify the axis ticks
ax.set_xticks(np.arange(20, 90, step=10))
ax.set_yticks(np.arange(40, 200, step=40))
ax.set_zticks(np.arange( 0, 1.2, step=0.2))
# Specify the title and axis titles
ax.set_title(“Probability of Hypertension by Age and Weight”)
ax.set_xlabel(“Age (years)”)
ax.set_ylabel(“Weight (kg)”)
ax.zaxis.set_rotate_label(False)
ax.set_zlabel(“Probability of Hypertension”, rotation=90)
# Specify the view angle of the graph
ax.view_init(elev=30, azim=240)
# Save the graph
plt.savefig("Margins3d.png", dpi=1200)
end
But unfortunately, after this first part of the commands, it stops.
// Fit the model and estimate the marginal predicted probabilities with Stata
webuse nhanes2, clear
logistic highbp c.age##c.weight
quietly margins, at(age=(20(5)80) weight=(40(5)180)) ///
saving(predictions, replace)
use predictions, clear
rename _at1 age
rename _at2 weight
rename _margin pr_highbp
save predictions, replace
. // Create the three-dimensional surface plot with Python
. python:
----------------------------------------------- python (type end to exit) -------------------------------------------------------
>>> # Import the necessary Python packages
... import numpy as np
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
(41 lines skipped)
---------------------------------------------------------------------------------------------------------------------------------
r(7102);
end of do-file
Can anyone help me to solve this issue?
Thank you in advance!
Related Posts with Three-dimensional surface plots of marginal predictions using Python
GLM and SQREG heteroscedasticity analysisHi all, I'm trying to analyse the heteroscedasticity posteestimation of my two possible models regre…
Calculating the share of respondents with certain preferences in Discrete Choice Experiments -mixed logitDear Stata users, I have been trying to replicate the following paper from Arne Risa Hole (2007) : …
Reshape wide to long over various attributesI have a dataframe that looks like so: Q1_2008_East Q1_2008_West Q2_2008_East Q2_2008_West .....…
Regression models with exposure, outcome, and GROUP of covariates in one loopHi All - just struggling to get some basic regression code to work how I want it in Stata. Imagine i…
mediation analysis using -ldecomp- commandHello Stata users, I am trying to perform the mediation analysis. Combined indirect effects of medi…
Subscribe to:
Post Comments (Atom)
0 Response to Three-dimensional surface plots of marginal predictions using Python
Post a Comment