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.
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, and predict(fit) gives elog(2)+0.3.fitted(fit) gives elog(2)+0.3, and predict(fit) gives log(2)+0.3.fitted(fit) gives 2+0.3, and predict(fit) gives log(2)+e0.3.fitted(fit) gives elog(2)+0.3, and predict(fit) gives e0.3.R Programming Quiz
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.
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.
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.
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, and predict(fit) gives elog(2)+0.3.fitted(fit) gives elog(2)+0.3, and predict(fit) gives log(2)+0.3. (correct answer)fitted(fit) gives 2+0.3, and predict(fit) gives log(2)+e0.3.fitted(fit) gives elog(2)+0.3, and predict(fit) gives e0.3.fitted(fit) always returns values on the response scale — the predicted counts in this case — so it gives elog(2)+0.3. Meanwhile, predict(fit) with no type argument defaults to type = "link", meaning it returns the linear predictor: 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.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.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?
coef(fit) includes a coefficient of 4 for z, and the fitted value is 15.coef(fit) includes a coefficient of 1 for z, and the fitted value is 11.coef(fit) has no estimated coefficient for z, and the fitted value is 11. (correct answer)coef(fit) has no estimated coefficient for z, and the fitted value is 7.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
This confirms C is correct: no estimated coefficient for z, and a fitted value of 11.
A is wrong on both counts — it claims coef(fit) contains a coefficient of 4 for z, but offsets are never estimated and never appear in coef(). The value 4 is the observed value of z, not a coefficient. B similarly invents a coefficient of 1 for z; while 1 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)=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.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?
groupC is the extracted coefficient for group C.groupC coefficient are added. (correct answer)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
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 (10−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 (10) 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+βgroupC⋅XC — and plug in XC=1 for the relevant group. This prevents both the "intercept-only" and "coefficient-only" traps.After fitting fit <- lm(y ~ x, data = d), an analyst needs three numeric outputs: adjusted R2, the estimated slope of x, and the fitted values for the training observations.
Which set of expressions extracts exactly those three outputs?
summary(fit)$adj.r.squared, coef(fit)[["x"]], and fitted(fit) (correct answer)summary(fit)$r.squared, coef(fit)[["x"]], and fitted(fit)summary(fit)$adjusted.r.squared, coef(fit)[["x"]], and residuals(fit)summary(fit)$adj.r.squared, summary(fit)$coefficients["x", 2], and fitted(fit)summary(fit), coef(), and the generic model extractors.
For adjusted R2, 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 y−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.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?
coef(fit)["x"]coef(summary(fit))["x", "Pr(>|t|)"] (correct answer)summary(fit)$coefficients["x", "Estimate"]fitted(fit)["x"]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.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)?
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 x, and a coefficient for x2 — each applied to their respective terms independently.
With coefficients (Intercept) = 1, x = 2, and I($x^2$) = -0.5, the fitted value at x=2 is simply:
y^=1+2(2)+(−0.5)(22)=1+4−2=3
That confirms D is correct.
Now let's see where the wrong answers go astray. A applies the x2 coefficient directly to x rather than squaring first — it computes −0.5(2) instead of −0.5(22), mixing up which term gets squared. B combines the two slope coefficients (2−0.5) and applies them together to x2, 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 2 to x2 and −0.5 to x, 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 x2, not x. Write out y^=β0+β1x+β2x2 explicitly before plugging in numbers to avoid misassignment errors.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?
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. For this row, that gives y^=1+2×3=7. Then residuals(fit) computes the ordinary difference y−y^=8−7=1. So the correct answer is A.
Choice B multiplies the fitted value by the weight (7×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 (7) but then multiplies the residual by the weight (1×3=3), again misapplying where weights matter. Choice D multiplies both x 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.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?
coef(summary(fit))[, "Estimate"]coef(fit)["Estimate", drop = FALSE]coef(summary(fit))[, "Estimate", drop = FALSE] (correct answer)summary(fit)$coefficients["Estimate", ]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.