R Programming Quiz: Logistic Regression Glm
10 questions · exam conditions
0:00
Logistic Regression GlmQuestion 1 of 10

A logistic model for a positive outcome has linear predictor 1.20.4x1.2-0.4x. An observation is classified as positive when its fitted probability is at least 0.50.5.

Which values of xx will be classified as positive under this rule?

Values satisfying x2x \leq 2, because the probability cutoff of 0.50.5 is substituted directly into the linear predictor before solving.
Values satisfying x3x \leq 3, because the logit link maps a probability of 0.50.5 to a linear predictor of 00, and solving 1.20.4x01.2 - 0.4x \geq 0 yields this bound.
Values satisfying x4x \geq 4, because the classification threshold requires the log-odds to exceed 11.
Values satisfying x1.2x \leq 1.2, because the intercept term sets the effective boundary for classification.
← Back to quizzes

R Programming Quiz

R Programming Quiz: Logistic Regression Glm

Practice Logistic Regression Glm 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 Logistic Regression Glm, 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 logistic model for a positive outcome has linear predictor 1.20.4x1.2-0.4x. An observation is classified as positive when its fitted probability is at least 0.50.5.

Which values of xx will be classified as positive under this rule?

  1. Values satisfying x2x \leq 2, because the probability cutoff of 0.50.5 is substituted directly into the linear predictor before solving.
  2. Values satisfying x3x \leq 3, because the logit link maps a probability of 0.50.5 to a linear predictor of 00, and solving 1.20.4x01.2 - 0.4x \geq 0 yields this bound. (correct answer)
  3. Values satisfying x4x \geq 4, because the classification threshold requires the log-odds to exceed 11.
  4. Values satisfying x1.2x \leq 1.2, because the intercept term sets the effective boundary for classification.
Explanation: Whenever you see a logistic regression classification question, your first instinct should be to think about the logit link function — the bridge between probabilities and the linear predictor. In logistic regression, the fitted probability is p^=11+eη\hat{p} = \frac{1}{1 + e^{-\eta}}, where η\eta is the linear predictor. The key insight is that p^0.5\hat{p} \geq 0.5 if and only if η0\eta \geq 0. This is because the logistic function equals exactly 0.50.5 when its argument is 00, and is increasing throughout. So the classification rule p^0.5\hat{p} \geq 0.5 translates cleanly to η0\eta \geq 0 — no messy algebra with exponentials required. Applying this to your linear predictor: you need 1.20.4x01.2 - 0.4x \geq 0, which gives 1.20.4x1.2 \geq 0.4x, so x3x \leq 3. That confirms B is correct. A commits a conceptual error by substituting 0.50.5 directly into the linear predictor as if it were a simple linear model — it ignores the logit transformation entirely and arrives at the wrong boundary. C claims the log-odds must exceed 11, which is incorrect. The threshold probability of 0.50.5 corresponds to a log-odds of 00, not 11, and the inequality runs the wrong direction anyway. D treats the intercept alone as the classification boundary, ignoring the role of xx and the coefficient 0.4-0.4 in shifting that boundary. Study tip: Always remember that in logistic regression, p^=0.5    η=0\hat{p} = 0.5 \iff \eta = 0. This one fact lets you convert any probability-based classification rule into a simple linear inequality instantly.

Question 2

A model is fitted with glm(y ~ hours * group, family = binomial, data = d). Group A is the reference level. The estimated coefficient for hours is 0.50.5, and the estimated coefficient for hours:groupB is 0.7-0.7.

What is the estimated effect of one additional hour for observations in group B?

  1. Their log-odds increase by 0.50.5, giving an odds ratio of about 1.651.65.
  2. Their log-odds decrease by 0.20.2, giving an odds ratio of about 0.820.82. (correct answer)
  3. Their log-odds decrease by 0.70.7, giving an odds ratio of about 0.500.50.
  4. Their log-odds increase by 1.21.2, giving an odds ratio of about 3.323.32.
