R Programming Quiz: Extracting Model Outputs
8 questions · exam conditions
0:00
Extracting Model OutputsQuestion 1 of 8

A Poisson model with a log link is fitted using fit <- glm(count ~ x, family = poisson(link = "log"), data = d). The extracted coefficients are (Intercept) = log(2) and x = 0.3. One training observation has x = 1.

Which pair correctly gives the fitted value from fitted(fit) and the default value from predict(fit) for that observation?

fitted(fit) gives log(2)+0.3\log(2)+0.3, and predict(fit) gives elog(2)+0.3e^{\log(2)+0.3}.
fitted(fit) gives elog(2)+0.3e^{\log(2)+0.3}, and predict(fit) gives log(2)+0.3\log(2)+0.3.
fitted(fit) gives 2+0.32+0.3, and predict(fit) gives log(2)+e0.3\log(2)+e^{0.3}.
fitted(fit) gives elog(2)+0.3e^{\log(2)}+0.3, and predict(fit) gives e0.3e^{0.3}.
← Back to quizzes

R Programming Quiz

R Programming Quiz: Extracting Model Outputs

Practice Extracting Model Outputs 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 Extracting Model Outputs, 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 Poisson model with a log link is fitted using fit <- glm(count ~ x, family = poisson(link = "log"), data = d). The extracted coefficients are (Intercept) = log(2) and x = 0.3. One training observation has x = 1.

Which pair correctly gives the fitted value from fitted(fit) and the default value from predict(fit) for that observation?

  1. fitted(fit) gives log(2)+0.3\log(2)+0.3, and predict(fit) gives elog(2)+0.3e^{\log(2)+0.3}.
  2. fitted(fit) gives elog(2)+0.3e^{\log(2)+0.3}, and predict(fit) gives log(2)+0.3\log(2)+0.3. (correct answer)
  3. fitted(fit) gives 2+0.32+0.3, and predict(fit) gives log(2)+e0.3\log(2)+e^{0.3}.
  4. fitted(fit) gives elog(2)+0.3e^{\log(2)}+0.3, and predict(fit) gives e0.3e^{0.3}.
Explanation: Whenever you work with GLMs in R, you need to track two distinct scales: the linear predictor (the raw output of β0+β1x\beta_0 + \beta_1 x) and the response scale (the predicted mean after applying the inverse link function). For this Poisson model with a log link, the linear predictor at x=1x = 1 is log(2)+0.31=log(2)+0.3\log(2) + 0.3 \cdot 1 = \log(2) + 0.3. The inverse of the log link is the exponential, so the response-scale prediction is elog(2)+0.3e^{\log(2)+0.3}. fitted(fit) always returns values on the response scale — the predicted counts in this case — so it gives elog(2)+0.3e^{\log(2)+0.3}. Meanwhile, predict(fit) with no type argument defaults to type = "link", meaning it returns the linear predictor: log(2)+0.3\log(2) + 0.3. This makes B correct. Choice A has the two scales swapped — it assigns the linear predictor to fitted() and the response to predict(), which is exactly backwards. Choice C invents an incorrect formula (2+0.32 + 0.3) by misapplying the intercept as if it weren't inside a logarithm, and the expression for predict() has no mathematical basis in this model. Choice D splits the exponential incorrectly across the two coefficients — the inverse link applies to the entire linear predictor as a sum, not term by term. A handy rule: fitted() = response scale, predict() default = link scale. If you want predict() on the response scale, you must explicitly write predict(fit, type = "response"). Memorize this distinction — it's a frequent trap in R GLM questions.

Question 2

An offset model is fitted with fit <- lm(y ~ x + offset(z), data = d). The extracted coefficients are (Intercept) = 1 and x = 2. For one training observation, x = 3 and z = 4.

Which statement correctly describes the coefficient and fitted-value outputs for this observation?

  1. coef(fit) includes a coefficient of 44 for z, and the fitted value is 1515.
  2. coef(fit) includes a coefficient of 11 for z, and the fitted value is 1111.
  3. coef(fit) has no estimated coefficient for z, and the fitted value is 1111. (correct answer)
  4. coef(fit) has no estimated coefficient for z, and the fitted value is 77.
