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?
Difference between clustered standard errors produced by "reghdfe" and "xtreg, fe"In my model, I regress wages by country-occupation on explanatory variables and country-occupation f…
Combining wordsHi Everyone, I have a simple question that the answer could be to do it on excel, but I am wonderin…
Generation of a new variable according to timeHello everyone, I have a time variable which is given as a difference. So if there is a starting d…
correct modeling of fixed effect regression with stata (how to cluster?)Dear forum users, I’m currently looking for some wisdom concerning the fixed modeling of my master …
Removing y-axis line and ticks in schemeI am trying to create a graph scheme for line graphs (-tsline-) that has no y-axis line and ticks, b…
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