Explanation: When a glm includes an interaction term like hours * group, the coefficient for hours alone represents the effect of one additional hour only for the reference group (here, group A). For any other group, you must add the relevant interaction coefficient to get the true slope for that group. For group B specifically, the effect of one additional hour on the log-odds is: 0.5+(0.7)=0.20.5 + (-0.7) = -0.2 This means one additional hour decreases the log-odds by 0.20.2 for group B observations. Converting to an odds ratio: e0.20.82e^{-0.2} \approx 0.82, confirming answer B is correct. Choice A applies the hours coefficient (0.50.5) as if group B behaves identically to group A — this ignores the interaction term entirely, which is the most common mistake students make with interaction models. Choice C treats the interaction coefficient (0.7-0.7) as the complete effect for group B, forgetting that the base slope for hours still applies and must be included. Choice D adds the two coefficients incorrectly as positives: 0.5+0.7=1.20.5 + 0.7 = 1.2, suggesting the student may have misread the sign of the interaction term or mistakenly treated it as reinforcing rather than opposing the base effect. A reliable strategy: whenever you see an interaction term in a regression model, mentally build a "slope formula" for each group — start with the base coefficient, then add any interaction terms that apply to that group. This keeps you from accidentally using partial information, which is exactly the trap this question is designed to set.

Question 3

An analyst fits glm(churn ~ plan, family = binomial, data = customers). The factor plan has reference level basic. The estimated coefficients are planpremium = -0.8 and planstandard = 0.3.

Holding all else fixed, what is the estimated odds ratio for churn comparing premium with standard?

  1. Approximately 0.330.33 because the relevant log-odds difference is 1.1-1.1. (correct answer)
  2. Approximately 0.450.45 because only the premium coefficient should be exponentiated.
  3. Approximately 2.232.23 because the standard coefficient is the comparison baseline.
  4. Approximately 3.003.00 because the relevant log-odds difference is 1.11.1.
Explanation: When a logistic regression includes a multi-level factor, each coefficient represents a log-odds difference relative to the reference level — not relative to each other. To compare two non-reference levels directly, you must subtract their coefficients to find the log-odds difference between them. Here, basic is the reference. The model gives you:
  • log-odds(premium vs. basic)=0.8\log\text{-odds}(\text{premium vs. basic}) = -0.8
  • log-odds(standard vs. basic)=0.3\log\text{-odds}(\text{standard vs. basic}) = 0.3
To compare premium directly against standard, subtract: 0.80.3=1.1-0.8 - 0.3 = -1.1. Exponentiating gives the odds ratio: e1.10.33e^{-1.1} \approx 0.33. This means premium customers have roughly 33% of the churn odds of standard customers — confirming A is correct. B is wrong because it exponentiates only the premium coefficient (e0.80.45e^{-0.8} \approx 0.45), ignoring that this compares premium to basic, not to standard. You haven't accounted for standard's position at all. C is wrong because it treats standard as a new baseline and uses only its coefficient (e0.31.35e^{0.3} \approx 1.35, not 2.23) — the reasoning is muddled and the arithmetic doesn't follow from any coherent method. D flips the subtraction order: 0.3(0.8)=1.10.3 - (-0.8) = 1.1, so e1.13.00e^{1.1} \approx 3.00. This gives you the odds ratio for standard vs. premium, not premium vs. standard. The sign matters. A useful habit: always ask "which group is in the numerator?" When comparing A to B, compute eβAβBe^{\beta_A - \beta_B}, where both coefficients are measured against the same reference.

Question 4

A fitted model has the linear predictor 2+0.7x1.1z-2+0.7x-1.1z, where the response level treated as success is yes.

What probability does the model predict for an observation with x=2x=2 and z=1z=1?

  1. Approximately 0.1540.154, obtained by applying the inverse-logit to 1.7-1.7. (correct answer)
  2. Approximately 0.3320.332, obtained by applying the inverse-logit to 0.7-0.7.
  3. Approximately 0.6680.668, obtained by applying the inverse-logit to 0.70.7.
  4. Approximately 0.8460.846, obtained by applying the inverse-logit to 1.71.7.
