Chapter 4 Data Structures and Types of Variables
4.1 Data require structure and context
Descriptive statistics are concerned with the presentation, organization and summary of data, as suggested in Norman and Streiner (2014). This includes various methods of organizing and graphing data to get an idea of what those data can tell us.
As Vittinghoff et al. (2012) suggest, the nature of the measurement determines how best to describe it statistically, and the main distinction is between numerical and categorical variables. Even this is a little tricky - plenty of data can have values that look like numerical values, but are just numerals serving as labels.
As Bock, Velleman, and De Veaux (2004) point out, the truly critical notion, of course, is that data values, no matter what kind, are useless without their contexts. The Five W’s (Who, What [and in what units], When, Where, Why, and often How) are just as useful for establishing the context of data as they are in journalism. If you can’t answer Who and What, in particular, you don’t have any useful information.
In general, each row of a data frame corresponds to an individual (respondent, experimental unit, record, or observation) about whom some characteristics are gathered in columns (and these characteristics may be called variables, factors or data elements.) Every column / variable should have a name that indicates what it is measuring, and every row / observation should have a name that indicates who is being measured.
4.2 A New NHANES Adult Sample
In previous work, we spent some time with a sample from the National Health and Nutrition Examination. Now, by changing the value of the set.seed
function which determines the starting place for the random sampling, and changing some other specifications, we’ll generate a new sample describing 500 adult subjects who completed the 2011-12 version of the survey when they were between the ages of 21 and 64.
Note also that what is listed in the NHANES data frame as Gender
should be more correctly referred to as sex
. Sex
is a biological feature of an individual, while Gender
is a social construct. This is an important distinction, so I’ll change the name of the variable. I’m also changing the names of three other variables, to create Race
, SBP
and DBP
.
# library(NHANES) # NHANES package/library of functions, data
nh_temp <- NHANES %>%
filter(SurveyYr == "2011_12") %>%
filter(Age >= 21 & Age < 65) %>%
mutate(Sex = Gender, Race = Race3, SBP = BPSysAve, DBP = BPDiaAve) %>%
select(ID, Sex, Age, Race, Education, BMI, SBP, DBP, Pulse, PhysActive, Smoke100, SleepTrouble, HealthGen)
set.seed(431002)
# use set.seed to ensure that we all get the same random sample
nh_adults <- sample_n(nh_temp, size = 500)
nh_adults
# A tibble: 500 x 13
ID Sex Age Race Education BMI SBP DBP Pulse PhysActive
<int> <fct> <int> <fct> <fct> <dbl> <int> <int> <int> <fct>
1 64427 male 37 White College ~ 36.5 111 72 56 Yes
2 63788 fema~ 40 White High Sch~ 18.2 115 74 102 Yes
3 66874 fema~ 31 White Some Col~ 27.2 95 52 98 Yes
4 69734 male 26 White College ~ 20.6 137 75 74 Yes
5 70409 male 44 White High Sch~ 29.2 112 71 62 Yes
6 68961 fema~ 64 White College ~ 24.2 123 70 80 Yes
7 62616 fema~ 37 Asian 8th Grade 19.3 109 73 82 No
8 70130 male 42 Black High Sch~ 31.2 119 71 62 No
9 71218 male 33 White College ~ 27.7 110 67 68 Yes
10 69181 fema~ 37 White 8th Grade 25 114 74 82 Yes
# ... with 490 more rows, and 3 more variables: Smoke100 <fct>,
# SleepTrouble <fct>, HealthGen <fct>
The data consist of 500 rows (observations) on 13 variables (columns). Essentially, we have 13 pieces of information on each of 500 adult NHANES subjects who were included in the 2011-12 panel.
4.2.1 Summarizing the Data’s Structure
We can identify the number of rows and columns in a data frame or tibble with the dim
function.
[1] 500 13
The str
function provides a lot of information about the structure of a data frame or tibble.
Classes 'tbl_df', 'tbl' and 'data.frame': 500 obs. of 13 variables:
$ ID : int 64427 63788 66874 69734 70409 68961 62616 70130 71218 69181 ...
$ Sex : Factor w/ 2 levels "female","male": 2 1 1 2 2 1 1 2 2 1 ...
$ Age : int 37 40 31 26 44 64 37 42 33 37 ...
$ Race : Factor w/ 6 levels "Asian","Black",..: 5 5 5 5 5 5 1 2 5 5 ...
$ Education : Factor w/ 5 levels "8th Grade","9 - 11th Grade",..: 5 3 4 5 3 5 1 3 5 1 ...
$ BMI : num 36.5 18.2 27.2 20.6 29.2 24.2 19.3 31.2 27.7 25 ...
$ SBP : int 111 115 95 137 112 123 109 119 110 114 ...
$ DBP : int 72 74 52 75 71 70 73 71 67 74 ...
$ Pulse : int 56 102 98 74 62 80 82 62 68 82 ...
$ PhysActive : Factor w/ 2 levels "No","Yes": 2 2 2 2 2 2 1 1 2 2 ...
$ Smoke100 : Factor w/ 2 levels "No","Yes": 1 2 1 1 2 2 1 1 1 2 ...
$ SleepTrouble: Factor w/ 2 levels "No","Yes": 1 2 1 1 1 1 1 1 1 2 ...
$ HealthGen : Factor w/ 5 levels "Excellent","Vgood",..: 2 3 3 1 3 2 3 3 3 2 ...
To see the first few observations, use head
, and to see the last few, try tail
…
# A tibble: 5 x 13
ID Sex Age Race Education BMI SBP DBP Pulse PhysActive
<int> <fct> <int> <fct> <fct> <dbl> <int> <int> <int> <fct>
1 69692 male 50 Black 9 - 11th~ 22.7 132 82 60 No
2 66472 male 61 White Some Col~ 41.3 141 77 62 No
3 71456 male 21 Mexi~ 9 - 11th~ 26.7 113 66 78 No
4 71420 fema~ 54 Mexi~ 9 - 11th~ 32.5 126 69 68 No
5 63617 male 29 White College ~ 23.2 105 72 76 Yes
# ... with 3 more variables: Smoke100 <fct>, SleepTrouble <fct>,
# HealthGen <fct>
4.2.2 What are the variables?
The variables we have collected are described in the brief table below3.
Variable | Description | Sample Values |
---|---|---|
ID | a numerical code identifying the subject | 64427, 63788 |
Sex | sex of subject (2 levels) | male, female |
Age | age (years) at screening of subject | 37, 40 |
Race | reported race of subject (6 levels) | White, Asian |
Education | educational level of subject (5 levels) | College Grad, High School |
BMI | body-mass index, in kg/m2 | 36.5, 18.2 |
SBP | systolic blood pressure in mm Hg | 111, 115 |
DBP | diastolic blood pressure in mm Hg | 72, 74 |
Pulse | 60 second pulse rate in beats per minute | 56, 102 |
PhysActive | Moderate or vigorous-intensity sports? | Yes, No |
Smoke100 | Smoked at least 100 cigarettes lifetime? | Yes, No |
SleepTrouble | Told a doctor they have trouble sleeping? | Yes, No |
HealthGen | Self-report general health rating (5 lev.) | Vgood, Good |
The levels for the multi-categorical variables are:
- Race: Mexican, Hispanic, White, Black, Asian, or Other.
- Education: 8th Grade, 9 - 11th Grade, High School, Some College, or College Grad.
- HealthGen: Excellent, Vgood, Good, Fair or Poor.
4.3 Types of Variables
4.3.1 Quantitative Variables
Variables recorded in numbers that we use as numbers are called quantitative. Familiar examples include incomes, heights, weights, ages, distances, times, and counts. All quantitative variables have measurement units, which tell you how the quantitative variable was measured. Without units (like miles per hour, angstroms, yen or degrees Celsius) the values of a quantitative variable have no meaning.
It does little good to be promised a salary of 80,000 a year if you don’t know whether it will be paid in Euros, dollars, yen or Estonian kroon.
You might be surprised to see someone whose age is 72 listed in a database on childhood diseases until you find out that age is measured in months.
Often just seeking the units can reveal a variable whose definition is challenging - just how do we measure “friendliness”, or “success,” for example.
Quantitative variables may also be classified by whether they are continuous or can only take on a discrete set of values. Continuous data may take on any value, within a defined range. Suppose we are measuring height. While height is really continuous, our measuring stick usually only lets us measure with a certain degree of precision. If our measurements are only trustworthy to the nearest centimeter with the ruler we have, we might describe them as discrete measures. But we could always get a more precise ruler. The measurement divisions we make in moving from a continuous concept to a discrete measurement are usually fairly arbitrary. Another way to think of this, if you enjoy music, is that, as suggested in Norman and Streiner (2014), a piano is a discrete instrument, but a violin is a continuous one, enabling finer distinctions between notes than the piano is capable of making. Sometimes the distinction between continuous and discrete is important, but usually, it’s not.
- The
nh_adults
data includes several quantitative variables, specifically Age, BMI, SBP, DBP and Pulse. - We know these are quantitative because they have units: Age in years, BMI in kg/m2, the BP measurements in mm Hg, and Pulse in beats per minute.
- Depending on the context, we would likely treat most of these as discrete given that are measurements are fairly crude (this is certainly true for Age, measured in years) although BMI is probably continuous in most settings, even though it is a function of two other measures (Height and Weight) which are rounded off to integer numbers of centimeters and kilograms, respectively.
- The
It is also possible to separate out quantitative variables into ratio variables or interval variables. An interval variable has equal distances between values, but the zero point is arbitrary. A ratio variable has equal intervals between values, and a meaningful zero point. For example, weight is an example of a ratio variable, while IQ is an example of an interval variable. We all know what zero weight is. An intelligence score like IQ is a different matter. We say that the average IQ is 100, but that’s only by convention. We could just as easily have decided to add 400 to every IQ value and make the average 500 instead. Because IQ’s intervals are equal, the difference between and IQ of 70 and an IQ of 80 is the same as the difference between 120 and 130. However, an IQ of 100 is not twice as high as an IQ of 50. The point is that if the zero point is artificial and moveable, then the differences between numbers are meaningful but the ratios between them are not. On the other hand, most lab test values are ratio variables, as are physical characteristics like height and weight. A person who weighs 100 kg is twice as heavy as one who weighs 50 kg; even when we convert kg to pounds, this is still true. For the most part, we can treat and analyze interval or ratio variables the same way.
- Each of the quantitative variables in our
nh_adults
data can be thought of as ratio variables.
- Each of the quantitative variables in our
Quantitative variables lend themselves to many of the summaries we will discuss, like means, quantiles, and our various measures of spread, like the standard deviation or inter-quartile range. They also have at least a chance to follow the Normal distribution.
4.3.2 Qualitative (Categorical) Variables
Qualitative or categorical variables consist of names of categories. These names may be numerical, but the numbers (or names) are simply codes to identify the groups or categories into which the individuals are divided. Categorical variables with two categories, like yes or no, up or down, or, more generally, 1 and 0, are called binary variables. Those with more than two-categories are sometimes called multi-categorical variables.
When the categories included in a variable are merely names, and come in no particular order, we sometimes call them nominal variables. The most important summary of such a variable is usually a table of frequencies, and the mode becomes an important single summary, while the mean and median are essentially useless.
- In the nh_adults data, Race is clearly a nominal variable with multiple unordered categories.
The alternative categorical variable (where order matters) is called ordinal, and includes variables that are sometimes thought of as falling right in between quantitative and qualitative variables.
- Examples of ordinal multi-categorical variables in the
nh_adults
data include the Education and HealthGen variables. - Answers to questions like “How is your overall physical health?” with available responses Excellent, Very Good, Good, Fair or Poor, which are often coded as 1-5, certainly provide a perceived order, but a group of people with average health status 4 (Very Good) is not necessarily twice as healthy as a group with average health status of 2 (Fair).
- Examples of ordinal multi-categorical variables in the
Sometimes we treat the values from ordinal variables as sufficiently scaled to permit us to use quantitative approaches like means, quantiles, and standard deviations to summarize and model the results, and at other times, we’ll treat ordinal variables as if they were nominal, with tables and percentages our primary tools.
Note that all binary variables may be treated as ordinal, or nominal.
- Binary variables in the
nh_adults
data include Sex, PhysActive, Smoke100, SleepTrouble. Each can be thought of as either ordinal or nominal.
- Binary variables in the
Lots of variables may be treated as either quantitative or qualitative, depending on how we use them. For instance, we usually think of age as a quantitative variable, but if we simply use age to make the distinction between “child” and “adult” then we are using it to describe categorical information. Just because your variable’s values are numbers, don’t assume that the information provided is quantitative.
References
Norman, Geoffrey R., and David L. Streiner. 2014. Biostatistics: The Bare Essentials. Fourth. People’s Medical Publishing House.
Vittinghoff, Eric, David V. Glidden, Stephen C. Shiboski, and Charles E. McCulloch. 2012. Regression Methods in Biostatistics: Linear, Logistic, Survival, and Repeated Measures Models. Second. Springer-Verlag, Inc. http://www.biostat.ucsf.edu/vgsm/.
Bock, David E., Paul F. Velleman, and Richard D. De Veaux. 2004. Stats: Modelling the World. Boston MA: Pearson Addison-Wesley.
Descriptions are adapted from the ?NHANES help file. Remember that what NHANES lists as Gender is captured here as Sex, and similarly Race3, BPSysAve and BPDiaAve from NHANES are here listed as Race, SBP and DBP.↩