SPSS Statistics

SPSS Statistics

Your hub for statistical analysis, data management, and data documentation. Connect, learn, and share with your peers! 

 View Only
  • 1.  Loops and vectors

    Posted Fri June 23, 2023 10:54 AM

    I need to compute 200 random variables with a binomial distribution with a success probability of .25.  I will use these variables in subsequent analyses of missing data.

    I called the 200 variables pflip1 to pflip200.

    I started with 100 cases and four draws.  I had no problem setting up the 100 cases but the two random variable draws baffle me.

    Here is my code

    INPUT PROGRAM.
    LOOP #id=1 TO 1000.
    - COMPUTE draw=UNIFORM(100).
    - END CASE.
    END LOOP.
    END FILE.
    END INPUT PROGRAM.


    VECTOR pf (4) .
    LOOP #i = 1 to 4 .
    COMPUTE pf(#i) = RV.BINOM(1,0.25).
    END LOOP .
    exe.

    I keep getting an error message that 

     
    >Error # 4257 in column 13.  Text: ) 
    >A variable being defined on the VECTOR command already exists.  To define a 
    >vector composed of existing variables, use the form VECTOR = VARIABLE TO 
    >VARIABLE. 
    >Execution of this command stops. 
     
    >Error # 4030 in column 9.  Text: pf 
    >The operand appearing on the left side of the assignment operator (equals 
    >sign) is not a known vector name or function name. 
    >Execution of this command stops. 
     
    >Error # 4257 in column 13.  Text: ) 
    >A variable being defined on the VECTOR command already exists.  To define a 
    >vector composed of existing variables, use the form VECTOR = VARIABLE TO 
    >VARIABLE. 
    >Execution of this command stops. 
     
    >Error # 4030 in column 9.  Text: pf 
    >The operand appearing on the left side of the assignment operator (equals 
    >sign) is not a known vector name or function name. 
    >Execution of this command stops.

    Please help.



    ------------------------------
    Steven Andes
    ------------------------------


  • 2.  RE: Loops and vectors

    Posted Fri June 23, 2023 12:06 PM
    The statement
    VECTOR PF(4).
    should work as long as pf doesn't already exist either as a vector or as a regular variable. Maybe you ran the code multiple times.

    There is, though, a much easier and more general way to generate datasets of random variables.
    There is a custom dialog box that can create datasets of random variable from around 20 different distributions.  It is available as a zip file from here.

    To use it, open the zip file and extract the two files.  Use Extensions > Utilities > Install Custom Dialog and select the .spd file.  Then copy the other file, statsgeneratedata.py to a location where Python can find it.  If you run SHOW EXT, you will see a list of possible locations in the EXTPATHS EXTENSIONS section.  The dialog will appear on the menus as File > New > Data with Cases.

    --





  • 3.  RE: Loops and vectors

    Posted Fri June 23, 2023 02:20 PM

    Based on your code, it seems like you're using two different statistical software packages, which might be causing confusion. The code you provided has a mix of syntax from different software packages, including some syntax from SPSS and some syntax that resembles R.

    If you want to generate 200 random variables with a binomial distribution in SPSS, you can modify your code as follows:

    VECTOR pflip(200). LOOP #i = 1 TO 200. COMPUTE pflip(#i) = RV.BINOM(1, 0.25). END LOOP. EXECUTE.

    In this modified code, I've changed the vector name from pf to pflip to match your variable naming convention. The loop generates 200 random variables using the RV.BINOM function, which follows the binomial distribution with a success probability of 0.25.

    Please note that this code is specific to SPSS syntax. If you are using a different statistical software package, the syntax and functions may vary.



    ------------------------------
    Youssef Sbai Idrissi
    Software Engineer
    ------------------------------



  • 4.  RE: Loops and vectors

    Posted Fri June 23, 2023 02:57 PM
    The OP's original code was actually all valid SPSS code.  Input programs are rarely used any more as input sources tend to be different from the past, but they are still valid SPSS code.

    --





  • 5.  RE: Loops and vectors

    Posted Fri June 23, 2023 04:34 PM
    Mr. Idrissi

    Thank you!

    I followed your directions and they worked.  I also tried the solution Mr. Peck suggested and that worked too, once I got past the limitation that the university has problems when I try to install anything!

    So, I have two sets of variables, each with 200 cases.  One follows a binomial distribution called pflip1 to pflip200, and the other follows a normal distribution called pnormal1 to pnormal200.

    Now I want to multiply one by other.  Pflip1*pnormal1, pflop2*pnormal2, etc, and store the results in a vector called zproduct.

    I tried this code:

    VECTOR zproduct(200).

    LOOP #i = 1 TO 200.
      COMPUTE zproduct(#i) = pflip(#i)*pnormal(#i).
    END LOOP.

    EXECUTE.

    Unfortunately, I receive this error

    >Error # 4257 in column 20.  Text: )
    >A variable being defined on the VECTOR command already exists.  To define a
    >vector composed of existing variables, use the form VECTOR = VARIABLE TO
    >VARIABLE.
    >Execution of this command stops.

    >Error # 4030 in column 11.  Text: zproduct
    >The operand appearing on the left side of the assignment operator (equals
    >sign) is not a known vector name or function name.
    >Execution of this command stops.

    What am I doing wrong?  

    Help!

    Thank you



    VECTOR zproduct(200).

    LOOP #i = 1 TO 200.
      COMPUTE zproduct(#i) = pflip(#i)*pnormal(#i).
    END LOOP.

    EXECUTE.