Explanation: When working with logistic regression, your job is to (1) plug values into the linear predictor to get the log-odds, then (2) convert that log-odds to a probability using the inverse-logit (also called the logistic function): p=eη1+eηp = \frac{e^\eta}{1 + e^\eta}, where η\eta is the linear predictor evaluated at your inputs. Start by substituting x=2x = 2 and z=1z = 1 into 2+0.7x1.1z-2 + 0.7x - 1.1z: η=2+0.7(2)1.1(1)=2+1.41.1=1.7\eta = -2 + 0.7(2) - 1.1(1) = -2 + 1.4 - 1.1 = -1.7 Now apply the inverse-logit to 1.7-1.7: p=e1.71+e1.70.18271.18270.154p = \frac{e^{-1.7}}{1 + e^{-1.7}} \approx \frac{0.1827}{1.1827} \approx 0.154 This confirms A is correct. The other choices each represent a specific arithmetic mistake. B uses η=0.7\eta = -0.7, which comes from incorrectly computing 2+0.71.1=2.4-2 + 0.7 - 1.1 = -2.4 and then mishandling signs — a sign or order-of-operations error. C uses η=0.7\eta = 0.7, the positive mirror of B, suggesting the student dropped the intercept entirely or flipped a sign. D uses η=1.7\eta = 1.7, which is exactly the negative of the correct answer — a classic trap where someone forgets the intercept is 2-2 (not +2+2) and ends up with the wrong sign throughout. As a study tip: always write out the linear predictor substitution step by step. Sign errors on the intercept are the most common mistake — double-check that negative intercept before you do anything else.

Question 5

For a new observation, predict(fit, newdata = new_case) returns 0.80.8 from a model fitted using family = binomial with the default logit link.

Which statement correctly interprets this result and obtains the predicted probability?

  1. The value 0.80.8 is already a probability because prediction from a binomial model defaults to the response scale.
  2. The value 0.80.8 is a log-odds prediction; predict(fit, new_case, type = "response") gives about 0.6900.690. (correct answer)
  3. The value 0.80.8 is an odds prediction; dividing it by 1+0.81+0.8 gives about 0.4440.444.
  4. The value 0.80.8 is a log-probability; applying exp() gives a probability of about 2.2262.226.
Explanation: Whenever you see predict() used with a glm object fitted using family = binomial, you need to ask yourself: what scale is the prediction on? By default — without specifying type = "response" — R returns predictions on the link scale, which for the logit link means log-odds, not probabilities. This is exactly what's happening in option B. The value 0.80.8 is a log-odds (logit) value. To convert it to a probability, you apply the inverse logit (sigmoid) transformation: p=e0.81+e0.8=2.22553.22550.690p = \frac{e^{0.8}}{1 + e^{0.8}} = \frac{2.2255}{3.2255} \approx 0.690. Equivalently, you can simply add type = "response" to your predict() call and R does this conversion for you automatically. B is correct on both counts. Option A is wrong because the default prediction scale is not the response scale — that's a very common trap. You must explicitly request type = "response" to get probabilities. Option C confuses log-odds with raw odds; the conversion formula p=odds/(1+odds)p = \text{odds}/(1 + \text{odds}) does apply to odds, but 0.80.8 here is a log-odds value, not odds — and applying that formula gives 0.4440.444, which is incorrect. Option D treats 0.80.8 as a log-probability and applies exp(), yielding 2.2262.226 — a value greater than 1, which can never be a probability. That alone should immediately signal something is wrong. Your study tip: always remember that predict(fit) defaults to the link scale for GLMs. Use type = "response" whenever you want the predicted probability directly.

Question 6

A two-level factor response result has levels ordered as c("fail", "pass"). A logistic model is fitted with glm(result ~ x, family = binomial, data = d). The analyst then makes pass the first factor level and refits the same formula on the same complete observations.

How should the refitted model generally relate to the original model?

  1. It models failure rather than passing, and both the intercept and slope estimates are negated. (correct answer)
  2. It still models passing, but only the fitted intercept changes because the predictors are unchanged.
  3. It models failure rather than passing, but only the slope estimate is negated.
  4. It still models passing, and all coefficient estimates remain numerically unchanged.
Explanation: Whenever you see a question about logistic regression and factor level ordering in R, the key is understanding what the model is actually predicting. By default, glm with family = binomial models the probability of the second factor level — the one coded as 1. When levels are c("fail", "pass"), R codes "fail" = 0 and "pass" = 1, so the model predicts P(pass). When you reorder the levels so that "pass" comes first — c("pass", "fail") — R now codes "pass" = 0 and "fail" = 1. The model switches to predicting P(fail). Since the log-odds of failure is simply the negative of the log-odds of success, every coefficient flips sign: the new intercept equals β^0-\hat{\beta}_0 and the new slope equals β^1-\hat{\beta}_1. This makes answer A correct — the model now targets failure, and both estimates are negated. Answer B is wrong because it claims the model still predicts passing, which ignores how R determines the reference outcome from factor level ordering. Answer C is wrong in a subtle way: it correctly notes the model switches to predicting failure, but claiming only the slope negates is incorrect — the intercept flips too, since the entire linear predictor changes sign. Answer D is wrong because it assumes nothing changes, which would only be true if the response coding remained identical. A handy rule: in R, logistic regression always models the last factor level. Change that ordering, and you flip both the target outcome and the sign of every coefficient.

