Python - Macro

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

Python - Macro

drfg2008
SPSS 17 / Vista / XP

Below you see (parts of) a syntax written in Macro which I would like to 'convert' into Python. As you see, the problem is, that the macro needs a lot of repetition. (It's a bit unskilfully programmed even with macro). Before trying to develop a better macro, I wanted to use python instead. I have been trying several python programming attempts (I better do not to show here), however it never worked.

The actual purpose of the macro is to check the quality of data imputation -with mean or regression- and check imputated data against not imputated data: beta coefficient, standard error. However, I would also be interested how to repeat whole parts of syntax programs with python (here with !imp p = ...)

I'm already a bit familiar with basic python.


Thanks for a little help

Frank

DEFINE !imp (p=!tokens(1)).

FILE HANDLE reg1 /NAME =!QUOTE(!CONCAT('C:\<path>\regression1_',!p,'.sav')).
FILE HANDLE reg2 /NAME =!QUOTE(!CONCAT('C:\<path>\regression2_',!p,'.sav')).

input program.
loop a =1 to 10**5 by 1.
end case.
end loop.
end file.
end input program.
EXECUTE.

COMPUTE uv1=RV.NORMAL(100,10).
COMPUTE !concat("missing=RV.BERNOULLI(0.",!p,")").

COMPUTE av=uv1 + RV.NORMAL(0,10).
COMPUTE uv_missing=uv1 * missing.
EXECUTE.

MISSING VALUES uv_missing (0).
*RMV /uv_missing_1=SMEAN(uv_missing).
RMV /uv_missing_1=LINT(uv_missing).


FORMATS uv1 (f8.4).
FORMATS uv_missing_1 (f8.4).
FORMATS av (f8.4).

DELETE VARIABLES  a uv_missing .

OMS /DESTINATION VIEWER=NO /TAG='suppressall'.

oms select tables
       /destination format = sav
       outfile = reg1
       /if commands =['Regression']
        subtype = ['Coefficients'].

REGRESSION
  /MISSING LISTWISE
  /STATISTICS COEFF OUTS R ANOVA
  /CRITERIA=PIN(.05) POUT(.10)
  /NOORIGIN
  /DEPENDENT av
  /METHOD=ENTER uv1.

OMSEND.

OMS /DESTINATION VIEWER=NO /TAG='suppressall'.
oms select tables
       /destination format = sav
       outfile = reg2
       /if commands =['Regression']
        subtype = ['Coefficients'].

REGRESSION
  /MISSING LISTWISE
  /STATISTICS COEFF OUTS R ANOVA
  /CRITERIA=PIN(.05) POUT(.10)
  /NOORIGIN
  /DEPENDENT av
  /METHOD=ENTER uv_missing_1.

OMSEND .

!enddefine.


!imp p =01.
!imp p =02.
!imp p =03.
!imp p =04.
!imp p =05.
!imp p =06.
!imp p =07.
!imp p =08.
!imp p =09.
!imp p =10.
!imp p =11.
!imp p =12.
!imp p =13.
!imp p =14.
!imp p =15.
!imp p =16.
!imp p =17.
!imp p =18.
!imp p =19.
!imp p =20.
!imp p =21.
!imp p =22.
!imp p =23.
!imp p =24.
!imp p =25.
!imp p =26.
!imp p =27.
!imp p =28.
!imp p =29.
!imp p =30.
!imp p =31.
!imp p =32.
!imp p =33.
!imp p =34.
!imp p =35.
!imp p =36.
!imp p =37.
!imp p =38.
!imp p =39.
!imp p =40.
!imp p =41.
!imp p =42.
!imp p =43.
!imp p =44.
!imp p =45.
!imp p =46.
!imp p =47.
!imp p =48.
!imp p =49.
!imp p =50.
!imp p =51.
!imp p =52.
!imp p =53.
!imp p =54.
!imp p =55.
!imp p =56.
!imp p =57.
!imp p =58.
!imp p =59.
!imp p =60.
!imp p =61.
!imp p =62.
!imp p =63.
!imp p =64.
!imp p =65.
!imp p =66.
!imp p =67.
!imp p =68.
!imp p =69.
!imp p =70.
!imp p =71.
!imp p =72.
!imp p =73.
!imp p =74.
!imp p =75.
!imp p =76.
!imp p =77.
!imp p =78.
!imp p =79.
!imp p =80.
!imp p =81.
!imp p =82.
!imp p =83.
!imp p =84.
!imp p =85.
!imp p =86.
!imp p =87.
!imp p =88.
!imp p =89.
!imp p =90.
!imp p =91.
!imp p =92.
!imp p =93.
!imp p =94.
!imp p =95.
!imp p =96.
!imp p =97.
!imp p =98.
!imp p =99.
Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Automatic reply: Python - Macro

Sarraf, Shimon Aaron

I will be out of the office until Thursday,  May 26. If you need immediate assistance, please call 812-856-5824. I will respond to your e-mail as soon as possible.

 

Thank you,

Shimon Sarraf

Center for Postsecondary Research

Indiana University at Bloomington

 

Reply | Threaded
Open this post in threaded view
|

Re: Python - Macro

Jon K Peck
In reply to this post by drfg2008
A few general comments first.

You don't need to keep starting and stopping OMS.  Let the first one stay in effect - on your OMSEND command you can specify a tag.
You could just let the second one accumulate a dataset - there will be a number for the iteration in the output file.

For the iterative part of the code, you can do something like this with Python

for p in xrange(.01, .99, .01):
   spss.Submit("""block of code %(p)s more code""" % locals())

where the parameter is where the !p appears in the macro.

HTH,

Jon Peck
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        drfg2008 <[hidden email]>
To:        [hidden email]
Date:        05/23/2011 03:23 AM
Subject:        [SPSSX-L] Python - Macro
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




SPSS 17 / Vista / XP

Below you see (parts of) a syntax written in Macro which I would like to
'convert' into Python. As you see, the problem is, that the macro needs a
lot of repetition. (It's a bit unskilfully programmed even with macro).
Before trying to develop a better macro, I wanted to use python instead. I
have been trying several python programming attempts (I better do not to
show here), however it never worked.

The actual purpose of the macro is to check the quality of data imputation
-with mean or regression- and check imputated data against not imputated
data: beta coefficient, standard error. However, I would also be interested
how to repeat whole parts of syntax programs with python (here with !imp p =
...)

I'm already a bit familiar with basic python.


Thanks for a little help

Frank

DEFINE !imp (p=!tokens(1)).

FILE HANDLE reg1 /NAME =!QUOTE(!CONCAT('C:\<path>\regression1_',!p,'.sav')).
FILE HANDLE reg2 /NAME =!QUOTE(!CONCAT('C:\<path>\regression2_',!p,'.sav')).

input program.
loop a =1 to 10**5 by 1.
end case.
end loop.
end file.
end input program.
EXECUTE.

COMPUTE uv1=RV.NORMAL(100,10).
COMPUTE !concat("missing=RV.BERNOULLI(0.",!p,")").

COMPUTE av=uv1 + RV.NORMAL(0,10).
COMPUTE uv_missing=uv1 * missing.
EXECUTE.

MISSING VALUES uv_missing (0).
*RMV /uv_missing_1=SMEAN(uv_missing).
RMV /uv_missing_1=LINT(uv_missing).


FORMATS uv1 (f8.4).
FORMATS uv_missing_1 (f8.4).
FORMATS av (f8.4).

DELETE VARIABLES  a uv_missing .

OMS /DESTINATION VIEWER=NO /TAG='suppressall'.

oms select tables
      /destination format = sav
      outfile = reg1
      /if commands =['Regression']
       subtype = ['Coefficients'].

REGRESSION
 /MISSING LISTWISE
 /STATISTICS COEFF OUTS R ANOVA
 /CRITERIA=PIN(.05) POUT(.10)
 /NOORIGIN
 /DEPENDENT av
 /METHOD=ENTER uv1.

OMSEND.

OMS /DESTINATION VIEWER=NO /TAG='suppressall'.
oms select tables
      /destination format = sav
      outfile = reg2
      /if commands =['Regression']
       subtype = ['Coefficients'].

REGRESSION
 /MISSING LISTWISE
 /STATISTICS COEFF OUTS R ANOVA
 /CRITERIA=PIN(.05) POUT(.10)
 /NOORIGIN
 /DEPENDENT av
 /METHOD=ENTER uv_missing_1.

OMSEND .

!enddefine.


!imp p =01.
!imp p =02.
!imp p =03.
!imp p =04.
!imp p =05.
!imp p =06.
!imp p =07.
!imp p =08.
!imp p =09.
!imp p =10.
!imp p =11.
!imp p =12.
!imp p =13.
!imp p =14.
!imp p =15.
!imp p =16.
!imp p =17.
!imp p =18.
!imp p =19.
!imp p =20.
!imp p =21.
!imp p =22.
!imp p =23.
!imp p =24.
!imp p =25.
!imp p =26.
!imp p =27.
!imp p =28.
!imp p =29.
!imp p =30.
!imp p =31.
!imp p =32.
!imp p =33.
!imp p =34.
!imp p =35.
!imp p =36.
!imp p =37.
!imp p =38.
!imp p =39.
!imp p =40.
!imp p =41.
!imp p =42.
!imp p =43.
!imp p =44.
!imp p =45.
!imp p =46.
!imp p =47.
!imp p =48.
!imp p =49.
!imp p =50.
!imp p =51.
!imp p =52.
!imp p =53.
!imp p =54.
!imp p =55.
!imp p =56.
!imp p =57.
!imp p =58.
!imp p =59.
!imp p =60.
!imp p =61.
!imp p =62.
!imp p =63.
!imp p =64.
!imp p =65.
!imp p =66.
!imp p =67.
!imp p =68.
!imp p =69.
!imp p =70.
!imp p =71.
!imp p =72.
!imp p =73.
!imp p =74.
!imp p =75.
!imp p =76.
!imp p =77.
!imp p =78.
!imp p =79.
!imp p =80.
!imp p =81.
!imp p =82.
!imp p =83.
!imp p =84.
!imp p =85.
!imp p =86.
!imp p =87.
!imp p =88.
!imp p =89.
!imp p =90.
!imp p =91.
!imp p =92.
!imp p =93.
!imp p =94.
!imp p =95.
!imp p =96.
!imp p =97.
!imp p =98.
!imp p =99.


-----
Dr. Frank Gaeth
FU-Berlin

--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Python-Macro-tp4418491p4418491.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

Reply | Threaded
Open this post in threaded view
|

Re: Python - Macro

drfg2008
Thanks Jon,

I tried the "for p in xrange(.01, .99, .01): " version but failed many times. Then I checked SPSS websites and found plenty of programs, however not the appropriate. Then I tried the following simple version with "while". However, it does not do anything (at least no errors) although it should work. Any help possible?



*..........................Test 2 - not working- .......................................*

OMS /DESTINATION VIEWER=NO /TAG='suppressall'.

BEGIN PROGRAM.
import spss
i = 1
while i < 10:
         spss.Submit(["FILE HANDLE reg1 /NAME ='C:\user\regression1_0" + i + ".sav'."])

         spss.Submit("""
input program.
loop a =1 to 10**5 by 1.
end case.
end loop.
end file.
end input program.
EXECUTE.

COMPUTE uv1=RV.NORMAL(100,10).
                               """)


spss.Submit(["COMPUTE missing=RV.BERNOULLI(0." + i +")."])

spss.Submit("""

COMPUTE av=uv1 + RV.NORMAL(0,10).
COMPUTE uv_missing=uv1 * missing.
EXECUTE.

MISSING VALUES uv_missing (0).
RMV /uv_missing_1=SMEAN(uv_missing).
*RMV /uv_missing_1=LINT(uv_missing).

FORMATS uv1 (f8.4).
FORMATS uv_missing_1 (f8.4).
FORMATS av (f8.4).

DELETE VARIABLES  a uv_missing .

oms select tables
       /destination format = sav
       outfile = reg1
       /if commands =['Regression']
        subtype = ['Coefficients'].

REGRESSION
  /MISSING LISTWISE
  /STATISTICS COEFF OUTS R ANOVA
  /CRITERIA=PIN(.05) POUT(.10)
  /NOORIGIN
  /DEPENDENT av
  /METHOD=ENTER uv1.


 """)
         i+=1
END PROGRAM.

OMSEND.
Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: Python - Macro

Albert-Jan Roskam
Hi,

for p in range(1, 100):
     i = float(p)/100
     spss.Submit(cmd % i)
 
Your code becomes clearer is you define cmd first. You could use SET MPRINT = ON to see what is happening.

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



From: drfg2008 <[hidden email]>
To: [hidden email]
Sent: Fri, May 27, 2011 11:13:31 AM
Subject: Re: [SPSSX-L] Python - Macro

Thanks Jon,

I tried the "for p in xrange(.01, .99, .01): " version but failed many
times. Then I checked SPSS websites and found plenty of programs, however
not the appropriate. Then I tried the following simple version with "while".
However, it does not do anything (at least no errors) although it should
work. Any help possible?



*..........................Test 2 - not working-
.......................................*

OMS /DESTINATION VIEWER=NO /TAG='suppressall'.

BEGIN PROGRAM.
import spss
i = 1
while i < 10:
        spss.Submit(["FILE HANDLE reg1 /NAME ='C:\user\regression1_0" + i +
".sav'."])

        spss.Submit("""
input program.
loop a =1 to 10**5 by 1.
end case.
end loop.
end file.
end input program.
EXECUTE.

COMPUTE uv1=RV.NORMAL(100,10).
                              """)


spss.Submit(["COMPUTE missing=RV.BERNOULLI(0." + i +")."])

spss.Submit("""

COMPUTE av=uv1 + RV.NORMAL(0,10).
COMPUTE uv_missing=uv1 * missing.
EXECUTE.

MISSING VALUES uv_missing (0).
RMV /uv_missing_1=SMEAN(uv_missing).
*RMV /uv_missing_1=LINT(uv_missing).

FORMATS uv1 (f8.4).
FORMATS uv_missing_1 (f8.4).
FORMATS av (f8.4).

DELETE VARIABLES  a uv_missing .

oms select tables
      /destination format = sav
      outfile = reg1
      /if commands =['Regression']
        subtype = ['Coefficients'].

REGRESSION
  /MISSING LISTWISE
  /STATISTICS COEFF OUTS R ANOVA
  /CRITERIA=PIN(.05) POUT(.10)
  /NOORIGIN
  /DEPENDENT av
  /METHOD=ENTER uv1.


""")
        i+=1
END PROGRAM.

OMSEND.

-----
Dr. Frank Gaeth
FU-Berlin

--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Python-Macro-tp4418491p4431312.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Python - Macro

Jon K Peck
In reply to this post by drfg2008
Sorry, I spaced out on xrange.  Use range with integers and multiply.  E.g.,
for i in range(1,101):
        amt = i*.01


Jon Peck
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        drfg2008 <[hidden email]>
To:        [hidden email]
Date:        05/27/2011 03:16 AM
Subject:        Re: [SPSSX-L] Python - Macro
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Thanks Jon,

I tried the "for p in xrange(.01, .99, .01): " version but failed many
times. Then I checked SPSS websites and found plenty of programs, however
not the appropriate. Then I tried the following simple version with "while".
However, it does not do anything (at least no errors) although it should
work. Any help possible?



*..........................Test 2 - not working-
.......................................*

OMS /DESTINATION VIEWER=NO /TAG='suppressall'.

BEGIN PROGRAM.
import spss
i = 1
while i < 10:
        spss.Submit(["FILE HANDLE reg1 /NAME ='C:\user\regression1_0" + i +
".sav'."])

        spss.Submit("""
input program.
loop a =1 to 10**5 by 1.
end case.
end loop.
end file.
end input program.
EXECUTE.

COMPUTE uv1=RV.NORMAL(100,10).
                              """)


spss.Submit(["COMPUTE missing=RV.BERNOULLI(0." + i +")."])

spss.Submit("""

COMPUTE av=uv1 + RV.NORMAL(0,10).
COMPUTE uv_missing=uv1 * missing.
EXECUTE.

MISSING VALUES uv_missing (0).
RMV /uv_missing_1=SMEAN(uv_missing).
*RMV /uv_missing_1=LINT(uv_missing).

FORMATS uv1 (f8.4).
FORMATS uv_missing_1 (f8.4).
FORMATS av (f8.4).

DELETE VARIABLES  a uv_missing .

oms select tables
      /destination format = sav
      outfile = reg1
      /if commands =['Regression']
       subtype = ['Coefficients'].

REGRESSION
 /MISSING LISTWISE
 /STATISTICS COEFF OUTS R ANOVA
 /CRITERIA=PIN(.05) POUT(.10)
 /NOORIGIN
 /DEPENDENT av
 /METHOD=ENTER uv1.


""")
        i+=1
END PROGRAM.

OMSEND.

-----
Dr. Frank Gaeth
FU-Berlin

--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Python-Macro-tp4418491p4431312.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

Reply | Threaded
Open this post in threaded view
|

Re: Python - Macro

David Marso
Administrator
In reply to this post by drfg2008
This is an  poorly designed incredibly inefficient approach to the problem!
You are generating 100,000 cases 100 times and running 2 regressions each time.
Why not generate ALL 100 imputations at 1 time and then generate a single correlation matrix and compute your regression coefficients from this matrix using stats 101 formulas?  Also don't need OMS in this case either.  Sometimes I tend to think all this new fangled crap (OMS, Python etc ) simply causes people to shut off their brains and forget to analyze the true nature of a task and the resulting processing requirements.
BTW:
In a MACRO you can do
DO !X=1 !TO 100 .
macro body...
!DOEND .
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Python - Macro

David Marso
Administrator
Another though occurred to me.  Why not generate everything in a single pass and utilize SPLIT FILE.
ie add an outer loop to control the missing probability...
 iteration.

INPUT PROGRAM.
LOOP P=1 TO 100.
LOOP #=1 TO 100000.
COMPUTE AV=NORMAL(100,10).
COMPUTE #P=1/P.
COMPUTE M=RV.BERNOULI(#P).
etc......
END CASE.
END LOOP.
END LOOP.
END FILE.
END INPUT PROGRAM.
*** KEY IDEA ***.
SPLIT FILE BY P.
RMV ...
CORR ...
or REGRESSION and use SAVE to extract the parameters.


Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Python - Macro

drfg2008
In reply to this post by David Marso
Thanks David,

I just tried to improve my knowledge of this new fangled crap (OMS, Python etc ). Just in case I need it one time.

You’re absolutely right, for that special purpose there would have been a much better solution with and even without macro. Coming from a php background I first have to get used to python's logic: No backslashes here, re-defining variables (which is obsolete in php).

Now this is a pretty simple solution (still generating huge samples each time)

Thanks everybody.




OMS /DESTINATION VIEWER=NO /TAG='suppressall'.
 
BEGIN PROGRAM.
import spss
i = 1
j = 100
filenumber = str(i)  +"_" + str(j)
percent = float(i)
percent=percent/100
percent=str(percent)

while i < j:
         
         spss.Submit("""
input program.
loop a =1 to 10**5 by 1.
end case.
end loop.
end file.
end input program.
EXECUTE.

COMPUTE uv1=RV.NORMAL(100,10).
COMPUTE missing=RV.BERNOULLI(""" +  percent + """).
COMPUTE av=uv1 + RV.NORMAL(0,10).
COMPUTE uv_missing=uv1 * missing.
EXECUTE.

MISSING VALUES uv_missing (0).
RMV /uv_missing_1=SMEAN(uv_missing).
*RMV /uv_missing_1=LINT(uv_missing).

*FORMATS uv1 (f8.4).
*FORMATS uv_missing_1 (f8.4).
*FORMATS av (f8.4).

*DELETE VARIABLES  a uv_missing .

oms select tables
       /destination format = sav
       outfile = 'C:/user/regression_""" + filenumber +""".sav'
       /if commands =['Regression']
        subtype = ['Coefficients'].

REGRESSION
  /MISSING LISTWISE
  /STATISTICS COEFF OUTS R ANOVA
  /CRITERIA=PIN(.05) POUT(.10)
  /NOORIGIN
  /DEPENDENT av
  /METHOD=ENTER uv1.

 """)
         i+=1
END PROGRAM.

OMSEND.
Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: Python - Macro

drfg2008
*..........................Test 3 - working- .......................................*

OMS /DESTINATION VIEWER=NO /TAG='suppressall'.
 
BEGIN PROGRAM.
import spss
i = 1
j = 25
filenumber = str(i)+"_" +str(j)

while i < j:

         percent = float(i)
         percent=percent/100
         percent=str(percent)
         
         spss.Submit("""
input program.
loop a =1 to 10**5 by 1.
end case.
end loop.
end file.
end input program.
EXECUTE.

COMPUTE uv1=RV.NORMAL(100,10).
COMPUTE missing=RV.BERNOULLI(""" +  percent + """).
COMPUTE av=uv1 + RV.NORMAL(0,10).
COMPUTE uv_missing=uv1 * missing.
EXECUTE.

MISSING VALUES uv_missing (0).
RMV /uv_missing_1=SMEAN(uv_missing).
*RMV /uv_missing_1=LINT(uv_missing).

oms select tables
       /destination format = sav
       outfile = 'C:/user/frank/frag-einen-statistiker/regression_""" + filenumber +""".sav'
       /if commands =['Regression']
        subtype = ['Coefficients'].

REGRESSION
  /MISSING LISTWISE
  /STATISTICS COEFF OUTS R ANOVA
  /CRITERIA=PIN(.05) POUT(.10)
  /NOORIGIN
  /DEPENDENT av
  /METHOD=ENTER uv1.

 """)
         i+=1
END PROGRAM.

OMSEND .
Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: Python - Macro

David Marso
Administrator
Here is something I built along the lines of my second post.
I would be interested in how it compares WRT runtime compared to the Python/OMS solution.
--
input program.
+  loop P=1 to 100.
+    loop #=1 to 100000.
+      compute uv1 = rv.normal(100,10).
+      compute av=uv1+rv.normal(0,10).
+      compute av1=av.
+      if rv.bernouli(p/100) av1=$SYSMIS.
+      end case.
+      leave p.
+    end loop.
+  end loop.
end file.
end input program.

split file by p.
rmv rav1=lint(av1).
corr var uv1 av rav1 / matrix out (*).
split file by p.
save outfile 'c:\splitcorrmat.sav'.

regression   matrix in (*)
 / var uv1 av
 / miss pairwise
 / dep uv1
 / method enter av
 / outfile = covb('c:\reg1.sav').

select if p < 100.
regression   matrix in (*)
 / var uv1 rav1
 / miss pairwise
 / dep uv1
 / method enter rav1
 / outfile = covb('c:\reg2.sav').
get file  'c:\splitcorrmat.sav'.

add files/ file 'c:\reg1.sav' / file 'c:\reg2.sav'.


drfg2008 wrote
*..........................Test 3 - working- .......................................*

OMS /DESTINATION VIEWER=NO /TAG='suppressall'.
 
BEGIN PROGRAM.
import spss
i = 1
j = 25
filenumber = str(i)+"_" +str(j)

while i < j:

         percent = float(i)
         percent=percent/100
         percent=str(percent)
         
         spss.Submit("""
input program.
loop a =1 to 10**5 by 1.
end case.
end loop.
end file.
end input program.
EXECUTE.

COMPUTE uv1=RV.NORMAL(100,10).
COMPUTE missing=RV.BERNOULLI(""" +  percent + """).
COMPUTE av=uv1 + RV.NORMAL(0,10).
COMPUTE uv_missing=uv1 * missing.
EXECUTE.

MISSING VALUES uv_missing (0).
RMV /uv_missing_1=SMEAN(uv_missing).
*RMV /uv_missing_1=LINT(uv_missing).

oms select tables
       /destination format = sav
       outfile = 'C:/user/frank/frag-einen-statistiker/regression_""" + filenumber +""".sav'
       /if commands =['Regression']
        subtype = ['Coefficients'].

REGRESSION
  /MISSING LISTWISE
  /STATISTICS COEFF OUTS R ANOVA
  /CRITERIA=PIN(.05) POUT(.10)
  /NOORIGIN
  /DEPENDENT av
  /METHOD=ENTER uv1.

 """)
         i+=1
END PROGRAM.

OMSEND .
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Python - Macro

drfg2008
Thanks David

"I would be interested in how it compares WRT runtime compared to the Python/OMS solution. "

I checked all three codes. Your code performed 1:34 min, python needed 2:06 and macro needed 2:05 min (on my laptop).

Ok, you won.

However, if I had to develop a script like yours, I would defenitely still be programming.

Frank



Here the macro:

OMS /DESTINATION VIEWER=NO /TAG='suppressall'.
FILE HANDLE reg1 /NAME ='C:\user\Monte_Carlo.sav'.

DEFINE !impute ().

!DO !p = 1 !TO 99 !BY 1 .

input program.
loop a =1 to 10**5 by 1.
end case.
end loop.
end file.
end input program.
EXECUTE.

COMPUTE uv1=RV.NORMAL(100,10).
COMPUTE !concat("missing=RV.BERNOULLI(",!p,"/100)").
COMPUTE av=uv1 + RV.NORMAL(0,10).
COMPUTE uv_missing=uv1 * missing.
EXECUTE.

MISSING VALUES uv_missing (0).
*RMV /uv_missing_1=SMEAN(uv_missing).
RMV /uv_missing_1=LINT(uv_missing).

oms select tables
       /destination format = sav
       outfile = reg1
       /if commands =['Regression']
        subtype = ['Coefficients'].

REGRESSION
  /MISSING LISTWISE
  /STATISTICS COEFF OUTS R ANOVA
  /CRITERIA=PIN(.05) POUT(.10)
  /NOORIGIN
  /DEPENDENT av
  /METHOD=ENTER uv1.

!DOEND .

OMSEND .

!enddefine.

!impute .
Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: Python - Macro

drfg2008
... thanks also for this little hint, David:

+      rv.bernouli(p/100)


since Arithmetic is not implemented in SPSS macros, I did't know how to solve this little problem (until your script). I tried:

http://www.spsstools.net/Macros/MacrosVariable/ArithmeticWithMacroVariables.txt

but no solution for computing decimals.

Frank

Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: Python - Macro

David Marso
Administrator
In reply to this post by drfg2008
If you get rid of the two EXECUTES (unnecessary), your code will run faster.  Possibly as fast as what I posted.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Python - Macro

drfg2008
I checked it again without exe.:

yours (with open viewer): 1:22 / 1:23 / 1:20

Macro: 1:25 / 1:26 / 1:24
Python: 1:26 / 1:25 / 1:26

Machine:
Vista / AMD Turion 64 X2 dual-core 2.0 GHz, 2x512 KB cache / 3GB DDR2

Frank
Dr. Frank Gaeth