Hello,
I am trying to execute the code below and it is giving me an error, mainly because the Stata takes the command 'end' to end the python mode as a termination of the entire foreach loop:
gen Alpha = .
gen AUC = .
local i = 0
range alphas 0.0 1.0 20
foreach a in alphas {
i++
python:
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn import metrics # import scikit-learn metrics module for accuracy calculation
from sfi import Data
import numpy as np
import pandas as pd
a = Data.get("a")
# predict using the best value for alpha
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
Y_mnb_score = mnb.fit(X_train, np.ravel(Y_train)).predict_proba(X_test)[:, 1]
# make test_compliance python variable
test_compliance = Y_test['compliance']
# transfer the python variables Y_mnb_score and test_compliance to STATA
Data.setObsTotal(len(Y_mnb_score))
Data.addVarFloat('mnbScore')
Data.store(var = 'mnbScore', obs = None, val = Y_mnb_score)
Data.setObsTotal(len(test_compliance))
Data.addVarFloat('testCompliance')
Data.store(var = 'testCompliance', obs = None, val = test_compliance)
end // this 'end' is causing a problem
roctab testCompliance mnbScore)
replace AUC= r(area) in `i’)
replace Alpha = `a’)
} // loop not working
How can I use the Stata-python integration module inside of the foreach loop?
Thank you,
Related Posts with Is it possible to use the Stata-python integration module inside of foreach Stata loop?
Stata output InterpretationKindly assist with the interpretation of Table 2, Figure 1, Table 3 and Table 4 in the attached docu…
Stata output InterpretationKindly assist with the discussion and interpretation of Tables 2-4 and Figure 1 in the attached docu…
How to calculate odds ratio for C.DVHi everyone, How to calculate odds ratio from continuous DV (0 - 100) then transformation log …
Data spanning across different time period - how to convert it into common variables?Hi, I asked this question in an earlier thread but I guess the data was too confusing, so simplifyin…
Odds ratios with cmmprobit ?Dear all, I am wondering if there is a way to get odds ratios or risk ratios with cmmprobit in Stata…
Subscribe to:
Post Comments (Atom)
0 Response to Is it possible to use the Stata-python integration module inside of foreach Stata loop?
Post a Comment