Question 7

An analyst fits fit <- glm(admitted ~ prep_hours + prior_score, family = binomial, data = applicants). The estimated coefficient for prep_hours is 0.4050.405. Two applicants have the same prior_score, but one completed 22 more preparation hours than the other.

According to the fitted model, how do the applicants' predicted odds of admission compare?

  1. The applicant with more preparation has about 1.501.50 times the predicted odds.
  2. The applicant with more preparation has about 1.811.81 times the predicted odds.
  3. The applicant with more preparation has about 2.252.25 times the predicted odds. (correct answer)
  4. The applicant with more preparation has about 2.812.81 times the predicted odds.
Explanation: When working with logistic regression coefficients, remember that each coefficient represents the change in the log-odds of the outcome per one-unit increase in the predictor. To compare odds between two applicants, you need to think in terms of the odds ratio. For a one-unit increase in a predictor, the odds ratio equals eβ^e^{\hat{\beta}}. When the difference between two applicants is Δx\Delta x units, the odds ratio becomes eβ^Δxe^{\hat{\beta} \cdot \Delta x}. Here, the coefficient for prep_hours is 0.4050.405 and the difference is 22 hours, so the odds ratio is: e0.405×2=e0.8102.25e^{0.405 \times 2} = e^{0.810} \approx 2.25 This confirms C — the applicant with more preparation has about 2.252.25 times the predicted odds of admission. Choice A (1.501.50) corresponds to roughly e0.405e^{0.405}, which is the odds ratio for only a one-hour difference, not two. Choice B (1.811.81) is close to e0.405×1.5e^{0.405 \times 1.5}, suggesting someone mistakenly used 1.51.5 instead of 22 as the multiplier, or miscalculated the exponent. Choice D (2.812.81) likely comes from arithmetic errors such as doubling the odds ratio multiplicatively (1.50×1.502.251.50 \times 1.50 \approx 2.25 is correct, but confusing addition and multiplication can push the result higher). A useful tip: whenever a question specifies a multi-unit difference, always scale the exponent first — compute β^×Δx\hat{\beta} \times \Delta x, then exponentiate. Exponentiating first and multiplying after is a common trap that leads to the wrong answer.

Question 8

A model is fitted with glm(y ~ age + region, family = binomial, data = train). In train, factor region has only the levels East and West. The analyst calls predict(fit, newdata = data.frame(age = 40, region = factor("North")), type = "response").

What should the analyst generally expect from this prediction call?

  1. R treats North as the reference level and uses only the fitted age contribution.
  2. R assigns North the average of the estimated East and West effects.
  3. R silently returns a valid probability by coercing North to the nearest known level.
  4. R issues a warning about the new factor level and returns NA for the prediction, because North was not present among the levels used when fitting the model. (correct answer)
Explanation: When working with factor variables in R's predict() function, the key question to ask yourself is: does the new data contain only factor levels that the model has seen during training? R encodes factors as dummy variables based on the levels present at fit time, so any unseen level breaks that encoding entirely. When you pass region = factor("North") to predict(), R cannot construct a valid model matrix row for this observation — it has no coefficient for North because that level never appeared in train. As a result, R issues a warning such as "factor region has new levels: North" and returns NA for that prediction. This is answer D, and it's the correct behavior — R refuses to silently fabricate a result. Answer A is wrong because R doesn't quietly fall back to a reference-level interpretation. The reference level (East) is encoded as all-zero dummies, but R doesn't reassign North to that encoding; it flags the problem instead. Answer B is wrong because R has no mechanism to average coefficients across known levels — that would require custom logic you'd have to write yourself. Answer C is the most tempting distractor: you might assume R "rounds" to the nearest level, but R performs no such coercion. It does not resolve unknown factor levels by proximity or any other heuristic. A good rule of thumb: before calling predict(), always verify that your new data's factor levels are a subset of those in the training data. Use levels(train$region) to check, and consider using factor(..., levels = levels(train$region)) when constructing new data frames.

