You are on page 1of 5

/*Tutorial 6 - Panel Data*/ /*Set the delimiter between one line and another as a semi-colon:*/ #delimit; /*Make it so the

program will pause whenever you tell it to:*/ pause off; /*Clear memory from previous data, and close any open log file. We are starting anew.*/ clear; cap log close; pause; /* Set memory to 15 megabytes. Can be bigger, but than your machine will run slower.*/ set mem 15m; pause; /* Set the more function off to expediate analysis.*/ set more off; pause; /* Set central directory*/ cd "C:\Documents and Settings\Maria Teresa Candido\My Documents\UCSD\Econ 120C\Data\Tutorials"; pause; /* Open a log file, so that you can keep track of all your work.*/ log using fatality_ch10.log, replace; pause; /* Let's begin by opening the dataset fatality.dta.*/ use fatality.dta; pause; /* Save your data as a new file, so that you do not alter your original file!! Doing this, will free you up to experiment at will. Replace the old file*/ save fatality_out.dta, replace; pause; /*Lets see what this dataset contains.*/ describe; /* The dataset contains information on the lower 48 states, annually for 1982 through 1988. It contains information on the number of traffic deaths in a given state for a given year, per 10,000 people living in that state in that year (traffic fatality rate). It also includes variables that might be relevant in explaining the traffic fatality rate, such as beer taxes, drunk driving conviction laws, total vehicle miles travelled annually in the state, for example. */ *************************************************************; * Replication of results for chapter 10 of the textbook; *************************************************************; ************************************************************; * summary statistics; sum year; sum state; ************************************************************;

