R Programming Quiz: Linear Models Lm
10 questions · exam conditions
0:00
Linear Models LmQuestion 1 of 10

A data frame has 1212 rows. For lm(y ~ x, data = d), y is missing in rows 22 and 77, while x is missing in rows 77 and 99. A separate variable z, which is not in the formula, is missing in row 1010. Using the default missing-value handling, what residual degrees of freedom should the fitted model have?

66 residual degrees of freedom, because four distinct missing entries each remove a separate row from the fit.
77 residual degrees of freedom, because only rows missing y or x are dropped, leaving nine complete cases from which two coefficients are estimated.
88 residual degrees of freedom, because row 77 is counted twice and the missing z in row 1010 removes that row as well.
99 residual degrees of freedom, because missing predictor values do not trigger row omission, so only rows 22 and 77 are excluded.
← Back to quizzes

R Programming Quiz

R Programming Quiz: Linear Models Lm

Practice Linear Models Lm in R Programming with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.

What this quiz covers

This quiz focuses on Linear Models Lm, giving you a quick way to practice the rules, question types, and explanations that matter most for R Programming.

How to use this quiz

Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.

All questions

Question 1

A data frame has 1212 rows. For lm(y ~ x, data = d), y is missing in rows 22 and 77, while x is missing in rows 77 and 99. A separate variable z, which is not in the formula, is missing in row 1010. Using the default missing-value handling, what residual degrees of freedom should the fitted model have?

  1. 66 residual degrees of freedom, because four distinct missing entries each remove a separate row from the fit.
  2. 77 residual degrees of freedom, because only rows missing y or x are dropped, leaving nine complete cases from which two coefficients are estimated. (correct answer)
  3. 88 residual degrees of freedom, because row 77 is counted twice and the missing z in row 1010 removes that row as well.
  4. 99 residual degrees of freedom, because missing predictor values do not trigger row omission, so only rows 22 and 77 are excluded.
Explanation: Whenever you see a question about lm() and missing data in R, the key concept to recall is na.action = na.omit, which is the default behavior. Under this rule, R drops any row that is missing a value for a variable actually used in the formula — and only those rows. Here, the formula is y ~ x, so R checks columns y and x for NAs. Row 2 is missing y, row 7 is missing both y and x, and row 9 is missing x. That gives three distinct rows removed: rows 2, 7, and 9. The variable z is not part of the formula, so row 10 is untouched. Starting from 12 rows, dropping 3 leaves 9 complete cases. Fitting lm(y ~ x) estimates 2 coefficients (intercept and slope), so residual degrees of freedom =92=7= 9 - 2 = 7. Answer B is correct. Answer A is wrong because it counts four "missing entries" as four dropped rows — but rows 2 and 7 share the same observation, and missing entries, not rows, were counted. Answer C incorrectly includes row 10 (missing z) in the exclusion, and its logic about row 7 being "counted twice" misunderstands how na.omit works: a row is dropped once regardless of how many of its formula variables are NA. Answer D is wrong in the opposite direction — it claims only y-missing rows are dropped, ignoring that missing predictor values (x in row 9) also trigger omission. A useful rule of thumb: R's default na.omit performs casewise (listwise) deletion on formula variables only — count unique rows affected, then subtract coefficients.

Question 2

For a fitted model fit <- lm(y ~ x, data = d), an analyst uses predict(fit, newdata = data.frame(x = 10), interval = "confidence"). The analyst instead needs an interval for a single future response observed at x = 10. Which change and interpretation are appropriate, assuming the usual linear-model conditions?

  1. Keep interval = "confidence"; it already includes future individual-response variation.
  2. Use interval = "prediction"; its center changes, but its interval is generally narrower.
  3. Use interval = "prediction"; its center is the same, but its interval is generally wider. (correct answer)
  4. Keep interval = "confidence"; prediction intervals and confidence intervals are numerically identical.