Question 9

Data frame d contains one row per dose level. Each row records success, the number of successful trials, and total, the number of trials. The analyst fits glm(cbind(success, total - success) ~ dose, family = binomial, data = d).

Which alternative call represents the same grouped-binomial information?

  1. glm(cbind(total - success, success) ~ dose, family = binomial, data = d)
  2. glm(success / total ~ dose, family = binomial, weights = success, data = d)
  3. glm(success / total ~ dose, family = binomial, data = d)
  4. glm(success / total ~ dose, family = binomial, weights = total, data = d) (correct answer)
Explanation: When fitting a binomial GLM in R, you have two equivalent ways to specify grouped binomial data: a two-column matrix via cbind(successes, failures), or a proportion response with a weights argument telling R the total number of trials per row. Understanding this equivalence is what this question tests. The original call uses cbind(success, total - success), meaning the first column is successes and the second is failures. R's binomial family models the probability of the first column event, using both columns to recover the total. The equivalent proportion-based form divides successes by total to get the observed proportion, then supplies weights = total so R knows how many trials each proportion is based on. This is exactly what D does — it preserves all the same information: the estimated proportion and the denominator needed to reconstruct the likelihood. A is wrong because it reverses the columns to cbind(total - success, success), which models the probability of failure rather than success. This flips the sign of all coefficients and changes the interpretation entirely. B is wrong because it uses weights = success instead of weights = total. The weight in a binomial GLM must represent the number of trials, not the number of successes — using successes as weights provides incorrect denominators and distorts the likelihood. C is wrong because it omits the weights argument entirely. Without knowing how many trials each proportion came from, R treats each row as a single Bernoulli trial, discarding all information about sample size. A handy rule: whenever you use a proportion as the response in a binomial GLM, always ask yourself "what is the denominator?" — that answer belongs in weights.

Question 10

In a logistic model, the estimated coefficient for exposure is 0.20.2, with a confidence interval on the log-odds scale from 0.2-0.2 to 0.60.6.

Which interpretation of this interval is most appropriate?

  1. The odds ratio interval is about 0.18-0.18 to 0.820.82, so the exposure reduces the odds.
  2. The probability interval is 0.2-0.2 to 0.60.6, so negative outcome probabilities remain possible.
  3. The odds ratio interval is about 0.820.82 to 1.821.82, so it contains the null value of 11 and is compatible with no change in odds. (correct answer)
  4. The odds ratio interval is about 1.221.22 to 1.821.82, so the exposure clearly increases the odds.
Explanation: Whenever you see a confidence interval from a logistic regression, remember that coefficients are on the log-odds scale — you must exponentiate them to interpret meaningful odds ratios. The transformation is eβe^{\beta}, and crucially, the null value of "no effect" shifts from 00 on the log-odds scale to 11 on the odds ratio scale. Here, the log-odds interval is (0.2, 0.6)(-0.2,\ 0.6). Exponentiating each bound gives e0.20.82e^{-0.2} \approx 0.82 and e0.61.82e^{0.6} \approx 1.82, yielding an odds ratio interval of approximately (0.82, 1.82)(0.82,\ 1.82). Because this interval contains 11 (the null value), the data are compatible with no change in odds — making C the correct interpretation. A is tempting because it correctly computes e0.20.82e^{-0.2} \approx 0.82 and e0.61.82e^{0.6} \approx 1.82, but then misreads the lower bound as 0.18-0.18 instead of 0.820.82, which is simply an arithmetic error. It then draws the wrong conclusion that exposure reduces odds. B treats the log-odds interval as if it were a probability interval, which it is not. Probabilities must lie in [0,1][0, 1]; log-odds have no such constraint, but you never report them as probabilities directly. D gives the right upper bound (1.821.82) but invents a lower bound of 1.221.22, ignoring the actual value of e0.20.82e^{-0.2} \approx 0.82. This fabrication makes the interval appear to exclude 11, falsely suggesting a clear increase. Study tip: Always exponentiate logistic regression confidence intervals before interpreting them, and check whether the resulting interval contains 11 — that single check determines whether your result is statistically compatible with no effect.