/* Lets start with the simple cross section regression of fatality rate on beer tax, for the year of 1982 and the year of 1988. We will replicate the results of equations 10.2 and 10.3 of your textbook. */ /* First, we have to generate the fatality rate variable. */ gen vfrall=10000*mrall; *fatality rate per 10,000 in the population; **********************************************; **** Equation 10.2 ; **********************************************; regress vfrall beertax if (year==1982), robust; display "Adjusted Rsquared = " _result(8); **********************************************; **** Equation 10.3 ; **********************************************; reg vfrall beertax if (year==1988), r; display "Adjusted Rsquared = " _result(8); **********************************************; /* Lets do the plots on Figure 10.1 */ twoway (lfit vfrall beertax) (scatter vfrall beertax, mcolor(black) msize(medsmall) msymbol(circle)) if year==1982, ytitle(Fatality Rate (Fatalities per 10,000)) ytitle(, orientation(vertical)) ylabel(#9) xtitle(Beer Tax (Dollars per case $1998)) xtitle(, orientation(horizontal) margin(right)) xlabel(#6) legend(off); twoway (lfit vfrall beertax) (scatter vfrall beertax, mcolor(black) msize(medsmall) msymbol(circle)) if year==1988, ytitle(Fatality Rate (Fatalities per 10,000)) ytitle(, orientation(vertical)) ylabel(#9) xtitle(Beer Tax (Dollars per case $1998)) xtitle(, orientation(horizontal) margin(right)) xlabel(#6) legend(off); /* Lets do the first difference regressions. We will replicate the result of equation 10.8 */ **********************************************; **** Equation 10.8 ; **********************************************; /* The preserve command will save the dataset. */ preserve; /* The next two commands generate diffrences in the traffic fatality rates and in the beer tax for a state in a specific period and the same state 6 periods before */ gen dvfrall = vfrall-vfrall[_n-6]; gen dbtax = beertax-beertax[_n-6]; /* We will only keep the data regarding 1988, and regress the difference on fatality rate on difference on beer tax. */ keep if year==1988; regress dvfrall dbtax, robust; display "Adjusted Rsquared = " _result(8); /* The following command restores the dataset to the same condition where it was when we preserve it last. */ restore; /* Lets now show the several different methods of controlling for state fixed effects, when you want to use more than two periods of observations. In stata, there are basically four ways to do fixed effect regressions. (1) You can use dummy variables and use the command regress, or (2) you can use the command areg with the option absorb, or (3) the command xtreg with the option fe, or (4) you can demean the variables and use the command regress. */ **********************************************;

**** Equation 10.15 ; **********************************************; /* AREG: This is equivalent to adding a dummy for each subject, but the value of each entity fixed effect alpha is not shown. */ areg vfrall beertax, absorb(state) robust; /* XTREG, with the option fe (fixed effects) also estimates the fixed effects regression. However, the output of areg is easier to read and more accurate (for example, R-squared in this xtreg regression is not accurate.*/ xtreg vfrall beertax, fe i(state) robust; /* DUMMY VARIABLE SPECIFICATION: You can also create dummie variables for all states, and see if you are able to obtain the coefficients by running a usual OLS regression. However, this method is usually just possible if the number of entities is small. Lets see if we can obtain any results using this method in this case. */ cap tab state, gen(stt); /* The above command generate a variable stt* for each state (where * will be 1, 2, ... 48). */ /* When you generate a bunch of new variables this way, which is usually so that we don't have to spell out in your regression 47 dummy variables, make sure that no other variable starts with the same letters. For example, you don't want to create dummy variables st* because when you do your regression and write st*, stata will go and look at all the variables starting with st and put them in the regression - so you would introduce in the regression the variable state, and you don't want that. */ /* Drop the first dummy variable to avoid multicolinearity. */ drop stt1; regress vfrall beertax stt*, robust; /* we were able to get an estimate! Notice how the estimated coefficient in beertax is the same for all regressions: the areg, the xtreg and the dummy variables specification. */ /* Lets test if the state effects are jointly significant. To test a list of parameters use the command testparm. */ testparm stt*; /* State fixed effects are jointly significant. */ /* Note: I have not yet figure out a way for testing the joint significance of state fixed effects when we use areg... */ /* DATA DEMEANING: In this case, we are going to compute each entity mean (we compute a mean across time for each state). Then, we subtract that mean to obtain demeaned data and then use usual regress command to estimate the effect of beer tax. */ /* To compute the means use the following command: */ sort state year; by state: egen m_vfrall = mean(vfrall); by state: egen m_tax = mean(beertax); /* Now, demean the data: */ gen dm_vfrall = vfrall - m_vfrall; gen dm_beertax = beertax - m_tax; regress dm_vfrall dm_beertax, robust noconstant;

/* Notice that we obtain the same coefficient for beer tax once again... */ /* Now lets introduce time effects. */ * year dummies; cap tab year, gen(yr); /* Notice that we could also have done: gen yr82=(year==1982); gen yr83=(year==1983); gen yr84=(year==1984); gen yr85=(year==1985); gen yr86=(year==1986); gen yr87=(year==1987); gen yr88=(year==1988);*/ /* Estimate the regression with state fixed effects and time fixed effects. Again, you can do that by using dummy variables, areg, xtreg and demeaning. Lets do some of those methods. */ **********************************************; **** Equation 10.21 ; **********************************************; /* To avoid perfect multicolinearity, lets drop yr1. */ /* Using areg, and testing for the joint significance of the time fixed effects. */ drop yr1; areg vfrall beertax yr*, absorb(state) robust; testparm yr*; /* Using demeaned data. */ /* Demeaning using entity means... */ regress dm_vfrall dm_beertax yr*, robust; /* Demeaning using entity and time means... */ sort year state; by year: egen tm_vfrall = mean(vfrall); by year: egen tm_tax = mean(beertax); gen dtm_vfrall = vfrall - m_vfrall - tm_vfrall; gen dtm_beertax = beertax - m_tax - tm_tax; regress dtm_vfrall dtm_beertax, robust; /* We could also use entity dummies and demean data using only time means, but we are not going to do that right now. */ /* Finally, lets us replicate the results from table 10.1 */ /* In particular take a look at column (7) where we use clustered standard errors... */ /* We need to generate a bunch of new variables: Dummies for minimum drinking age, etc */

gen da18=(mlda<19); * minimum legal drinking age; gen da19=(mlda>=19)*(mlda<20); gen da20=(mlda>=20)*(mlda<21); gen da21=(mlda>=21); gen incperc=perinc/1000; gen lincperc = ln(incperc); gen vmilespd = vmiles/1000; gen frmall = mrall/(vmiles/100000); gen jailcom = ((jaild+comserd)>0); gen mjailcom = (jaild==.)*(comserd==.); mvdecode mjailcom,mv(1); replace jailcom = jailcom + mjailcom; **********************************************; **** Table 10.1 ; **********************************************; **** col(1); reg vfrall beertax, r; dis "Adjusted Rsquared = " _result(8); **** col(2); areg vfrall beertax, absorb(state) robust; **** Col(3); areg vfrall beertax yr*, absorb(state) r; testparm yr*; **** Col(4); areg vfrall beertax da18 da19 da20 jaild comserd vmilespd unrate lincperc yr*, absorb(state) robust; testparm yr*; test da18 da19 da20; test jaild comserd; test unrate lincperc; **** Col(5); areg vfrall beertax da18 da19 da20 jaild comserd vmilespd yr*, absorb(state) robust; testparm yr*; test da18 da19 da20; test jaild comserd; **** Col(6); areg vfrall beertax mlda jailcom vmilespd unrate lincperc yr*, absorb(state) robust; testparm yr*; test unrate lincperc; **** Col(7); areg vfrall beertax da18 da19 da20 jaild comserd vmilespd unrate lincperc yr*, absorb(state) robust cluster(state); testparm yr*; test da18 da19 da20; test jaild comserd; test unrate lincperc; log close; exit;