Example: quiz answers

Working with categorical data and factor variables

26 Working with categorical data and factor , categorical , and indicator continuous variables to indicator continuous variables to categorical with factor factor base base levels significance of a main indicator (dummy) variables as factor significance of factorial squared terms and Including interactions with continuous Parentheses Including indicators for single Including subgroups of Combining factor variables and time-series Treatment of empty Continuous, categorical , and indicator variablesAlthough to Stata a variable is a variable, it is helpful to distinguish among three conceptual types: Acontinuous variablemeasures something. Such a variable might measure a person s age, height,or weight; a city s population or land area; or a company s revenues or term continuous here is deliberately broad and includes variables that are discrete byconvention (ages in years) or by definition (counts of people).

2[U] 26 Working with categorical data and factor variables it is an indicator variable because it denotes the truth value of the statement “the observation is in this group”. All indicator variables are categorical variables, but the opposite is not true. A categorical variable might divide the data into more than two groups.

Tags:

  With, Data, Working, Categorical, Working with categorical data

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of Working with categorical data and factor variables

1 26 Working with categorical data and factor , categorical , and indicator continuous variables to indicator continuous variables to categorical with factor factor base base levels significance of a main indicator (dummy) variables as factor significance of factorial squared terms and Including interactions with continuous Parentheses Including indicators for single Including subgroups of Combining factor variables and time-series Treatment of empty Continuous, categorical , and indicator variablesAlthough to Stata a variable is a variable, it is helpful to distinguish among three conceptual types: Acontinuous variablemeasures something. Such a variable might measure a person s age, height,or weight; a city s population or land area; or a company s revenues or term continuous here is deliberately broad and includes variables that are discrete byconvention (ages in years) or by definition (counts of people).

2 Even for such variables , reportedvalues are points on continuous scales with natural origins, and not arbitrary codes. Acategorical variableidentifies a group to which the thing belongs. You could categorize personsaccording to their race or ethnicity, cities according to their geographic location, or companiesaccording to their industry. Sometimes, categorical variables are stored as strings. Anindicator variabledenotes whether something is true. For example, is a person a veteran, doesa city have a mass transit system, or is a company profitable?Indicator variables are a special case of categorical variables . Consider a variable that recordswhether or not a person is employed. Examined one way, it is a categorical variable. A categoricalvariable identifies the group to which a thing belongs, and here the thing is a person and the basis forcategorization is employment. Looked at another way, however, it is an indicator variable.

3 It indicateswhether the person is employed. In this example, and most others, there is much scope for a fineror otherwise different categorization, but bear with can use the same logic on any categorical variable that divides the data into two groups. It isa categorical variable because it identifies whether an observation is a member of this or that group;12 [ U ] 26 Working with categorical data and factor variablesit is an indicator variable because it denotes the truth value of the statement the observation is inthis group .All indicator variables are categorical variables , but the opposite is not true. A categorical variablemight divide the data into more than two groups. For clarity, let s reserve the termcategorical variablefor variables that divide the data into more than two groups, and let s use the termindicator variablefor categorical variables that divide the data into exactly two can convert continuous variables to categorical and indicator variables and categorical variablesto indicator Converting continuous variables to indicator variablesStata treats logical expressions as taking on the valuestrueorfalse, which it identifies with thenumbers 1 and 0; see[U] 13 Functions and expressions.

4 For instance, if you have a continuousvariable measuring a person s age and you wish to create an indicator variable denoting persons aged21 and over, you could type. generate age21p = age>=21 The variableage21ptakes on the value 1 for persons aged 21 and over and 0 for persons under take on only 0 or 1, it would be more economical to store the variable as abyte. Thus it would be better to type. generate byte age21p = age>=21 This solution has a problem. The value ofage21is set to 1 for all persons whoseageis missingbecause Stata defines missing to be larger than all other numbers. In our data , we might have nosuch missing ages, but it still would be safer to type. generate byte age21p = age>=21 if age<.That way, persons whose age is missing would also have a notePut aside missing values and consider the following alternative togenerate age21p = age>=21that may have occurred to you:. generate age21p = 1 if age>=21 That does not produce the desired result.