Explanation: Whenever you see a question about predict() in R, ask yourself: am I estimating the mean response at a given x, or am I predicting one new individual observation? These require different intervals, and confusing them is one of the most common modeling mistakes. A confidence interval (CI) for the mean captures uncertainty about where the true average response falls at x = 10. A prediction interval (PI) captures that same uncertainty plus the natural scatter of individual observations around the mean — represented by the residual variance σ2\sigma^2. Formally, the CI width depends on SE(μ^)SE(\hat{\mu}), while the PI adds σ^2\hat{\sigma}^2 underneath the square root: PI=y^±tσ^1+1n+(xxˉ)2SxxPI = \hat{y} \pm t^* \cdot \hat{\sigma}\sqrt{1 + \frac{1}{n} + \frac{(x^* - \bar{x})^2}{S_{xx}}} Notice the extra "1" inside the square root — that single term is what widens the prediction interval. Critically, both intervals share the same center y^\hat{y}; only the width differs. This makes C correct: switching to interval = "prediction" keeps the point estimate identical but produces a wider interval, as required for a single future observation. A is wrong because a confidence interval does not account for individual-response variability — it only reflects uncertainty about the mean. B is wrong on two counts: the center does not change between CI and PI, and prediction intervals are wider, not narrower, than confidence intervals. D is wrong because the two intervals are numerically distinct; only their centers coincide. As a study tip, remember: "confidence" → mean, "prediction" → individual. Same center, PI is always wider.

Question 3

An analyst fits lm(log(y) ~ x, data = d) and obtains an intercept of 22 and an x coefficient of 0.10.1. According to the fitted equation, what is the effect of increasing x by 33 units on the back-transformed fitted value?

  1. The fitted value increases by 0.30.3 units on the original response scale, because the log-scale change transfers directly.
  2. The fitted value is multiplied by e0.3e^{0.3}, which is approximately 1.351.35. (correct answer)
  3. The fitted value is multiplied by e2.3e^{2.3}, which is approximately 9.979.97, because the intercept shifts as well.
  4. The fitted value increases by 30%30\%, because the log-scale change of 0.30.3 converts directly to a percentage increase.
Explanation: Whenever you fit a log-transformed response model like lm(log(y) ~ x), you need to remember that predictions live on the log scale, and back-transformation requires exponentiation — not simple arithmetic. The fitted equation on the log scale is log(y^)=2+0.1x\log(\hat{y}) = 2 + 0.1x. When you increase xx by 3, the log-scale prediction changes by 0.1×3=0.30.1 \times 3 = 0.3. To understand the multiplicative effect on y^\hat{y} itself, consider two predictions at values x0x_0 and x0+3x_0 + 3: y^new=e2+0.1(x0+3)=e2+0.1x0e0.3=y^olde0.3\hat{y}_{new} = e^{2 + 0.1(x_0 + 3)} = e^{2 + 0.1x_0} \cdot e^{0.3} = \hat{y}_{old} \cdot e^{0.3} The intercept cancels out completely in the ratio. So the fitted value is multiplied by e0.31.35e^{0.3} \approx 1.35, confirming B is correct. A is wrong because a change on the log scale does not transfer additively to the original scale — you must exponentiate. C incorrectly includes the intercept (e2.3e^{2.3}) in the multiplicative effect. Since you're measuring a change between two predictions, the intercept cancels — it shifts both values equally. D tempts you with the "log ≈ percent change" approximation, which only holds for very small changes; a log-scale shift of 0.3 corresponds to a 35% increase (e0.31e^{0.3} - 1), not 30%. As a study tip, always take the ratio of two back-transformed predictions when assessing a unit-change effect in log-linear models — intercepts vanish and only the slope-driven term remains.

Question 4

A model is fit with lm(y ~ x * group), where group has reference level A and comparison level B. The estimated coefficients are: intercept 1010, x 22, groupB 55, and x:groupB 1.5-1.5. For an observation in group B, how much does the fitted response change when x increases from 44 to 77?

  1. It increases by 1.51.5 because the group B slope is 0.50.5. (correct answer)
  2. It increases by 66 because the reference-group slope is 22.
  3. It decreases by 4.54.5 because the interaction coefficient is 1.5-1.5.
  4. It increases by 6.56.5 after adding the group B main effect.
Explanation: When a model includes an interaction term like x * group, the slope of x is not the same for all groups — it depends on which group an observation belongs to. Your job is to find the effective slope for the specific group in question, then multiply by the change in x. For group B, the effective slope of x is the reference-group slope plus the interaction coefficient: 2+(1.5)=0.52 + (-1.5) = 0.5. When x increases from 44 to 77, that's a change of Δx=3\Delta x = 3. The predicted change in response is 0.5×3=1.50.5 \times 3 = 1.5, so the fitted value increases by 1.51.5. That confirms A is correct. Here's why each distractor fails. B uses the reference-group slope of 22 and computes 2×3=62 \times 3 = 6, but that slope applies to group A only — it ignores the interaction term entirely, which is exactly the trap the question is setting. C focuses only on the interaction coefficient 1.5-1.5 and multiplies it by Δx=3\Delta x = 3 to get 4.5-4.5, confusing the adjustment to the slope with the slope itself; the interaction term modifies the slope, it doesn't replace it. D adds the groupB main effect (55) into the slope calculation, but main effects shift the intercept for group B, not the slope — the groupB coefficient is irrelevant when calculating how much the response changes with x. A useful rule of thumb: when computing change in ŷ, intercept-type terms (including group main effects) cancel out. Only terms involving x matter — so always build the effective slope first, then multiply by Δx\Delta x.