Explanation: When you use offset() in a linear model, you're telling R to include a variable with a fixed coefficient of 1 rather than estimating one from the data. This is fundamentally different from a regular predictor — the offset shifts predictions without consuming a degree of freedom or appearing in coef(). Because z is wrapped in offset(), R never estimates a coefficient for it. So coef(fit) returns only the intercept and the slope for x — no entry for z at all. The fitted value for any observation is computed as: y^=β0+β1x+z=1+2(3)+4=1+6+4=11\hat{y} = \beta_0 + \beta_1 x + z = 1 + 2(3) + 4 = 1 + 6 + 4 = 11 This confirms C is correct: no estimated coefficient for z, and a fitted value of 1111. A is wrong on both counts — it claims coef(fit) contains a coefficient of 44 for z, but offsets are never estimated and never appear in coef(). The value 44 is the observed value of z, not a coefficient. B similarly invents a coefficient of 11 for z; while 11 is indeed the implicit multiplier an offset uses, R does not report it as an estimated coefficient in coef(). D gets the coefficient story right — no entry for z — but miscalculates the fitted value by omitting the offset entirely: 1+2(3)=71 + 2(3) = 7 ignores z's contribution. A good rule of thumb: offset() means "fixed at 1, not reported." Whenever you see offset(z) in a model formula, expect coef() to be silent about it, but always include z in your fitted-value calculation.

Question 3

A factor is defined as group <- factor(group, levels = c("A", "B", "C")), and the model fit <- lm(y ~ group, data = d) is fitted with default treatment contrasts. coef(fit) returns (Intercept) = 10, groupB = -2, and groupC = 3.

Ignoring any other predictors, what fitted value does fitted(fit) assign to an observation in group C?

  1. 33, because groupC is the extracted coefficient for group C.
  2. 77, because the group coefficient is subtracted from the intercept.
  3. 1010, because the intercept is used for every factor level.
  4. 1313, because the intercept and the groupC coefficient are added. (correct answer)
Explanation: Whenever you encounter a question about fitted values in a linear model with factor predictors, your first instinct should be to recall how treatment (dummy) coding works: every fitted value is built by adding the intercept to the relevant group coefficient. In treatment contrasts, R automatically designates one factor level as the reference group — here, group A (the first level alphabetically). The intercept represents the mean of that reference group. Each subsequent coefficient represents the difference from that reference. So for an observation in group C, the fitted value is: y^C=Intercept+groupC=10+3=13\hat{y}_C = \text{Intercept} + \text{groupC} = 10 + 3 = 13 That makes D the correct answer. A is wrong because groupC = 3 alone is just the difference from group A, not the actual predicted value. Ignoring the intercept gives you an incomplete picture. B is wrong because it subtracts the coefficient from the intercept (103=710 - 3 = 7), which has no basis in the model formula. There is no subtraction happening here — the coefficient is simply added. C is wrong because the intercept (1010) applies only to the reference group (A). Saying the intercept is used for every level conflates the baseline with the full prediction for non-reference groups. A useful rule of thumb: always write out the model equation explicitly — y^=β0+βgroupCXC\hat{y} = \beta_0 + \beta_{\text{groupC}} \cdot X_C — and plug in XC=1X_C = 1 for the relevant group. This prevents both the "intercept-only" and "coefficient-only" traps.

Question 4

After fitting fit <- lm(y ~ x, data = d), an analyst needs three numeric outputs: adjusted R2R^2, the estimated slope of x, and the fitted values for the training observations.

Which set of expressions extracts exactly those three outputs?

  1. summary(fit)$adj.r.squared, coef(fit)[["x"]], and fitted(fit) (correct answer)
  2. summary(fit)$r.squared, coef(fit)[["x"]], and fitted(fit)
  3. summary(fit)$adjusted.r.squared, coef(fit)[["x"]], and residuals(fit)
  4. summary(fit)$adj.r.squared, summary(fit)$coefficients["x", 2], and fitted(fit)