5 This statement makesage21p1 (true) for all persons aged21 and above but makesage21pmissing for everyone you followed this second approach, you would have to combine it with . replace age21p = 0 if age<21[ U ] 26 Working with categorical data and factor variables Converting continuous variables to categorical variablesSuppose that you wish to categorize persons into four groups on the basis of their age. You wanta variable to denote whether a person is 21 or under, between 22 and 38, between 39 and 64, or65 and above. Although most people would label these categories 1, 2, 3, and 4, there is really noreason to restrict ourselves to such a meaningless numbering scheme. Let s call this new variableagecatand make it so that it takes on the topmost value for each group. Thus persons in the firstgroup will be identified with anagecatof 21, persons in the second with 38, persons in the thirdwith 64, and persons in the last (drawing a number out of the air) with 75.

6 Here is a way to createthe variable that will work, but it is not the best method for doing so:. use generate byte agecat=21 if age<=21(176 missing values generated). replace agecat=38 if age>21 & age<=38(148 real changes made). replace agecat=64 if age>38 & age<=64(24 real changes made). replace agecat=75 if age>64 & age<.(4 real changes made)We created the categorical variable according to the definition by using thegenerateandreplacecommands. The only thing that deserves comment is the openinggenerate. We (wisely) told Statatogeneratethe new variableagecatas abyte, thus conserving can create the same result with one command using therecode()function:. use , clear. generate byte agecat=recode(age,21,38,64,75)recode()ta kes three or more arguments. It examines the first argument (hereage) against theremaining arguments in the list. It returns the first element in the list that is greater than or equal tothe first argument or, failing that, the last argument in the list.

7 Thus, for each observation,recode()asked ifagewas less than or equal to 21. If so, the value is 21. If not, is it less than or equal to 38?If so, the value is 38. If not, is it less than or equal to 64? If so, the value is 64. If not, the value researchers typically make tables of categorical variables , so we willtabulatethe result:. tabulate agecatagecatFreq. Percent [ U ] 26 Working with categorical data and factor variablesThere is another way to convert continuous variables into categorical variables , and it is even moreautomated:autocode()works likerecode(), except that all you tell the function is the range andthe total number of cells that you want that range broken into:. use , clear. generate agecat=autocode(age,4,18,65). tabulate agecatagecatFreq. Percent one instruction, we told Stata to breakageinto four evenly spaced categories from 18 to wetabulate agecat, we see the result.

8 In particular, we see that the breakpoints of thefour categories are , , , and 65. The first category contains everyone aged yearsor less; the second category contains persons over who are years old or less; the thirdcategory contains persons over who are years old or less; and the last category containsall persons over noteWe chose the range 18 65 arbitrarily. Although you cannot tell from the table above, there arepersons in this dataset who are under 18, and there are persons over 65. Those persons are countedin the first and last cells, but we have not divided the age range in the data evenly. We could splitthe full age range into four categories by obtaining the overall minimum and maximum ages (bytypingsummarize) and substituting the overall minimum and maximum for the 18 and 65 in theautocode()function:. use , clear. summarize ageVariableObs Mean Std.

9 Dev. Min Maxage204 2 66. generate agecat2=autocode(age,4,2,66)We could alsosortthe data into ascending order ofageand tell Stata to construct four categoriesover the rangeage[1](the minimum) toage[N](the maximum):. use , clear. sort age. generate agecat2=autocode(age,4,age[1],age[_N]). tabulate agecat2agecat2 Freq. Percent [ U ] 26 Working with categorical data and factor variables Estimation with factor variablesStata handles categorical variables as factor variables ; see[U] factor variables . Categoricalvariables refer to the variables in your data that take on categorical values, variables such assex,group, andregion. factor variables refer to Stata s treatment of categorical variables . factor variablescreate indicator variables for the levels (categories) of categorical variables and, optionally, for what follows, the word level means the value that a categorical variable takes on.

10 The variableemployedmight take on levels 0 and 1, with 0 representing not employed and 1 representingemployed. We could say thatemployedis a two-level factor regressors created by factor variables are called indicators or, more explicitly, virtual indicatorvariables. They are called virtual because the machinery for factor variables seldom creates newvariables in your dataset, even though the indicators will appear just as if they were variables in yourestimation be used as a factor variable, a categorical variable must take on nonnegative integer values. Ifyou have variables with negative values, recode them; see [D]recode. If you have string variables ,you can useegen sgroup()function to recode them,. egennewcatvar= group(mystringcatvar)If you also specify thelabeloption,egenwill create a value label for the numeric code itproduces so that your output will be subsequently more readable.


Related search queries