Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Stata II

Similar presentations


Presentation on theme: "Introduction to Stata II"— Presentation transcript:

1 Introduction to Stata II
2016 Stefan Dahlberg Dep. of Political Science University of Gothenburg

2 Data management II – creating and changing variables
We want to group this variabel into fewer categorys of countries with low or high degrees of disproportional electoral systems

3 Data management II – creating and changing variables
There are (as usuall) several ways to do this and here is the long way using the generate command: Use excercise2.dta 1, .tab fh_pr 2, create a new variable . gen fh_pr_cat=fh_pr 3, recode the new values of the new variable .recode fh_pr_cat (1/2=1) (2/5=2) (6/7=3) .tab fh_pr_cat

4 Data management II – creating and changing variables
Let’s have a quick look at our new variable: . des fh_pr_cat . sum fh_pr_cat . tab fh_pr_cat

5 Data management II – creating and changing variables
And here’s another way to do it using the genenrate and the replace commands: (we make a new var with even fewer categories) 1, create a new variable . gen fh_pr_cat2=. 2, replace the values of the new variable .replace fh_pr_cat2 =1 if fh_pr_cat==2 .replace fh_pr_cat2 =2 if fh_pr_cat==1 | fh_pr_cat==3

6 Data management II – creating and changing variables
Let’s have a quick look at our new variable: . des disp_cat2 . sum disp_cat2 . tab disp_cat2 To inspect that the codings are okay use tab .tab disp_cat disp_cat2 . drop disp_cat2

7 Data management II – creating and changing variables
Or a combination of them both using the recode and gen commands simultaniously Recode into a new variable . recode fh_pr_cat (1/2=1) (2/5=2) (6/7=3), gen(fh_pr_cat3) .tab fh_pr_cat3

8 Data management II – creating and changing variables
Now, use the quickest method to create a new variable of a fh_ipolity2 with four categorys based on the percentile values…

9 Data management II – creating and changing variables
Solution: .sum fh_ipolity2, d .recode fh_ipolity2 (min/ 1.83 =1) (1.83 /5.21=2) (5.21/8.91=3) (8.91/max=4), gen(fh_ipolity2_4cat) .tab fh_ipolity2_4cat

10 Data management II – creating and changing variables
Lets say that we are interested in the interaction effect between political rights and GDP per capita! .gen int_fhxgdp= fh_pr*mad_gdppc .tab int_fhxgdp Now, we briefly examine the new variable in relation to the others . sum fh_pr mad_gdppc int_fhxgdp

11 Data management II – creating and changing variables
Creating a variable for deviations from the mean .sum fh_cl .gen fh_cl_dev=fh_cl-3.86 .tab fh_cl_dev

12 Data management II – creating and changing variables
Try to only list/tab countries with large deviations from the mean

13 Data management II – creating and changing variables
Now, try to only list/tab countries with large deviations from the mean .tab fh_cl_dev .list cname_year if fh_cl_dev>3.86 .tab cname_year if fh_cl_dev>3.86

14 Data management II – creating and changing variables
Many times the absolute deviation is a better operationalization .sum fh_cl .gen fh_cl_dev=abs(fh_cl-3.86) .tab fh_cl_dev

15 Data management II – creating and changing variables
Introducing the egen command egen = extensions to generate Egen contains many ways to create new variables, mostly as a function of another variable To see everything it can do, type help egen

16 Data management II – creating and changing variables
The egen command egen = extensions to generate General syntax . egen new varname = function(argument), [options] For example: varname, numlist or expression

17 Data management II – creating and changing variables
The egen command includes several functions A few examples of useful egen functions: std fill mean min / max cut Any many many more…

18 Data management II The egen command
If we want standardized variables: .egen var_z=std(varX) For example: .egen mad_pop_z=std(mad_pop) .tab mad_pop_z

19 Data management II The egen command
Or if we want to create an id variable: . egen id=fill(1 2 3) .order id .browse To find information about the numlist, type .help numlist

20 Data management II The egen command
Or if we want to center a variable around its mean: .egen temp=mean(bl_asyt25 ) .gen bl_asyt25 _c=bl_asyt25 -temp .drop temp sometimes used for interactions

21 Data management II The egen command
For more complex variables we can do this separately for different groups via the by-statement using the by or bysort statment. The command is then executed separately for each category of the variable you specify . [by varname:] command varlist [in] [if], [options]

22 Data management II The egen command
A solution (among others) . bysort countrycode: egen temp = min(fh_cl) . gen mad_rs = mad_gdppc – temp . drop temp . tab mad_rs . list cname mad_rs Gen x=

23 Data management II – creating and changing variables
If you have an ordinal variabel and want to turn into a set of dummy-variables .tab x, gen(x_dummy) For example .tab year, gen(year_dummy)

24 Data management II - Creating and changing variables
A somewhat faster method: .recode fh_cl (1=1) (2/max=0), gen(fh_cl1) Even quicker .tab fh_cl, gen(fh_cldum)

25 Data management II After these excercises we have alot of unwanted variables in our data-set Which variables do we want to keep .keep var1-var_n Which variables do we want to drop .drop var1-var_n

26 Data management II Keep and drop commands does also work for observations, remember? .keep if var1==2/4 .drop if var1!=1 .drop if var1<.

27 Summing up III What have we learnt?
Generating new variables Changing and recoding variables Using temporary commands/programs Simple two-way cross-tabulations Creating dummy variables


Download ppt "Introduction to Stata II"

Similar presentations


Ads by Google