Explanation: When working with fitted linear models in R, you need to know which extractor functions and named list elements pull out specific summaries. This question tests your familiarity with summary(fit), coef(), and the generic model extractors. For adjusted R2R^2, the correct field inside summary(fit) is $adj.r.squared — this is the exact name R uses internally. The estimated slope for x is cleanly retrieved with coef(fit)[["x"]], which indexes the named coefficient vector by predictor name. Finally, fitted(fit) returns the vector of predicted \hat{y} values on the training data. Together, these three expressions make A the correct and complete set. Here's why the distractors fail. B retrieves $r.squared instead of $adj.r.squared — that gives you the ordinary R^2 , not the adjusted version, which penalizes for extra predictors. C uses $adjusted.r.squared, which simply doesn't exist as a field name in R's summary output — it will return NULL rather than an error, making it a silent bug. C also swaps fitted(fit) for residuals(fit), which returns yy^y - \hat{y}, not the fitted values themselves. D uses summary(fit)$coefficients["x", 2], which extracts the standard error of the slope (column 2 of the coefficients table), not the estimate (column 1) — a subtle but meaningful distinction. As a study tip, memorize the exact field names in summary(lm(...)): $r.squared, $adj.r.squared, and $coefficients. When you need just the point estimate of a coefficient, prefer coef() over indexing the summary table to avoid column-order confusion.

Question 5

An analyst fits fit <- lm(y ~ x + z, data = d). The coefficient table returned by summary(fit) has rows (Intercept), x, and z, and columns Estimate, Std. Error, t value, and Pr(>|t|).

Which expression extracts only the p-value for the coefficient of x?

  1. coef(fit)["x"]
  2. coef(summary(fit))["x", "Pr(>|t|)"] (correct answer)
  3. summary(fit)$coefficients["x", "Estimate"]
  4. fitted(fit)["x"]
Explanation: When working with linear model output in R, it helps to think in layers: lm() stores a model object, summary() computes inferential statistics from it, and subsetting functions like coef() or $ extract specific pieces. The p-values live in the summary layer, not the model layer itself. To grab the p-value for x, you need to reach into the coefficient matrix that summary() produces. That matrix is accessible via summary(fit)$coefficients (or equivalently coef(summary(fit))), and it has both row names (one per predictor) and column names including "Pr(>|t|)". So coef(summary(fit))["x", "Pr(>|t|)"] pinpoints exactly the cell at row "x" and the p-value column — making B correct. Here's why the other options miss the mark. A uses coef(fit), which extracts coefficients from the model object itself — this returns only the point estimates (the Estimate column), not any inferential statistics. You'd just get a single number like 2.34, not a p-value. C also accesses the right summary matrix, but requests the "Estimate" column instead of "Pr(>|t|)" — so it returns the coefficient estimate, not the p-value. It's a near-miss that tests whether you know the column names. D uses fitted(fit), which returns the model's fitted values (predicted ŷ for each observation in the dataset) — completely unrelated to coefficient-level statistics. A handy tip: whenever you need anything beyond point estimates — standard errors, t-statistics, or p-values — always go through summary(fit)$coefficients and double-bracket with both row and column names.

Question 6

An analyst fits fit <- lm(y ~ x + I($x^2$), data = d). The extracted coefficients are (Intercept) = 1, x = 2, and I($x^2$) = -0.5.

For a training observation with x = 2, which value should appear in fitted(fit)?

  1. 1+2(2)0.5(2)=41+2(2)-0.5(2)=4
  2. 1+(20.5)(22)=71+(2-0.5)(2^2)=7
  3. 1+2(22)0.5(2)=81+2(2^2)-0.5(2)=8
  4. 1+2(2)0.5(22)=31+2(2)-0.5(2^2)=3 (correct answer)
Explanation: When working with polynomial regression in R, the key is understanding how lm() interprets each term in the formula and how those terms map to the prediction equation. The model y ~ x + I($x^2$) fits three parameters: an intercept, a coefficient for xx, and a coefficient for x2x^2 — each applied to their respective terms independently. With coefficients (Intercept) = 1, x = 2, and I($x^2$) = -0.5, the fitted value at x=2x = 2 is simply: y^=1+2(2)+(0.5)(22)=1+42=3\hat{y} = 1 + 2(2) + (-0.5)(2^2) = 1 + 4 - 2 = 3 That confirms D is correct. Now let's see where the wrong answers go astray. A applies the x2x^2 coefficient directly to xx rather than squaring first — it computes 0.5(2)-0.5(2) instead of 0.5(22)-0.5(2^2), mixing up which term gets squared. B combines the two slope coefficients (20.5)(2 - 0.5) and applies them together to x2x^2, treating the model as if it had a single compound coefficient rather than two separate terms — a structural misreading of the formula. C swaps the roles of the coefficients entirely, applying 22 to x2x^2 and 0.5-0.5 to xx, which reverses the model structure. The study tip here: always match each coefficient to its exact term. In R, I($x^2$) creates a standalone column of squared values — its coefficient multiplies x2x^2, not xx. Write out y^=β0+β1x+β2x2\hat{y} = \beta_0 + \beta_1 x + \beta_2 x^2 explicitly before plugging in numbers to avoid misassignment errors.

