HOW TO RUN REGRESSIONS IN STATA AND WINBUGS FROM EXCEL FILES


  1. Go to this page on my server that lists 105 Excel Spreadsheets with House DW-NOMINATE Data:

    Excel Spreadsheet Download Page

    Follow the old CMU homework instructions for picking a file to download. For example, suppose you picked H105.XLS, when you download it and bring it up in Excel it should look something like this:



  2. Open STATA and bring up the data editor. It should look something like this:



  3. Go back to Excel and highlight the whole spreadsheet and copy it to the clipboard:



  4. Now go to the open STATA data editor and right click on the highlighted cell in the upper left hand corner of the spread sheet. Select "Paste" and it should copy the Excel data into the STATA data editor and you should see this:



  5. It is important that every variable be defined accurately. For example, double-click on the variable "id" and fill in the "Label" box with "icpsr id number":



    For "state" use the label "icpsr state code"; for "district" use the label "cong. district number"; for "ser05" rename it to "statenm" with the label "name of state"; for "party", use the label "100=Demo, 200=Repub"; for "ser07" rename it to "member" with the label "name of member"; for "x1", use the label "dwnom 1st dim."; and, finally, for "x2" use the label "dwnom 2nd dim."

  6. Exit the STATA data editor and save the file as (in my case) h105.dta:



  7. Turn on the logging function in STATA so you have a text-file record of what it is you are doing. Select the *.log option! Here I have named the file h105_example.log:



  8. Now, enter the commands:

    d

    and

    summ

    and you should see something like this:



  9. Bring your log file up in Epsilon and you will see:



  10. Create two dummy variables for political party and North/South regions of the U.S.. In particular, call these partydum and southdum, respectively. These can be created in STATA in the following manner:

    generate partydum=0
    replace partydum=1 if party==200


    generate southdum=0
    replace southdum=1 if state >= 40 & state <= 49
    replace southdum=1 if state==51
    replace southdum=1 if state==53
    replace southdum=1 if state==54


    It is a very good idea to check your work. You can do this by using the browse command in Stata. In particular:

    browse party partydum

    and

    browse state southdum

    Open the data editor and type in definitions for partydum and southdum. Enter the d

    and summ

    commands and paste the results into your homework answer.

    To do this, go back to Epsilon and you will see:



    Click on "Read" and that brings up the latest version of your STATA log file -- in my case h105_example.log. Scroll down and highlight the d and summ output:



    Put the highlighted text on the clipboard and then go to (if this is what you use) Microsoft Word and paste the text into the document. It will look lousy, namely:



    In Word highlight everything you just pasted in and the select font "Courier New", font "8", under the paragraph option select "single space" with "no space between paragraphs of the same style." This formats it all very nicely so it looks like this:



    Do this for everything that I ask you to put into Word.

  11. In STATA run the following regressions:

    regress x1 partydum x2

    and

    regress x1 partydum southdum

    Paste the results into your homework answer.

  12. Now we are going to run the same regressions in WINBUGS. First we need to get the variables we need out of STATA so that we can use Epsilon to put them in a form that WINBUGS understands. To get the data, in STATA type:

    outfile x1 x2 partydum southdum using c:\docs_bayesian_statistics\h105_bugs, wide

    This creates a file called h105_bugs.raw in the indicated directory. Bring this file up in Epsilon and it should look like this:



    Position your cursor at the top of the screen to the far left. We are now going to write an Epsilon keyboard macro (to see a helpful example of a macro go through Advanced Multivariate Homework 3 from 2001). To begin the macro, type:

    C-X ( -- This means "Control-X" then "(" -- that is, hold down the Control Key -- Ctrl -- and type X)

    . You will see:



    Note the Remembering at the bottom of the screen. Epsilon now records all the key-strokes that you enter until you give the command C-X ).

    Type C-F 9 times (the Right Arrow Key does the same thing). Now type a "," (comma) and you will see:



    (Also note the asterisk "*" to the far right of the name line at the bottom of the top screen. This is a signal from Epsilon that the file has been altered and not saved. If you clicked on "File Save" that asterisk will not be there.) Now type C-F 11 times and then ","; type C-F 11 times and then ","; and finally type C-F 11 times and ",". This puts you at the end of the first line and it should look like this:



    Now type C-A. This takes the cursor to the beginning of the line. Again, hold down the Ctrl key and hit "A".
    Now type C-N. This positions the cursor at the beginning of the line below. We need to do this so that when the macro runs the first time it ends with the cursor positioned at the next line. You should now see this:



    Now close the macro with the command C-X ). This ends the remembering and completes the macro.

    You should see:



    To execute the macro one time type:

    C-X E -- This means "hold the Control key down and type X then type E"

    You should see:



    Before executing the macro multiple times it is always good practice to check how many lines there are in the file so that you do not try to go beyond the end of the file (which can have some pretty nasty consequences). To do this type:

    C-X L -- This means "hold the Control key down and type X then type L". This command tells you how many lines are in the file.

    You should see:



    Epsilon tells you that there are 443 lines and the cursor is on line 3. We need to run the macro 441 more times but lets execute it only 439 times just to be on the safe side (this is good practice!). To do this type:

    C-U -- Hold down the Control Key and type "U". You should see:



    The C-U function will cause whatever command you enter next to be repeated. The "4" is a default. Simply type:

    439 C-X E

    When you type "439" it overwrites the default "4" and when you type the C-X the red banner at the bottom of Epsilon looks like this:



    Now just type "E" and the Keyboard Macro will run 439 times and you should see this:



    Run the macro two more times by simply typing C-X E twice and this will put you at the end of the file. Now, either click on the standard WINDOWS save file icon or you can save a file in Epsilon by typing C-X C-S -- hold down the Control Key and type "X" and then still holding down the Control Key and type "S". Note that the asterisk will disappear at the bottom of the window. Here is what it should look like:



    Now, place the cursor back at the top of the file. You can do this by simply using the Page-Up but try the command:

    ESC < -- This means "hit the Escape key then hit the < (less than) key.

    To go to the bottom of the file use the command:

    ESC >

    Note that ALT appears at the bottom of the Epsilon window.

    (Note that the two commands are the same as:

    C-Home -- Hold down the Ctrl key and type Home.

    C-End -- Hold down the Ctrl key and type End.)

    Only two steps left to make this file ready to read into WINBUGS. At the top of the file hit Enter to give yourself a blank line and then type:

    list (X = structure(.Data = c( so it looks like this:



    Now go to the bottom of the file with C-End, back the cursor up so it is between the last "0" and the "," of the last line, hit Enter, then put a right parentheses before the comma and type .Dim=c(443,4))) (Note that the data matrix has 443 rows and 4 columns.) Save the file and the end should look like this:



    Finally, save the file with a *.txt extension with the write-file command C-X C-W:



    Now type something with a *.txt extension -- in this case, I will use h105_BUGS_data_example.txt:



    Now, simply type Enter and you have created the file:



    Note the appearance of the word written at the end of the command window of Epsilon. This means that the file has been written to disk with the name that you have assigned it.

    You can now close out this session of Epsilon on this specific file by typing C-X C-C -- hold down Control Key and type "X" and hold down Control Key and type "C".

  13. Now we need the model file for WINBUGS and we can run the regression.

    You can screen scape the model below or simply download it here:

    h105_BUGS_model_example.txt
    model
    {
    #  
    #  X[,1] = DW-NOMINATE 1st Dimension
    #  X[,2] = DW-NOMINATE 2nd Dimension
    #  X[,3] = 1 if Republican, 0 otherwise
    #  X[,4] = 1 if South (CQ def.), 0 otherwise
    #  
    #  PRIORS
    #
             tau ~ dgamma(1.0E-1, 1.0E-1)
             for (k in 1 : 3) { beta[k] ~ dnorm(0,0.001)} # vague priors
    #	  
    # LIKELIHOOD	
    #
        for (i in 1 : 434)  # loop over congressional districts
        {     
    #
             X[i,1] ~ dnorm( mu[i] , tau)
             mu[i] <- beta[1]+X[i,3]*beta[2]+X[i,2]*beta[3]
        }  
        sigma <- sqrt(1/tau)
    #
     }