Question 5

A factor treatment has levels control, drugA, and drugB, with control as the reference level. For lm(response ~ treatment), the estimated coefficients are: intercept 5050, treatmentdrugA 4-4, and treatmentdrugB 66. If drugB is made the reference level and the model is refitted, which coefficients should result?

  1. Intercept 5656, treatmentcontrol 6-6, and treatmentdrugA 10-10. (correct answer)
  2. Intercept 5050, treatmentcontrol 66, and treatmentdrugA 4-4.
  3. Intercept 5656, treatmentcontrol 66, and treatmentdrugA 4-4.
  4. Intercept 66, treatmentcontrol 5050, and treatmentdrugA 4646.
Explanation: When working with linear models in R, the intercept always represents the mean of the reference level, and each treatment coefficient represents the difference from that reference. Changing the reference level doesn't change the underlying group means — it just re-expresses the same relationships from a different baseline. From the original model, you can recover the three group means directly: control =50= 50, drugA =50+(4)=46= 50 + (-4) = 46, and drugB =50+6=56= 50 + 6 = 56. These are fixed facts about your data. When drugB becomes the reference level, the new intercept must equal drugB's mean: 5656. The coefficient for treatmentcontrol is then controldrugB=5056=6\text{control} - \text{drugB} = 50 - 56 = -6, and treatmentdrugA is 4656=1046 - 56 = -10. That gives you intercept 5656, treatmentcontrol 6-6, treatmentdrugA 10-10 — exactly answer A. Answer B is a trap: it keeps the original intercept of 5050 and coefficients unchanged, which would only be correct if the reference level hadn't changed. Answer C gets the intercept right (5656) but incorrectly reuses the old coefficients without adjusting them to the new baseline — a common half-step error. Answer D appears to confuse coefficients with raw means in a scrambled way, producing nonsensical values that don't correspond to any coherent model. A reliable strategy: always recover the group means first, then rebuild from the new reference. The means never change; only the arithmetic used to express them does. In R, use relevel(treatment, ref = "drugB") before refitting to let R handle this automatically.

Question 6

A model lm(y ~ x1 + x2) is fit using 1010 complete observations. Its residual sum of squares is 1818, and the total sum of squares around the sample mean is 9090. Which pair gives the model's multiple R2R^2 and adjusted R2R^2, respectively?

  1. Multiple R2=0.20R^2=0.20 and adjusted R20.257R^2\approx0.257.
  2. Multiple R2=0.80R^2=0.80 and adjusted R20.775R^2\approx0.775.
  3. Multiple R2=0.80R^2=0.80 and adjusted R20.743R^2\approx0.743. (correct answer)
  4. Multiple R2=0.80R^2=0.80 and adjusted R2=0.80R^2=0.80.
Explanation: Whenever you see a question combining RSS, TSS, and model degrees of freedom, you need two formulas: one for R2R^2 and one for adjusted R2R^2. Multiple R2R^2 measures the proportion of total variation explained by the model: R2=1RSSTSS=11890=10.20=0.80R^2 = 1 - \frac{RSS}{TSS} = 1 - \frac{18}{90} = 1 - 0.20 = 0.80 Adjusted R2R^2 penalizes for the number of predictors pp relative to sample size nn: Radj2=1RSS/(np1)TSS/(n1)R^2_{adj} = 1 - \frac{RSS/(n-p-1)}{TSS/(n-1)} Here, n=10n = 10, p=2p = 2 (two predictors: x1 and x2), so: Radj2=118/(1021)90/(101)=118/790/9=12.5711010.257=0.743R^2_{adj} = 1 - \frac{18/(10-2-1)}{90/(10-1)} = 1 - \frac{18/7}{90/9} = 1 - \frac{2.571}{10} \approx 1 - 0.257 = 0.743 This confirms C is correct. Choice A flips the interpretation entirely — it reports RSS/TSS=0.20RSS/TSS = 0.20 as R2R^2 instead of 1RSS/TSS1 - RSS/TSS, getting both values wrong. Choice B gets R2=0.80R^2 = 0.80 right but miscalculates adjusted R2R^2 as 0.775\approx 0.775, likely by using the wrong degrees of freedom (perhaps npn - p instead of np1n - p - 1 in the denominator). Choice D correctly identifies R2=0.80R^2 = 0.80 but sets adjusted R2R^2 equal to it, ignoring the penalty for predictors entirely. A reliable tip: adjusted R2R^2 is always less than R2R^2 when you have at least one predictor, so any answer where they're equal (like D) should immediately raise a flag.

