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?
Dropping value label for a variableHi! I have a dataset in stata in which I want to string a numeric variable called bopael. When I tr…
Imposing Constraints on a Regression Model Using Stata.Hello Everyone, I intend estimating a regression model of the form: log(y) = ax1 + bx2 + cx3 + dx4.…
PPMLHDFE and LassoHi all, I'm looking to do some gravity modelling with a large number of rhs variables and interacti…
areg vs reghdfe vs xtreg with a group of controlling variables and fixed effectsHi Statalist, I want to run OLS regression on my dataset and I need advice on if I should choose ar…
gsem: a two-equation multilevel model with two random effectsDear all, I am running a two-equation multilevel model with random intercepts at the nation and reg…
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