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?
Bysort Qualifying PartsI’m trying to find which qualifying part was treated after the first visit for the 2nd and 3rd visit…
Maximum likelihood probit estimation (multiple outcomes)I am interested in how the characteristics of policy i and the characteristics of insurance j influe…
Assumptions SLRHi everyone, I ran a regress model, but when I checked for the assumptions, they were all violated.…
Omitted independent variables in xtabond2 using twostep system GMMHi, I am trying my data set in two-step System GMM, three variables annual GDP growth rate, Bond mar…
Postfile __000000 already existsHi I am running Stata 15.1 on a Windows 7 8 core desktop. I am trying to run postfile but keep gett…
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