Hello,
I am trying to run the code below, and my ultimate goal is to draw the scatter plot of the macros AUC vs. Alpha.
The AUC seem to be okay, but when I do ' list Alpha ' after running my code, the every value stored under the macro Alpha is 5, where as I want Alpha to be the list of same values used for `a1' (foreach a1 of numlist 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.5 5.0)
How can I resolve this issue? I am new to Stata so I have lots of questions.
Thank you,
STATA Code:
gen Alpha = .
gen AUC = .
local i = 1
foreach a1 of numlist 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.5 5.0 {
python: a = float(Macro.getLocal("a1"))
// predict using the best value for alpha
python: mnb = MultinomialNB(alpha = a, class_prior = None, fit_prior = True)
// calculate probability of each class on the test set
// '[:, 1]' at the end extracts the probability for each pharmacy to be under compliance
python: Y_mnb_score = mnb.fit(X_train, np.ravel(Y_train)).predict_proba(X_test)[:, 1]
// make test_compliance python variable
python: test_compliance = Y_test['compliance']
// transfer the python variables Y_mnb_score and test_compliance as global STATA variables
python: Data.setObsTotal(len(Y_mnb_score))
python: Data.addVarFloat('mnbScore')
python: Data.store(var = 'mnbScore', obs = None, val = Y_mnb_score)
python: Data.setObsTotal(len(test_compliance))
python: Data.addVarFloat('testCompliance')
python: Data.store(var = 'testCompliance', obs = None, val = test_compliance)
roctab testCompliance mnbScore
replace AUC = r(area) in `i'
replace Alpha = `a1' // this line is causing a problem
drop testCompliance mnbScore
local i = `++i'
}
scatter AUC Alpha xline(bestAlpha, lcolor(blue)) //ultimate goal
Related Posts with how to update the list of global macro within foreach loop?
Automatic average calculationHey there I have another question. Here as example for the Grunfeld database: I need a code that cal…
Standard errors ivqte vs qreg2Maybe more of an Econometrics question, but I was wondering why the standard errors between qreg2 an…
Problem with date formatI split a variable which had date and time together to one with just the dates. Now I am trying to c…
categorical variable sample sizesHi, I am running a regression using the following code: Code: reghdfe incvote i.X controls, absorb…
directions of endogenous coefficients are the opposite in simultaneous equation modelHi everyone, I'm running simultaneous equation model (2 equations) but getting a weird result. My a…
Subscribe to:
Post Comments (Atom)
0 Response to how to update the list of global macro within foreach loop?
Post a Comment