Question 7

An analyst fits fit <- lm(score ~ hours + prior, data = d). The estimated intercept is 2020, the coefficient of hours is 33, and the coefficient of prior is 0.50.5. Student 1 has hours = 4 and prior = 60; Student 2 has hours = 2 and prior = 68. According to the fitted model, how do their predicted scores compare?

  1. Student 1's predicted score is 22 points higher than Student 2's. (correct answer)
  2. Student 1's predicted score is 1010 points higher than Student 2's.
  3. Student 2's predicted score is 22 points higher than Student 1's.
  4. The two students have equal predicted scores because the predictors offset exactly.
Explanation: When working with multiple regression predictions, your job is to plug each student's values into the fitted equation: y^=β0+β1x1+β2x2\hat{y} = \beta_0 + \beta_1 x_1 + \beta_2 x_2. Here, that means y^=20+3(hours)+0.5(prior)\hat{y} = 20 + 3(\text{hours}) + 0.5(\text{prior}). For Student 1: 20+3(4)+0.5(60)=20+12+30=6220 + 3(4) + 0.5(60) = 20 + 12 + 30 = 62. For Student 2: 20+3(2)+0.5(68)=20+6+34=6020 + 3(2) + 0.5(68) = 20 + 6 + 34 = 60. Student 1 scores 2 points higher, confirming A is correct. The traps in the wrong answers are worth understanding. B claims a 10-point difference — this likely comes from only comparing the hours effect: Student 1 studies 2 more hours, so 3×2=63 \times 2 = 6 extra points from hours, but you must also account for the prior difference. Student 2 has 8 more prior points, contributing 0.5×8=40.5 \times 8 = 4 extra points, which partially offsets Student 1's advantage: 64=26 - 4 = 2, not 10. C reverses the direction — Student 2 does benefit more from prior, but not enough to overcome Student 1's larger hours advantage. D is tempting because the predictors do partially offset each other, but "exactly" is wrong; the net difference is 2 points, not zero. A useful habit: when comparing two observations, calculate the difference in each predictor, multiply by its coefficient, then sum. Here, 3(42)+0.5(6068)=64=23(4-2) + 0.5(60-68) = 6 - 4 = 2. This shortcut saves time and reduces arithmetic errors.

Question 8

An analyst fits fit <- lm(y ~ x + z, data = d) and obtains the fitted equation y^=4+2x3z\hat{y}=4+2x-3z. For one row, x=5x=5, z=1z=1, and the observed response is 1818. What are the fitted value and ordinary residual for that row?

  1. The fitted value is 1818, and the residual is 00.
  2. The fitted value is 1111, and the residual is 7-7.
  3. The fitted value is 1717, and the residual is 11.
  4. The fitted value is 1111, and the residual is 77. (correct answer)
Explanation: When working with linear regression output in R, you need to distinguish between two separate calculations: the fitted value (what the model predicts) and the residual (how far off that prediction is from reality). To find the fitted value, simply plug the predictor values into the estimated equation. With y^=4+2x3z\hat{y} = 4 + 2x - 3z, substituting x=5x = 5 and z=1z = 1 gives: y^=4+2(5)3(1)=4+103=11\hat{y} = 4 + 2(5) - 3(1) = 4 + 10 - 3 = 11 The residual is then computed as observed minus fitted: e=yy^=1811=7e = y - \hat{y} = 18 - 11 = 7 So the fitted value is 11 and the residual is 7, making D the correct answer. Choice A is tempting if you confuse the fitted value with the observed value — the fitted value is what the model predicts, not what was actually measured. Choice B gets the fitted value right (11) but flips the sign of the residual by computing y^y\hat{y} - y instead of yy^y - \hat{y}. This is a very common sign error. Choice C likely results from an arithmetic mistake — perhaps forgetting to subtract zz's contribution correctly and arriving at 17 instead of 11. A useful memory aid: residual = Real minus Regression (observed minus fitted). The sign matters — a positive residual means the model underpredicted, and a negative residual means it overpredicted. On regression questions in R, always double-check your plug-in arithmetic and keep the subtraction order straight.

