"I have a data file on which I'd like to carry out several regression analyses. I have four dependent variables, v1 through v4. The independent variables (v5 through v14) are the same for all analyses. How can I carry out these four analyses in an efficient way that would also work for 100 dependent variables?"
SPSS Python Syntax Example
*Run REGRESSION repeatedly over different dependent variables.
begin program.
import spss,spssaux
dependent = 'v1 to v4' # dependent variables.
spssSyntax = '' # empty Python string that we add SPSS REGRESSION commands to
depList = spssaux.VariableDict(caseless = True).expand(dependent) # create Python list of variable names
for dep in depList: # "+=" (below) concatenates SPSS REGRESSION commands to spssSyntax
spssSyntax += '''
REGRESSION
/MISSING PAIRWISE
/STATISTICS COEFF OUTS R ANOVA
/CRITERIA=PIN(.05) POUT(.10)
/NOORIGIN
/DEPENDENT %s
/METHOD=STEPWISE v5 to v14.
'''%dep # replace "%s" in syntax by by dependent var
print spssSyntax # prints REGRESSION commands to SPSS output window
end program.
*If REGRESSION commands look good, have SPSS run them.
begin program.
spss.Submit(spssSyntax)
end program.
begin program.
import spss,spssaux
dependent = 'v1 to v4' # dependent variables.
spssSyntax = '' # empty Python string that we add SPSS REGRESSION commands to
depList = spssaux.VariableDict(caseless = True).expand(dependent) # create Python list of variable names
for dep in depList: # "+=" (below) concatenates SPSS REGRESSION commands to spssSyntax
spssSyntax += '''
REGRESSION
/MISSING PAIRWISE
/STATISTICS COEFF OUTS R ANOVA
/CRITERIA=PIN(.05) POUT(.10)
/NOORIGIN
/DEPENDENT %s
/METHOD=STEPWISE v5 to v14.
'''%dep # replace "%s" in syntax by by dependent var
print spssSyntax # prints REGRESSION commands to SPSS output window
end program.
*If REGRESSION commands look good, have SPSS run them.
begin program.
spss.Submit(spssSyntax)
end program.
Description
- That this syntax uses Python so you need to have the SPSS Python Essentials installed in order to run it;
- The syntax will simply run a standard SPSS regression analysis analysis over different dependent variables one-by-one;
- Except for the occurrence of
%s
, Python will submit to SPSS a textbook example of regression syntax generated by the GUI. It can be modified as desired. - The TO and ALL keywords may be used for specifying the dependent and independent variables. The entire specification is enclosed in quotes.
- As a test file for this solution, you could use supermarket.sav.
THIS TUTORIAL HAS 22 COMMENTS:
By Ruben Geert van den Berg on July 26th, 2017
Hi Emma!
Try and replace
'''%dep
with
'''(%dep,%dep)
You used %dep twice so you need to specify two replacement stings after the last triple quotes, one for each... uh... %dep. Right?
By Emma on July 26th, 2017
Thank you again for getting back to me, Ruben. When I tried using '''(%dep,%dep) I got the following error:
SyntaxError: invalid syntax
Thanks again for helping me! Figuring this out would save me so many hours of time in the long run since I am running hundreds of regressions a day. THANK YOU!!
By Ruben Geert van den Berg on July 27th, 2017
Sorry, my bad! Try this
data list free/id.
begin data
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
end data.
do repeat #vars = a b c d e f g h i j k l.
compute #vars = rnd(rv.normal(100,15)).
end repeat.
execute.
begin program.
import spss,spssaux
spssSyntax = ''
depList = spssaux.VariableDict(caseless = True).expand('b to l') # create Python list of variable names
for dep in depList: # "+=" (below) concatenates SPSS REGRESSION commands to spssSyntax
spssSyntax += '''
TITLE %s
REGRESSION
/MISSING PAIRWISE
/STATISTICS COEFF COLLIN
/CRITERIA=PIN(.05) POUT(.10)
/NOORIGIN
/DEPENDENT %s
/METHOD=ENTER a.
'''%(dep,dep) # replace "%s" in syntax by dependent var
print spssSyntax
#spss.Submit(spssSyntax)
end program.
By Emma on July 27th, 2017
Thank you, very much! It worked when I wrote: TITLE "%s".
Thanks so much for your help. I really appreciate it!
If you figure out how to make a table of contents with the titles for when I export, please let me know.
Also, is there a way to create a significance summary table, where I have the first column with dependent variables, the second column with the corresponding independent variables, and the third column with the significance values (only if the values are < or = to 0.05)?
By Ruben Geert van den Berg on July 28th, 2017
Hi Emma!
For the summary table, look up SPSS OMS Tutorial - Creating Data from Output.
Hope that helps!