// Task 1: routine setup capture log close // close log file if one is open set more off // tells Stata not to pause or display the --more-- message estimates clear // drop all stored estimation results log using cda01c-ex1done.log, replace text version 9.2 set scheme s2mono // scheme for graphs // pgm: cda01c-ex1done.do // task: 1 - Introduction to Stata - Excercise // project: CDA Lab Guide // author: your name // date: today's date use nes3.dta, clear // Task 2: Keep abortion, age, didvote, fincome, partyid and preclin. // Explore these variables and drop missing cases. // Step 2.1 keep the 6 variables. keep abortion age didvote fincome partyid preclint // Step 2.2 explore your variables using -desc- -tab- and -dotplot- desc //what is the note attached to the variable preclint? note preclint tab abortion, m tab age, m tab didvote, m tab fincome, m tab partyid, m tab preclint, m * another way to get the same results. tab1 abortion age didvote fincome partyid preclint, m dotplot preclint graph export cda01c-ex1-fig1.emf, replace // Step 2.3 drop missing cases drop if abortion>=. | age>=. | didvote>=. | fincome>=. /// | partyid>=. |preclint>=. // Task 3: Generate partyaff based on partyid. // // If partyid is strong democrat or weak democrat, partyaff is 1. // If partyid is any type of independents, partyaff is 2. // If partyid is any type of Republican, partyaff is 3. // Recode other parties as missing; drop cases if partyaff is missing. // // Add labels to partyaff. Verify your steps. // Step 3.1 generate partyaff from partyid. gen partyaff = partyid // Step 3.2 recode partyaff and drop missing cases. // // partyaff=1 if partyid is 0 or 1 // partyaff=2 if partyid is 2 or 3 or 4 // partyaff=3 if partyid is 5 or 6 // partyaff=missing if partyid is 7 recode partyaff 0/1=1 2/4=2 5/6=3 7=. drop if partyaff >=. // Step 3.3 label variable and label values lab var partyaff "Party Affiliation." lab def partyafffmt 1 "1_Democrat" 2 "2_Independ" 3 "3_Republic" lab val partyaff partyafffmt // Step 3.4 verify your results tab partyid partyaff, m // Task 4: Generate vote from didvote. // // If didvote=1, then vote=1 // If didvote=5, then vote=0 // If didvote=6, then vote=. // // Add labels and verify your transformation. // Step 4.1 generate vote from didvote gen vote = didvote // Step 4.2 recode vote and drop the missing cases. recode vote 1=1 5=0 6=. drop if vote >=. // Step 4.3 add variable and value labels. label var vote "Vote in 92 pres election?" label define votefmt 1 "1_yes" 0 "0_no" label values vote votefmt // Step 4.4 verify tab vote didvote, m // Task 5 Generate income from fincome. Then recode income in $1000s by // choosing the midpoint of each category of fincome. For the highest // category, >105k, multiply that number by 1.5. Make 66 (below 25K but NA) // and 77 (above 25K but NA) missing. Label and verify your transformation. // Step 5.1 create income from fincome gen income = fincome // Step 5.2 recode income into $1000 using midpoints. recode income /// 1=1.5 3=6 4=8 2=4 5=9.5 6=10.5 7=11.5 /// 8=12.5 9=13.5 10=14.5 11=16 12=18.5 13=21 14=23.5 /// 15=27.5 16=32.5 17=37.5 18=42.5 19=47.5 20=55 21=67.5 /// 22=82.5 23=97.5 24=157.5 66/max=. // Step 5.3 label variable income label var income "Income in $1000s." // Step 5.4 verify bysort fincome: tab income fincome, m // Task 6 [Optional]: First, create three dummy variables. // // democrat=1 if partyaff=1, else 0. // indep=1 if partyaff=2, else 0. // republican=1 if partyaff=3, else 0. // // Add labels and verify // Run an OLS regression of vote on age, income, indep & republican. // Run a logit of vote vote on age, income, indep & republican. // Compare results. // Step 6.1 create the dummy variables. Note that missing don't change. gen democrat = (partyaff==1) if partyaff<. gen indep = (partyaff==2) if partyaff<. gen republican = (partyaff==3) if partyaff<. // Step 6.2 add labels and verify label var democrat "Democrat?" label var indep "Independent?" label var republican "Republican?" label define yesno 1 "1_yes" 0 "0_no" label val democrat yesno label val indep yesno label val republican yesno tab partyaff democrat , m tab partyaff indep , m tab partyaff republican , m // Task 7 Run OLS and logit regressions and get coefficients using listcoef // Step 7.1 run OLS of vote on age, income, indep and republican. reg vote age income indep republican // Step 7.2 use listcoef to list coefficients listcoef , help // Step 7.3 run logit for same variables. logit vote age income indep republican // Step 7.4 use listcoef to list coefficients listcoef , help // Task 8: Save the new variables in a new data set. // Create a summary table of the variables and close the log file. // Step 8.1 save the data save cda01c-ex1-nes, replace // Step 8.2 descriptive statistics desc sum * you can use the command vardesc to create a nicer looking table vardesc , squeeze minl // Step 8.3 close log file log close exit