How To Use Gen To Change Dummy Variable
Dummy variables in Stata
Dummy (logical) variables in Stata accept values of 0, 1 and missing.
Indicator variables in variable lists
The most mutual use of dummy variables is in modelling, for instance using regression (nosotros will use this every bit a general example below). For this use yous do not demand to create dummy variables as the variable listing of any command can comprise factors and operators based on factors generating indicator (dummy) variables.
When y'all are generating indicator variables (dummy variables, contrasts) from a categorical variables like the continent variable, you need to omit 1 of the categories (base or reference categories). In all regression examples below 1 of the continents will be omitted, i.east. in the regression you will discover five out of the six continents. By default the get-go (smallest) value volition be used equally reference category; in that location is a ib operator to indicate other base values
regress infmor urb i.continent | 5 indicator variables, the kickoff continent is the base of operations category |
regress infmor urb ib2.continent | continent two is the base of operations |
backslide infmor urb ib(offset).continent | Kickoff continent is the base, same every bit i.continent |
backslide infmor urb ib(last).continent | Last continent is base |
regress infmor urb ib(freq).continent | The continent with the highest frequency count is base |
If you wish to contrast a specific continent, e.chiliad. Asia against all others your can wite (both forms are equivalent)
backslide infmor 1.continent regress infmor i1.continent
See the documentation for further variations
Employ the generate command
Generate a dummy variable: Countries below 50% of urbanization=0, in a higher place 50=1
generate urbdum = 0 replace urbdum= 1 if urb>50
Or shorter
generate urbdum= (urb>fifty)
generate urbdum= (urb>l) produces the variable every bit when urb>50 is true Stata produces a value or 1 (for true) and 0 otherwise (=simulated).
There is however a problem with this when you lot have missing values in the variable. Stata stores missing values as positive infinity, i.due east. a very large positive value, i.e. a value of 1. If you wish to avoid this, y'all demand to care for missing values specifically, namely
generate urbdum=0 supersede urbdum=i if urb>50 supercede urbdum= . if missing(urb)
or
generate urbdum1= urb>50 if !missing(urb)
Examples showing how to create a dummy variable from a chiselled variable, continent here:
generate Asia=continent==1 generate America=continent==iv | continent==five
This creates a variable with value of ane if the condition is true and 0 if the condition is imitation. (In Stata logical values are represented past 0/1 (false/true).
Use tabulate
The tabulate command has an option to generate automatically dummy variables from a categorical variable:
tabulate continent, generate(cont)
Produces the variables shown to the left.
Using the reode command
recode can also be used as shown here:
recode v3 (min/xx=1 Rich ) (else=0 Not_rich) , generate (d6) label(Dummy_richcountry)
Labels tin can be specified directly. The to a higher place case creates a new variable d6 from v3, values beneath twenty will exist set to 1 (labelled "Rich" in the new variable), 0 otherwiese
Using gene/dummy variables outside modelling
As dummy variables are logical variables you tin can utilise them with if to simplify the utilize of filters. Assuming that you lot accept created: generate America=continent==iv | continent==five yous tin can simply write
list urb infmor land if America
To list only american countries.
Related documents
- Create groups
- *Data transformation
Source: http://www.unige.ch/ses/sococ/cl/stata/tasks/dummies.html
Posted by: bohntheirried.blogspot.com
0 Response to "How To Use Gen To Change Dummy Variable"
Post a Comment