0

Updated question in Edit II

I am running a regression model. For that model, I want to test a large set of values in one specific part of the function and then run the model and all following functions.

E.g.

alpha = ['1', '2', '3']

for a in alpha:
    oddsvar = 'odds_all' + a
    result = 'result_all' + a
    norm_odds = 'norm_odds' + a
    pos_norm_odds = 'pos_norm_odds' + a
    
    oddsvar = mba263.odds_ratios(result)['Odds ratios']
    norm_odds = numpy.power(oddsvar, std_all)
    pos_norm_odds = norm_odds
    pos_norm_odds[pos_norm_odds<1]=1/pos_norm_odds[pos_norm_odds<1]
    pos_norm_odds.sort_values(ascending=False)
    print(pos_norm_odds)

When I first define oddsvar (and the other variables) they just contain a string. I want that string to be used in the rest of the function to either be a variable name or pull data from a different part of my code.

For alpha = 1 the handwritten code would look like this:

odds_all1 = mba263.odds_ratios(result_all1)['Odds ratios']
norm_odds_all1 = numpy.power(odds_all1, std_all)
pos_norm_odds_all1=norm_odds_all1
pos_norm_odds_all1[pos_norm_odds_all1<1]=1/pos_norm_odds_all1[pos_norm_odds_all1<1]
pos_norm_odds_all1.sort_values(ascending=False)

I have been trying (and searching) but so far couldn't find a solution that works for me.

Edit:

Rewording my question: I am trying to define a name in the first half by making

oddsvar = 'odds_all' + a -->> result should be e.g. odds_all1

Then I want to use the new name "odds_all1" and have it be the result of the equation on the right. What I can't figure out in the code is, how to have

oddsvar = mba263.odds_ratios(result)['Odds ratios']
... translated to ...
odds_all1 = mba263.odds_ratios(result_all1)['Odds ratios']

So that after the function has run, I can call the variable "odds_all1" and it gives me the whole list of odds for my logistic regression with alpha = 1.

I see that I can solve the first half with a dictionary (thanks @Teh), however, that approach doesn't seem to work for the argument within the function.

Edit II:

alpha= ['1', '2', '3']
for a in alpha:
    d = {} #odds_all dict
    e = {a: 'result_all' + a} #result_all dict
    f = {} #norm_odds dict
    g = {} #pos_norm_odds dict
       
    d['odds_all' + a] = mba263.odds_ratios(e[a])['Odds ratios']
    f['norm_odds' + a] = numpy.power(d['odds_all' + a], std_all)
    g['pos_norm_odds' + a] = e['norm_odds' + a]
    g['pos_norm_odds' + a][g['pos_norm_odds' + a]<1]=1/g['pos_norm_odds' + a][g['pos_norm_odds' + a]<1]
    g['pos_norm_odds' + a].sort_values(ascending=False)
    print(g)

Thank you for the help so far. With the dict approach I was able to solve the left side of the equation.

However, the "mba263.odds_ratios(e[a])" still yields an error (AttributeError: 'str' object has no attribute 'params'). I have "result_all1" defined as a variable name elsewhere, but combining result_all and the alpha can only be done as a string.

Can I switch it back from being a string to being a name within the dict?

Djann
  • 1
  • 1
  • What are you trying to achieve by this? I am pretty sure this it isn't possible – Teh Apr 10 '21 at 04:40
  • 1
    Does this answer your question? [Changing variable names with Python for loops](https://stackoverflow.com/questions/1060090/changing-variable-names-with-python-for-loops) – Teh Apr 10 '21 at 04:48
  • @Teh I have a long list of alpha values to test (about 50 or so) and would like to just write the code once, then run it with all the alpha values so that I can compare the outputs. – Djann Apr 10 '21 at 17:59
  • You need to provide an [MRE-minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) also this is a totally different question now and because I don't complete understand your code or what exactly you want to do, I can't help much but I will say that e just has a single entry have you checked how dictionaries work? – Teh Apr 10 '21 at 21:59

1 Answers1

0

This is just a guess on my part, but I found the exec() to run a command, and you might look that up. You could probably build a string with "global {}" or set it to a throw away value and fill in your built name, and then run the string with exec([stringname]).

Here's just a test I ran from the command prompt in python.

myval = "test1"
mystr = "global " + myval
exec(mystr)
print(mystr)
global test1
print(type("test1"))
<class 'str'>

Hope this helps you. Good luck with your program!

Did you try something like this after your first setting lines?

temp_str = "global " + oddsvar
exec(temp_str)

Again, I'm just guessing. I hope you can get this to work like you need. For the other set of values, you might try building a string, and then using the exec on that. Something like this

temp_str = oddsvar + "mba263.odds_ratios(result)['Odds ratios']"
exec(temp_str)
p wilson
  • 75
  • 7
  • Thanks for the response. I tried to play around with your suggestion but I couldn't figure out how to make it work in my example above. I will detail my problem a bit further in my post. – Djann Apr 10 '21 at 18:05