Question 7

A weighted regression is fitted with fit <- lm(y ~ x, data = d, weights = w). The extracted coefficients are (Intercept) = 1 and x = 2. One training row has x = 3, y = 8, and weight w = 9.

What values do fitted(fit) and residuals(fit) contain for this row?

  1. The fitted value is 77, and the residual is 11. (correct answer)
  2. The fitted value is 6363, and the residual is 55-55.
  3. The fitted value is 77, and the residual is 33.
  4. The fitted value is 2121, and the residual is 13-13.
Explanation: When working with weighted regression in R, it's crucial to understand what the weights actually affect — and what they don't. Weights in lm() influence how much each observation contributes to estimating the coefficients, but they do not scale the fitted values or raw residuals themselves. Once the model is fitted, fitted(fit) simply plugs each row's predictor values into the regression equation: y^=Intercept+slope×x\hat{y} = \text{Intercept} + \text{slope} \times x. For this row, that gives y^=1+2×3=7\hat{y} = 1 + 2 \times 3 = 7. Then residuals(fit) computes the ordinary difference yy^=87=1y - \hat{y} = 8 - 7 = 1. So the correct answer is A. Choice B multiplies the fitted value by the weight (7×9=637 \times 9 = 63) and computes a correspondingly distorted residual. This confuses the role of weights — they enter the loss function during estimation, not the prediction step. Choice C gets the fitted value right (77) but then multiplies the residual by the weight (1×3=31 \times 3 = 3), again misapplying where weights matter. Choice D multiplies both xx and the fitted value by the weight before computing the residual, compounding the same misconception. A helpful rule of thumb: weights change how the line is fit, not what the line predicts. After fitting, fitted() and residuals() behave identically to unweighted regression — it's only functions like weighted.residuals() or the internal WLS math that incorporate the weights explicitly. Keep this distinction clear and you'll avoid all three traps above.

Question 8

For fit <- lm(y ~ x, data = d), coef(summary(fit)) is a matrix with rows (Intercept) and x, and columns including Estimate. A later function requires the estimates to remain a one-column matrix rather than becoming a vector.

Which expression extracts the estimates in the required form?

  1. coef(summary(fit))[, "Estimate"]
  2. coef(fit)["Estimate", drop = FALSE]
  3. coef(summary(fit))[, "Estimate", drop = FALSE] (correct answer)
  4. summary(fit)$coefficients["Estimate", ]
Explanation: When subsetting a matrix in R, you need to understand how the drop argument controls whether dimensions are preserved. By default, extracting a single row or column from a matrix drops that dimension, returning a named vector instead of a one-column (or one-row) matrix. When a downstream function strictly requires matrix input, you must suppress this behavior with drop = FALSE. coef(summary(fit)) returns a matrix where rows are predictor terms and columns are "Estimate", "Std. Error", "t value", and "Pr(>|t|)". Option C, coef(summary(fit))[, "Estimate", drop = FALSE], correctly selects the "Estimate" column while keeping the result as a matrix — exactly what the question requires. Option A, coef(summary(fit))[, "Estimate"], is the most tempting distractor. It extracts the same values, but without drop = FALSE, R collapses the result into a plain named vector. This breaks any function expecting a matrix. Option B, coef(fit)["Estimate", drop = FALSE], has two problems: coef(fit) returns a simple named vector of coefficients (not a matrix), and "Estimate" is not a valid index for it — the names are "(Intercept)" and "x". The drop = FALSE argument is also irrelevant when subsetting a vector. Option D, summary(fit)$coefficients["Estimate", ], attempts to subset a row named "Estimate", but no such row exists — "Estimate" is a column name, not a row name. A good habit: whenever matrix structure must be preserved through subsetting, always reach for drop = FALSE as your default defensive practice.