| Model | Residual df | \(SS_{Res}\) |
|---|---|---|
| A: separate lines | 30 | 1.0029 |
| B: parallel lines | 32 | 1.0796 |
| C: coincident line | 34 | 4.4895 |
14 Indicator (Dummy) Variables
The regressors considered so far have been quantitative: nitrogen dose, rainfall, farm size. Many important factors are qualitative: variety, season, irrigation method (flood or drip), soil type, district, tillage system, sex of an animal. Such factors are included in a regression model through indicator variables, also called dummy variables, which take the values 0 and 1 (Montgomery, Peck, and Vining 2021; Gujarati and Porter 2009; Draper and Smith 1998).
14.1 A qualitative factor with two levels
Suppose rice yield depends on nitrogen dose \(x\) and on the irrigation method, flood or drip. Define
\[D=\begin{cases}0,&\text{flood irrigation}\\1,&\text{drip irrigation}\end{cases}\]
and fit
\[y=\beta_0+\beta_1x+\beta_2D+\varepsilon. \tag{14.1}\]
For flood irrigation (\(D=0\)): \(E(y)=\beta_0+\beta_1x\). For drip irrigation (\(D=1\)): \(E(y)=(\beta_0+\beta_2)+\beta_1x\). The two regression lines are parallel with common slope \(\beta_1\); \(\beta_2\) is the difference in mean yield between drip and flood at any fixed N dose. The level coded 0 is the reference (base) category. A test of \(H_0:\beta_2=0\) is a test of no difference between irrigation methods after adjusting for nitrogen: this is exactly the analysis of covariance.
14.2 A factor with more than two levels
A factor with \(c\) levels requires \(c-1\) indicator variables. For three varieties (Jyothi, Uma, Kanchana) with Jyothi as reference:
| Variety | \(D_1\) | \(D_2\) |
|---|---|---|
| Jyothi (reference) | 0 | 0 |
| Uma | 1 | 0 |
| Kanchana | 0 | 1 |
\[y=\beta_0+\beta_1x+\beta_2D_1+\beta_3D_2+\varepsilon \tag{14.2}\]
gives three parallel lines with intercepts \(\beta_0\) (Jyothi), \(\beta_0+\beta_2\) (Uma) and \(\beta_0+\beta_3\) (Kanchana). \(\beta_2\) and \(\beta_3\) are differences from the reference variety; \(\beta_2-\beta_3\) is the Uma–Kanchana difference.
The dummy variable trap. Using \(c\) indicators for \(c\) levels together with an intercept makes the columns of \(\mathbf X\) linearly dependent (the indicators sum to the column of ones), so \(\mathbf X'\mathbf X\) is singular. Either use \(c-1\) indicators with an intercept, or \(c\) indicators without an intercept. Coding the levels 1, 2, 3 in a single variable is wrong: it forces equal spacing and an arbitrary order on unordered categories.
14.3 Different slopes: interaction terms
If the response to nitrogen differs among varieties, the lines are not parallel. Products of the dummy variables with \(x\) allow different slopes:
\[y=\beta_0+\beta_1x+\beta_2D_1+\beta_3D_2+\beta_4xD_1+\beta_5xD_2+\varepsilon. \tag{14.3}\]
Jyothi: \(E(y)=\beta_0+\beta_1x\); Uma: \((\beta_0+\beta_2)+(\beta_1+\beta_4)x\); Kanchana: \((\beta_0+\beta_3)+(\beta_1+\beta_5)x\). Fitting Equation 14.3 is equivalent to fitting separate regressions for each variety, except that a single pooled estimate of \(\sigma^2\) is used.
14.4 Testing equality of regression lines
Three nested models answer three questions:
| Model | Form | Lines |
|---|---|---|
| A: separate | Equation 14.3 | different intercepts and slopes |
| B: parallel | Equation 14.2 | different intercepts, common slope |
| C: coincident | \(y=\beta_0+\beta_1x+\varepsilon\) | one line for all |
- Parallelism (\(H_0:\beta_4=\beta_5=0\)): compare A and B by the extra sum of squares \(F\) test (Equation 7.12).
- Equal intercepts given parallelism (\(H_0:\beta_2=\beta_3=0\)): compare B and C.
- Coincidence (\(H_0:\beta_2=\cdots=\beta_5=0\)): compare A and C. For two groups this is the Chow test of structural stability, common in econometrics (for example, whether a yield–rainfall relation changed after a new variety was released):
\[F=\frac{\left[SS_{Res}(C)-SS_{Res,1}-SS_{Res,2}\right]/p}{\left(SS_{Res,1}+SS_{Res,2}\right)/(n_1+n_2-2p)}, \tag{14.4}\]
where \(SS_{Res,1}\) and \(SS_{Res,2}\) come from separate regressions in the two groups.
Example 14.1 Yields (t/ha) of three rice varieties at four nitrogen doses with three replications are in csv/variety.csv. Test whether the varieties have parallel and coincident nitrogen response lines and estimate the variety differences.
Solution
Parallelism (A vs B): \(F=\dfrac{(1.0796-1.0029)/2}{1.0029/30}=1.147\) (\(p=0.331\)). Not significant: the varieties respond to nitrogen at the same rate.
Equal intercepts (B vs C): \(F=\dfrac{(4.4895-1.0796)/2}{1.0796/32}=50.53\) (\(p<0.001\)). The varieties differ in yield level.
The parallel-lines model is
\[\hat y=3.010+0.01159N+0.484D_{\text{Uma}}-0.258D_{\text{Kanchana}}.\]
Every kg of N raises yield by 11.6 kg/ha for all varieties; at any dose Uma outyields Jyothi by 0.484 t/ha (\(t=6.46\)) and Kanchana yields 0.258 t/ha less than Jyothi (\(t=-3.45\)).
14.5 Other uses of indicator variables
- Seasonal effects in monthly or quarterly data: 11 (or 3) seasonal dummies capture the seasonal pattern of prices or arrivals.
- Structural breaks: a dummy equal to 1 after a policy change (for example, introduction of minimum support price) and its interaction with time.
- Piecewise linear (spline) regression: to allow a change of slope at a known knot \(x=t\) (for example, a linear-plateau fertilizer response), use \(y=\beta_0+\beta_1x+\beta_2(x-t)D+\varepsilon\) with \(D=1\) if \(x>t\). The line is continuous at \(t\) and its slope changes from \(\beta_1\) to \(\beta_1+\beta_2\).
- Analysis of variance as regression: a one-way ANOVA with \(c\) treatments is a regression on \(c-1\) dummies; the regression \(F\) equals the ANOVA \(F\). Two-way ANOVA and ANCOVA are likewise regressions with dummy variables, which is why unbalanced experiments are analysed with
lm(). - Qualitative response: when \(y\) itself is a 0–1 variable (adopted or not, infested or not), linear regression is inappropriate; logistic regression is used instead.
14.6 R practical
vg <- read.csv("csv/variety.csv")
vg$variety <- factor(vg$variety, levels = c("Jyothi", "Uma", "Kanchana"))
head(model.matrix(~ N + variety, data = vg)) # R creates c - 1 dummies automatically#> (Intercept) N varietyUma varietyKanchana
#> 1 1 0 0 0
#> 2 1 50 0 0
#> 3 1 100 0 0
#> 4 1 150 0 0
#> 5 1 0 0 0
#> 6 1 50 0 0
sep <- lm(yield ~ N * variety, data = vg) # separate lines
par <- lm(yield ~ N + variety, data = vg) # parallel lines
one <- lm(yield ~ N, data = vg) # coincident line
anova(par, sep) # test of parallelism#> Analysis of Variance Table
#>
#> Model 1: yield ~ N + variety
#> Model 2: yield ~ N * variety
#> Res.Df RSS Df Sum of Sq F Pr(>F)
#> 1 32 1.0796
#> 2 30 1.0029 2 0.07669 1.147 0.3311
anova(one, par) # test of equal intercepts#> Analysis of Variance Table
#>
#> Model 1: yield ~ N
#> Model 2: yield ~ N + variety
#> Res.Df RSS Df Sum of Sq F Pr(>F)
#> 1 34 4.4895
#> 2 32 1.0796 2 3.4098 50.533 1.251e-10 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(par)#>
#> Call:
#> lm(formula = yield ~ N + variety, data = vg)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -0.49367 -0.11421 -0.01467 0.05633 0.44883
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 3.0101667 0.0670705 44.881 < 2e-16 ***
#> N 0.0115867 0.0005476 21.158 < 2e-16 ***
#> varietyUma 0.4841667 0.0749872 6.457 2.91e-07 ***
#> varietyKanchana -0.2583333 0.0749872 -3.445 0.00162 **
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 0.1837 on 32 degrees of freedom
#> Multiple R-squared: 0.9449, Adjusted R-squared: 0.9397
#> F-statistic: 182.9 on 3 and 32 DF, p-value: < 2.2e-16
# Change the reference level
summary(lm(yield ~ N + relevel(variety, ref = "Uma"), data = vg))$coef#> Estimate Std. Error t value
#> (Intercept) 3.49433333 0.0670705474 52.099371
#> N 0.01158667 0.0005476287 21.157887
#> relevel(variety, ref = "Uma")Jyothi -0.48416667 0.0749871517 -6.456662
#> relevel(variety, ref = "Uma")Kanchana -0.74250000 0.0749871517 -9.901696
#> Pr(>|t|)
#> (Intercept) 1.624377e-32
#> N 2.226259e-20
#> relevel(variety, ref = "Uma")Jyothi 2.912979e-07
#> relevel(variety, ref = "Uma")Kanchana 2.885391e-11
# Piecewise linear with a knot at 100 kg N
vg$N100 <- pmax(vg$N - 100, 0)
coef(lm(yield ~ N + N100 + variety, data = vg))#> (Intercept) N N100 varietyUma varietyKanchana
#> 2.982685185 0.012411111 -0.002748148 0.484166667 -0.258333333
cols <- c("darkgreen", "steelblue", "firebrick")
plot(yield ~ N, data = vg, pch = 19, col = cols[variety],
xlab = "Nitrogen (kg/ha)", ylab = "Yield (t/ha)")
for (k in 1:3) {
nn <- data.frame(N = c(0, 150), variety = levels(vg$variety)[k])
lines(nn$N, predict(par, nn), col = cols[k], lwd = 2)
}
legend("topleft", levels(vg$variety), col = cols, lwd = 2, bty = "n")
Dummies and the Chow test
Indicator variables were popularized in econometrics by Daniel Suits in a short 1957 note, “Use of dummy variables in regression equations”, which also warned of the dummy variable trap. Gregory Chow’s 1960 test for equality of regression coefficients in two samples, motivated by questions about whether economic relationships changed over time, became one of the most widely used tests in applied economics. In agricultural experiments the same idea had long been used under the name of analysis of covariance, introduced by Fisher in the 1930s.
“Far better an approximate answer to the right question, which is often vague, than an exact answer to the wrong question.”
- John W. Tukey
14.7 Practical exercises
14.7.1 Practical 14.1: Drip versus flood irrigation
Banana bunch weight (kg) at three nitrogen levels (g/plant) under flood and drip irrigation, two plants each, is given below. Using an indicator variable for drip, test whether the response lines are parallel and whether drip gives a higher bunch weight.
| N (g/plant) | 100 | 100 | 200 | 200 | 300 | 300 |
|---|---|---|---|---|---|---|
| Flood | 9.8 | 10.4 | 12.1 | 12.6 | 14.0 | 14.5 |
| Drip | 11.2 | 11.6 | 13.4 | 13.9 | 15.6 | 15.9 |
Solution
Code \(D=1\) for drip and \(D=0\) for flood. The separate-lines model \(y=\beta_0+\beta_1N+\beta_2D+\beta_3ND+\varepsilon\) gives \(SS_{Res}=0.7283\) (8 df); the parallel-lines model gives \(SS_{Res}=0.7483\) (9 df). The parallelism test is \(F=0.220\) on (1, 8) df (\(p=0.652\)): the slopes do not differ, so the parallel model is used:
\[\hat y=7.983+0.02125N+1.367D.\]
At every N level, drip raises bunch weight by 1.37 kg (\(t=8.21\), \(p<0.001\)), and each additional 100 g N adds 2.12 kg under both methods.
Doing it in R
banana <- data.frame(N = rep(rep(c(100, 200, 300), each = 2), 2),
method = factor(rep(c("flood", "drip"), each = 6), levels = c("flood", "drip")),
y = c(9.8, 10.4, 12.1, 12.6, 14.0, 14.5, 11.2, 11.6, 13.4, 13.9, 15.6, 15.9))
sepl <- lm(y ~ N * method, data = banana)
parl <- lm(y ~ N + method, data = banana)
anova(parl, sepl) # test of parallelism#> Analysis of Variance Table
#>
#> Model 1: y ~ N + method
#> Model 2: y ~ N * method
#> Res.Df RSS Df Sum of Sq F Pr(>F)
#> 1 9 0.74833
#> 2 8 0.72833 1 0.02 0.2197 0.6518
summary(parl)$coef # 'methoddrip' is the dummy coefficient#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 7.983333 0.235440223 33.908111 8.323090e-11
#> N 0.021250 0.001019486 20.843836 6.304334e-09
#> methoddrip 1.366667 0.166481378 8.209126 1.800056e-05
anova(lm(y ~ N, data = banana), parl) # test of equal intercepts#> Analysis of Variance Table
#>
#> Model 1: y ~ N
#> Model 2: y ~ N + method
#> Res.Df RSS Df Sum of Sq F Pr(>F)
#> 1 10 6.3517
#> 2 9 0.7483 1 5.6033 67.39 1.8e-05 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
14.8 Chapter Summary
Fill in the blanks
Answers are given at the end of the chapter.
Qualitative regressors are included in regression through __________ variables.
A factor with \(c\) levels requires __________ indicator variables when the model has an intercept.
The level coded 0 on all indicators is the __________ category.
Including \(c\) indicators and an intercept causes the __________.
A model with a dummy but no interaction gives __________ regression lines.
Different slopes for groups are obtained by including __________ of dummy variables with \(x\).
The test of equality of two regression lines is known in econometrics as the __________ test.
A one-way ANOVA with \(c\) treatments is a regression on __________ dummies.
In piecewise linear regression with a knot at \(t\), the extra regressor is __________.
When the response is binary, __________ regression is preferred.
Short-answer questions
Why can a qualitative factor not be coded 1, 2, 3 in a single variable?
Explain the dummy variable trap.
Interpret the coefficient of a dummy variable in a model with one quantitative regressor.
How are different slopes for groups modelled?
Describe the tests of parallelism and coincidence of regression lines.
Explain the Chow test.
How is a one-way ANOVA expressed as a regression?
Explain piecewise linear regression using indicator variables.
Numerical and conceptual questions
Answers are given at the end of the chapter.
A model \(\hat y=2.1+0.015x+0.4D\) is fitted, with \(D=1\) for drip irrigation. Write the lines for flood and drip, and predict yield at \(x=100\) for both.
How many dummies are needed for 5 agro-climatic zones? For 12 months?
In \(\hat y=3+0.02x+0.5D+0.004xD\), find the slope and intercept for \(D=0\) and \(D=1\).
Separate regressions for two periods gave \(SS_{Res,1}=12\) (\(n_1=15\)) and \(SS_{Res,2}=18\) (\(n_2=20\)); the pooled regression gave \(SS_{Res}=45\); \(p=2\). Apply the Chow test.
For a piecewise line \(\hat y=2+0.03x-0.025(x-120)D\), find the slope before and after 120.
Why is \(\beta_2-\beta_3\) in Equation 14.2 the difference between Uma and Kanchana?
Important formulae
Parallel lines with one dummy:
\[ y=\beta_0+\beta_1x+\beta_2D+\varepsilon \]
Separate lines:
\[ y=\beta_0+\beta_1x+\beta_2D+\beta_3xD+\varepsilon \]
Chow test:
\[ F=\frac{[SS_{Res}-SS_{Res,1}-SS_{Res,2}]/p}{(SS_{Res,1}+SS_{Res,2})/(n_1+n_2-2p)} \]
Piecewise linear model:
\[ y=\beta_0+\beta_1x+\beta_2(x-t)D+\varepsilon,\qquad D=1\text{ if }x>t \]
Quick revision
- Qualitative factor with \(c\) levels → \(c-1\) dummies + intercept.
- Dummy alone → shifts intercept (parallel lines); dummy × \(x\) → changes slope.
- Nested models → test parallelism, then equal intercepts (coincidence).
- Chow test → structural stability between two groups or periods.
- ANOVA and ANCOVA are regressions on dummies.
Answers to fill in the blanks
1. Indicator (dummy) 2. \(c-1\) 3. Reference (base) 4. Dummy variable trap 5. Parallel 6. Products (interactions) 7. Chow 8. \(c-1\) 9. \((x-t)D\) 10. Logistic
Solutions to numerical and conceptual questions
Flood: \(\hat y=2.1+0.015x\); drip: \(\hat y=2.5+0.015x\). At \(x=100\): 3.6 and 4.0 t/ha.
4 dummies for 5 zones; 11 for 12 months.
\(D=0\): intercept 3, slope 0.02; \(D=1\): intercept 3.5, slope 0.024.
By Equation 14.4, \(F=\dfrac{(45-12-18)/2}{30/31}=7.75>F_{0.05}(2,31)\approx3.30\); the relationship differs between periods.
Before 120: slope 0.03; after: \(0.03-0.025=0.005\).
Uma mean \(=\beta_0+\beta_1x+\beta_2\) and Kanchana mean \(=\beta_0+\beta_1x+\beta_3\); their difference is \(\beta_2-\beta_3\) at every \(x\).