SPSS-Python Regression Loop

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

SPSS-Python Regression Loop

Matthew-33
I need to iteratively run a regression analysis against a constant dependent
variable (DV) until all of the independent variables (IV) have beta weights
with a positive value. I am trying to set up a looping structure that will
run the regression, dropping the IV's with negative values, and then run the
regression again with the IV's that contain positive values. I must do this
until all IVs have positive values. I have upwards of a hundred IV’s -I know
collinearity is a problem- but for now I am just trying to deal with the
programming task. My attempt at this has involved using SPSS v14 with the
new Python module to retrieve the beta values from the XML output. However,
I cannot get the looping structure correct in Python. I can manage to loop
through a list I've created, but I cannot force the loop to continue
iterating until all beta weights are positive. Is using Python the right
approach? Is there a better way outside of Python to attack this problem? I
am very grateful for any assistance offered. I've included a sample of the
current syntax I am using. Thank You.

*Retrieve procedure output.
BEGIN PROGRAM.
import spss, spssaux
regressionfiltered= "REGRESSION / DEPENDENT dv /METHOD=ENTER %s."
iv = ["iv_1", "iv_2", "iv_3", "iv_4", "iv_5", "iv_6", "iv_7", "iv_8",
"iv_9", "iv,10"]
spss.Submit(regression % " ".join(iv))
reg_table,errcode=spssaux.CreateXMLOutput(
regression % " ".join(iv),
omsid="Regression")
beta_value=spssaux.GetValuesFromXMLWorkspace(
reg_table,
tableSubtype="Coefficients",
colCategory="Beta",
cellAttrib="number")
print "The Beta is: ", beta_value
negative = []
for i, b in enumerate(beta_value):
if float(b) < 0: negative.append(i)
print negative
for i in reversed(negative):
iv.pop(i)
print iv
spss.Submit(regression % " ".join(iv))
END PROGRAM.
Reply | Threaded
Open this post in threaded view
|

Re: SPSS-Python Regression Loop

Peck, Jon
I need to iteratively run a regression analysis against a constant dependent
variable (DV) until all of the independent variables (IV) have beta weights
with a positive value. I am trying to set up a looping structure that will
run the regression, dropping the IV's with negative values, and then run the
regression again with the IV's that contain positive values. I must do this
until all IVs have positive values. I have upwards of a hundred IV's -I know
collinearity is a problem- but for now I am just trying to deal with the
programming task. My attempt at this has involved using SPSS v14 with the
new Python module to retrieve the beta values from the XML output. However,
I cannot get the looping structure correct in Python. I can manage to loop
through a list I've created, but I cannot force the loop to continue
iterating until all beta weights are positive. Is using Python the right
approach? Is there a better way outside of Python to attack this problem? I
am very grateful for any assistance offered. I've included a sample of the
current syntax I am using. Thank You.

*Retrieve procedure output.
BEGIN PROGRAM.
import spss, spssaux
regressionfiltered= "REGRESSION / DEPENDENT dv /METHOD=ENTER %s."
iv = ["iv_1", "iv_2", "iv_3", "iv_4", "iv_5", "iv_6", "iv_7", "iv_8",
"iv_9", "iv,10"]
spss.Submit(regression % " ".join(iv))
reg_table,errcode=spssaux.CreateXMLOutput(
regression % " ".join(iv),
omsid="Regression")
beta_value=spssaux.GetValuesFromXMLWorkspace(
reg_table,
tableSubtype="Coefficients",
colCategory="Beta",
cellAttrib="number")
print "The Beta is: ", beta_value
negative = []
for i, b in enumerate(beta_value):
if float(b) < 0: negative.append(i)
print negative
for i in reversed(negative):
iv.pop(i)
print iv
spss.Submit(regression % " ".join(iv))
END PROGRAM.