Question 9

In every row of a data frame, x2 equals exactly twice x1. An analyst fits lm(y ~ x1 + x2, data = d). Which outcome best describes the fitted model?

  1. Both slopes are uniquely estimated, because lm() automatically standardizes correlated predictors before solving the normal equations.
  2. All coefficients are forced to zero because exact linear dependence makes the least-squares criterion undefined and no solution is returned.
  3. The model cannot estimate an intercept because exact collinearity between the predictors absorbs its degree of freedom, but both predictor slopes remain uniquely estimable.
  4. At least one slope is not separately estimable due to linear dependence in the model matrix, though fitted values can still be produced from an equivalent reduced-rank solution. (correct answer)
Explanation: Whenever you see a question involving predictors with an exact linear relationship, your first instinct should be to think about the rank of the model matrix and what that means for identifiability of coefficients. Here, since x2=2x1x_2 = 2x_1 in every row, the model matrix columns are perfectly collinear. R's lm() detects this rank deficiency and drops one redundant predictor (typically x2), setting its coefficient to NA. This means the two slopes cannot be separately identified — infinitely many combinations of β1\beta_1 and β2\beta_2 satisfying β1+2β2=c\beta_1 + 2\beta_2 = c produce identical fitted values. The model still runs and still produces fitted values (and predictions) using the reduced-rank solution, which is exactly what D describes. A is wrong because lm() does not automatically standardize predictors before solving the normal equations. Even if it did, standardizing perfectly collinear columns wouldn't break the dependence — they'd still be proportional. B is wrong because lm() does return a solution; it simply drops the linearly dependent column and estimates the remaining coefficients. Coefficients are not forced to zero, and no error is thrown — just a warning and an NA for the dropped term. C is wrong because the intercept is unaffected by collinearity among predictors. The intercept is estimated normally; it's the slopes that become non-uniquely estimable, not the intercept. Study tip: When you see "exact collinearity" in a question, remember the key consequence: fitted values are still valid, but individual slopes are not separately identifiable. Always check summary(model) for NA coefficients as your diagnostic signal.

Question 10

Suppose group has levels A and B, and an analyst fits lm(y ~ 0 + group + x). The estimated coefficients are groupA 1010, groupB 1515, and x 22. Which interpretation is correct?

  1. The group coefficients equal unadjusted sample means, so the group difference of 55 applies only at x=0x=0.
  2. At x=0x=0 the fitted values are 1010 and 1515, and their difference of 55 is constant for every value of xx. (correct answer)
  3. Removing the global intercept forces group A's fitted value to zero at every xx, while group B starts at 1515.
  4. The groupB coefficient is added to groupA, so group B's baseline fitted value is 10+15=2510+15=25.
Explanation: When you see lm(y ~ 0 + group + x) in R, recognize that suppressing the global intercept with 0 causes R to estimate a separate intercept for each factor level rather than one shared baseline. Each group coefficient becomes that group's fitted value of yy when x=0x = 0, and the slope on x applies equally to both groups. With coefficients groupA =10= 10, groupB =15= 15, and x =2= 2, the fitted value equations are: y^A=10+2xy^B=15+2x\hat{y}_A = 10 + 2x \qquad \hat{y}_B = 15 + 2x At x=0x = 0, group A fits to 1010 and group B fits to 1515. Because both lines share the same slope of 22, their vertical distance stays at 1510=515 - 10 = 5 for every value of xx. That makes B the correct interpretation. A is wrong because the group coefficients here are adjusted baselines (controlling for xx), not raw sample means — and the claim that the difference "applies only at x=0x = 0" is misleading; since the slope is shared, the difference is constant everywhere. C is wrong because removing the intercept does not force group A's fitted value to zero — it simply gives each group its own intercept estimated from the data. D reflects a confusion with treatment-coded (dummy) models, where one group's coefficient is added to a reference intercept; in a no-intercept model, groupA and groupB are independent, not cumulative. As a quick rule of thumb: in ~ 0 + factor, every level gets its own intercept — they are never added together.