Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Statistics for Sociologists/Chapter 4: Types of Variables and Levels of Measurement

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Chapter 4: Types of Variables and Levels of Measurement Here is a sentence that is grammatically valid, mathematically executable, and entirely meaningless: "The average religious affiliation in America in 2010 was 2.6, which is about half Catholic and half Jewish." R will compute that number for you without complaint the moment you ask it to. The problem isn't R. The problem is that nobody told R — or, more to the point, nobody told you — that religion isn't the kind of thing you're allowed to average. This chapter is about knowing, before you run analyses in R, what a variable will actually let you do with it. Skip this chapter and you can still write working R code. You just won't be able to trust anything it tells you. Why Variable Type Is the Most Important Thing You'll Learn This Semester Every statistic in this book — every mean, every correlation, every t-test — carries an unstated assumption about the kind of variable it's being applied to. Ignore that assumption and the software will not stop you. R does not know or care whether "religion" is a number you're allowed to average; it just sees numbers and does what numbers do. The judgment about whether that's an appropriate thing to ask of the data has to come from you. This is, not coincidentally, one of the more common ways published research goes wrong. Not fraud, not incompetence — just a mismatch between the kind of variable someone had and the kind of statistic they ran on it. Levels of Measurement Statisticians sort variables into four levels of measurement,Stevens, S. S. (1946). On the theory of scales of measurement. Science, 103(2684), 677–680. sometimes remembered by the acronym NOIR — an appropriately moody name for a topic this dry, like a detective show but with fewer deaths, roughly the same number of false starts, and considerably more decimal places. Nominal Nominal variables are categories with no inherent order. Race, religion, region, sex — you can count how many respondents fall into each category, and you can compute a mode (see Chapter 8), but there is no meaningful sense in which one category is "more" or "less" than another, and there is certainly no meaningful average. Which brings us back to the opening example. GSS's RELIG variable codes Protestant as 1, Catholic as 2, Jewish as 3, and so on. Running mean(RELIG, na.rm = TRUE) will not produce an error. It will produce a number — 2.6, in the 2010 GSS — and that number will describe nothing real about the world. The average religion in America is not "half Catholic and half Jewish." It's an artifact of doing arithmetic on labels that only look like numbers because a codebook had to store them somehow. Importantly, software like R is much better at running analyses on numbers (e.g., 1, 2, 3, 4, etc.) than on values (e.g., Protestant, Jewish, Catholic, etc.). That's why values are stored as numbers and codebooks tell you what the values represent. Ordinal Ordinal variables are categories with a genuine order, but the distance between categories isn't guaranteed to be equal. The GSS's HAPPY variable — very happy, pretty happy, not too happy — is a clear example. "Very happy" is clearly more than "pretty happy," which is clearly more than "not too happy." But is the gap between "very happy" and "pretty happy" the same size as the gap between "pretty happy" and "not too happy"? There's no way to know, and probably no reason to assume so. The values can be put in an order, but the gap is not clear or consistent. Interval Interval variables have equal, meaningful distances between values, but no true zero — zero doesn't mean "none of the thing." Temperature in Celsius is the standard example: the gap between 10° and 20° equals the gap between 20° and 30°, but 0°C doesn't mean "no temperature," it means "the point at which water happens to freeze." True interval variables are genuinely rare in the GSS and in sociology generally; most of what looks interval is actually ordinal data being treated as if it were interval (more on that shortly). Ratio Ratio variables have equal intervals and a true zero, where zero means the complete absence of the thing being measured. Age, years of education, and constant-dollar income (CONRINC) are all ratio variables in the GSS: zero years of education really does mean no formal education, and this is what lets you say something like "person A has twice the years of education as does person B" — a comparison that would be nonsense for interval data, and complete incoherent for nominal or ordinal data. A particularly clean ratio-level example, worth keeping in your back pocket: WORDSUM, a simple count of correct answers on a 10-item vocabulary test. It has equal intervals (the gap between 3 and 4 correct is the same as the gap between 7 and 8), a true zero (zero correct really does mean zero), and none of the ambiguity that complicates a variable like income, where "zero" could mean genuinely no income, a respondent who didn't answer, or a coding artifact depending on which GSS income variable you're looking at. When you want an uncomplicated ratio-level variable to illustrate a concept without also having to explain a bunch of caveats, WORDSUM is often the best choice. The real point of NOIR isn't memorizing four labels. It's this: the level of measurement determines what you're allowed to do with a variable. Means require at least interval-level data. Modes work on anything. Everything you'll learn from Chapter 8 onward assumes you've correctly identified which bucket a variable belongs in before you touch it. alt=An infographic illustrating levels of measurement|center|thumb|800x800px Discrete vs. Continuous Independent of level of measurement, variables are also either discrete (countable, with gaps between possible values — you can have 2 children or 3, never 2.5, no matter how the parent of the 2 looks by dinnertime) or continuous (able to take any value within a range, at least in principle — age is often treated as continuous even though the GSS records it in whole years, and income would be genuinely continuous if it weren't typically collected in bands). This distinction matters most when you get to visualizing distributions in Chapter 7 and modeling relationships in Chapter 16 — discrete and continuous variables tend to call for different kinds of plots and, later, different regression approaches. Independent and Dependent Variables One more distinction belongs in this chapter because you'll need it in every inferential chapter from here to the end of the book: independent variables (predictors, causes, the thing you think is doing the explaining) and dependent variables (outcomes, the thing being explained). If you're asking whether education predicts income, education is your independent variable and income is your dependent variable — X causes, Y merely reacts, which is about as close as mathematics gets to office politics. This distinction is theoretical, not statistical — R has no idea which of your variables you intend as the cause and which as the effect. You decide that based on sociological reasoning, and the statistics you run in Chapters 13 through 16 will test whether the data are consistent with the story you've told, not whether the story itself is correct. A Complication: Treating Ordinal as Interval Here is an uncomfortable, entirely common practice you should know about now rather than discover later and feel betrayed by: sociologists routinely treat ordinal variables — Likert-type scales especially, "strongly disagree" through "strongly agree" — as though they were interval, and compute means, standard deviations, and correlations on them as a matter of course. Strictly, this is a violation of NOIR's rules: a 5-point agreement scale doesn't guarantee that the psychological distance between "agree" and "strongly agree" equals the distance between "disagree" and "agree." Practically, decades of methodological research suggest that treating a reasonably long ordinal scale (five categories or more, roughly symmetric) as interval rarely distorts results badly, and the convenience of being able to compute means and run standard models is considerable.Norman, G. (2010). Likert scales, levels of measurement and the "laws" of statistics. Advances in Health Sciences Education, 15(5), 625–632. This book will do this too, when appropriate, because it reflects how sociology is actually practiced. Just do it knowingly rather than by accident; there's a meaningful difference between an informed simplification and an oversight, even when they produce the same line of code. Checking and Converting Variable Types in R R's own type system doesn't map perfectly onto NOIR, but it gives you the tools to enforce the right behavior once you know what you're aiming for. class(gss$RELIG) str(gss$RELIG) Fresh off a CSV import, a variable like RELIG will almost always show up as integer or numeric — which is exactly the problem, because R now considers it fair game for mean(). Converting it to a factor tells R, explicitly, "this is categorical, treat it accordingly": gss$RELIG <- factor(gss$RELIG, levels = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), labels = c("Protestant", "Catholic", "Jewish", "None", "Other", "Buddhism", "Hinduism", "Other Eastern Religions", "Muslim/Islam", "Orthodox-Christian", "Christian", "Native American", "Inter-Nondenominational")) Now summary(gss$RELIG) returns a count per category instead of a nonsensical mean. R changes its behavior entirely based on the object's declared type, which is the whole point of doing this. If you loaded your data with haven's read_sav() instead, as this course does, you can usually skip typing out levels and labels by hand — the .sav file's own metadata already carries those labels, and haven::as_factor() reads them directly: gss$RELIG <- as_factor(gss$RELIG) This produces the same kind of result — a proper factor with readable category labels — without you having to know or retype the codebook's numeric scheme yourself. It's worth learning the manual factor() approach above regardless, both because you'll encounter CSV-only data eventually and because understanding what as_factor() is doing for you under the hood is exactly the kind of thing that keeps you from being helpless when it does something you don't expect. One classic trap worth flagging before it happens to you: calling as.numeric() directly on a factor does not give you back the original numbers. It gives you R's internal level codes — typically 1, 2, 3... in whatever order the factor's levels happen to be stored — which may or may not match the numbers you started with. This has confused enough generations of R users that it deserves its own paragraph and, frankly, its own support group. If you need the original numeric values back, convert to character first, then to numeric: as.numeric(as.character(x)). Chapter Summary Every variable has a level of measurement — nominal, ordinal, interval, or ratio — and that level determines which statistics are legitimate to run on it, regardless of whether R stops you from running them anyway. Nominal variables (like religion) support counts and modes but not means; ordinal variables (like happiness) add a genuine order but not guaranteed equal spacing; interval and ratio variables (like age and income) support the full range of arithmetic, with ratio variables alone supporting meaningful statements like "twice as much." Variables are separately discrete or continuous, and in any relationship you study, one variable typically plays the independent (predictor) role and another the dependent (outcome) role — a distinction that matters for every inferential chapter ahead. Sociologists commonly treat long ordinal scales as interval for practical purposes; this book does too, but deliberately. In R, class() and str() reveal how a variable is currently stored, and factor() lets you correct it — just watch for the as.numeric()-on-a-factor trap along the way. Practice Examples Example 1: Identify the Level of Measurement The Task: For each of the following GSS variables, identify its level of measurement (nominal, ordinal, interval, or ratio) and briefly justify your answer: RACE, HAPPY, AGE, EDUC, MARITAL. The Code: None required — this is a conceptual exercise. If you want to peek at the actual values first, though: table(gss$RACE) table(gss$HAPPY) table(gss$MARITAL) summary(gss$AGE) summary(gss$EDUC) The Output: (Varies by variable — categorical variables will show a count per category; AGE and EDUC will show a numeric summary.) The Interpretation: RACE and MARITAL are nominal — categories with no inherent order (there is no sense in which "divorced" is more or less than "married"). HAPPY is ordinal — clearly ordered, but without guaranteed equal spacing between categories. AGE and EDUC are ratio — equal intervals, a true zero, and division is meaningful (a 40-year-old is twice the age of a 20-year-old; a 40-year-old with a GED is not automatically anything relative to a 20-year-old with the same, which is a good reminder that "ratio" describes the variable, not every possible comparison you might want to make with it). Follow-Up Challenge: Find one GSS variable not listed here that you find genuinely ambiguous to classify. Make a case for two different levels of measurement it could reasonably be assigned, and explain what would change about how you'd analyze it under each classification. Example 2: Check and Correct Variable Types The Task: Confirm how RELIG is currently stored in R, then convert it to an appropriately labeled factor. The Code: class(gss$RELIG) gss$RELIG <- factor(gss$RELIG, levels = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), labels = c("Protestant", "Catholic", "Jewish", "None", "Other", "Buddhism", "Hinduism", "Other Eastern Religions", "Muslim/Islam", "Orthodox-Christian", "Christian", "Native American", "Inter-Nondenominational")) class(gss$RELIG) summary(gss$RELIG) The Output: > summary(GSS2010$RELIG_F) Protestant Catholic Jewish None Other 973 482 37 363 20 Buddhism Hinduism Other Eastern Religions Muslim/Islam Orthodox-Christian 18 4 3 11 8 Christian Native American Inter-Nondenominational NA's 90 2 20 13 The Interpretation: Before conversion, R considered RELIG a plain integer — mathematically fair game for a mean, which would have been meaningless. After conversion to a factor with proper labels, summary() automatically switches to reporting category counts, and the labels make the output immediately readable without cross-referencing a codebook. Follow-Up Challenge: Try running mean(gss$RELIG) both before and after the factor conversion. What happens the second time, and why is that arguably a better outcome than getting a number back? Example 3: What Happens When You Average a Nominal Variable The Task: Deliberately compute the mean of a nominal variable and observe the result, so you recognize this mistake instantly if you ever see it in the wild — including, potentially, in your own code three weeks from now. The Code: relig_numeric <- as.integer(gss$RELIG) mean(relig_numeric, na.rm = TRUE) The Output: [1] 2.566716 The Interpretation: R obligingly reports that the "average religion" is 2.566716. This number is not wrong in an arithmetic sense — it correctly describes the mean of the underlying codes. It is wrong in every sense that matters, because the codes were never meant to be added together in the first place. The correct move here isn't a different formula; it's a different statistic entirely — a frequency table and a mode, not a mean. Follow-Up Challenge: What should you report instead of a mean for a nominal variable like this? Compute it, and write one sentence describing RELIG's distribution the way you'd actually want it to appear in a paper. References