Logistic Regression (glm) - R Programming
- What probability does the model predict for an observation with and ? Answer: Approximately , obtained by applying the inverse-logit to . 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): , where is the linear predictor evaluated at your inputs. Start by substituting and into : Now apply the inverse-logit to : This confirms A is correct. The other choices each represent a specific arithmetic mistake. B uses , which comes from incorrectly computing and then mishandling signs — a sign or order-of-operations error. C uses , the positive mirror of B, suggesting the student dropped the intercept entirely or flipped a sign. D uses , which is exactly the negative of the correct answer — a classic trap where someone forgets the intercept is (not ) 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.
- How should the refitted model generally relate to the original model? Answer: It models failure rather than passing, and both the intercept and slope estimates are negated. 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,
glmwithfamily = binomialmodels the probability of the second factor level — the one coded as 1. When levels arec("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 and the new slope equals . 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. - Which statement correctly interprets this result and obtains the predicted probability? Answer: The value is a log-odds prediction;
predict(fit, new_case, type = "response")gives about . Whenever you seepredict()used with aglmobject fitted usingfamily = binomial, you need to ask yourself: what scale is the prediction on? By default — without specifyingtype = "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 is a log-odds (logit) value. To convert it to a probability, you apply the inverse logit (sigmoid) transformation: . Equivalently, you can simply addtype = "response"to yourpredict()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 requesttype = "response"to get probabilities. Option C confuses log-odds with raw odds; the conversion formula does apply to odds, but here is a log-odds value, not odds — and applying that formula gives , which is incorrect. Option D treats as a log-probability and appliesexp(), yielding — a value greater than 1, which can never be a probability. That alone should immediately signal something is wrong. Your study tip: always remember thatpredict(fit)defaults to the link scale for GLMs. Usetype = "response"whenever you want the predicted probability directly. - What is the estimated effect of one additional hour for observations in group
B?Answer: Their log-odds decrease by , giving an odds ratio of about . When aglmincludes an interaction term likehours * group, the coefficient forhoursalone 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: This means one additional hour decreases the log-odds by for group B observations. Converting to an odds ratio: , confirming answer B is correct. Choice A applies thehourscoefficient () 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 () as the complete effect for group B, forgetting that the base slope forhoursstill applies and must be included. Choice D adds the two coefficients incorrectly as positives: , 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. - What should the analyst generally expect from this prediction call? Answer: R issues a warning about the new factor level and returns
NAfor the prediction, becauseNorthwas not present among the levels used when fitting the model. When working with factor variables in R'spredict()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 passregion = factor("North")topredict(), R cannot construct a valid model matrix row for this observation — it has no coefficient forNorthbecause that level never appeared intrain. As a result, R issues a warning such as *"factorregionhas new levels: North"* and returnsNAfor 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 reassignNorthto 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 callingpredict(), always verify that your new data's factor levels are a subset of those in the training data. Uselevels(train$region)to check, and consider usingfactor(..., levels = levels(train$region))when constructing new data frames. - According to the fitted model, how do the applicants' predicted odds of admission compare? Answer: The applicant with more preparation has about times the predicted odds. 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 . When the difference between two applicants is units, the odds ratio becomes . Here, the coefficient for
prep_hoursis and the difference is hours, so the odds ratio is: This confirms C — the applicant with more preparation has about times the predicted odds of admission. Choice A () corresponds to roughly , which is the odds ratio for only a one-hour difference, not two. Choice B () is close to , suggesting someone mistakenly used instead of as the multiplier, or miscalculated the exponent. Choice D () likely comes from arithmetic errors such as doubling the odds ratio multiplicatively ( 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 , then exponentiate. Exponentiating first and multiplying after is a common trap that leads to the wrong answer. - Holding all else fixed, what is the estimated odds ratio for churn comparing
premiumwithstandard?Answer: Approximately because the relevant log-odds difference is . 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,basicis the reference. The model gives you: - - To compare premium directly against standard, subtract: . Exponentiating gives the odds ratio: . 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 (), 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 treatsstandardas a new baseline and uses only its coefficient (, not 2.23) — the reasoning is muddled and the arithmetic doesn't follow from any coherent method. D flips the subtraction order: , so . 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 , where both coefficients are measured against the same reference. - Which alternative call represents the same grouped-binomial information? Answer:
glm(success / total ~ dose, family = binomial, weights = total, data = d)When fitting a binomial GLM in R, you have two equivalent ways to specify grouped binomial data: a two-column matrix viacbind(successes, failures), or a proportion response with aweightsargument telling R the total number of trials per row. Understanding this equivalence is what this question tests. The original call usescbind(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 suppliesweights = totalso 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 tocbind(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 usesweights = successinstead ofweights = 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 theweightsargument 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 inweights. - Which interpretation of this interval is most appropriate? Answer: The odds ratio interval is about to , so it contains the null value of and is compatible with no change in odds. 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 , and crucially, the null value of "no effect" shifts from on the log-odds scale to on the odds ratio scale. Here, the log-odds interval is . Exponentiating each bound gives and , yielding an odds ratio interval of approximately . Because this interval contains (the null value), the data are compatible with no change in odds — making C the correct interpretation. A is tempting because it correctly computes and , but then misreads the lower bound as instead of , 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 ; log-odds have no such constraint, but you never report them as probabilities directly. D gives the right upper bound () but invents a lower bound of , ignoring the actual value of . This fabrication makes the interval appear to exclude , falsely suggesting a clear increase. Study tip: Always exponentiate logistic regression confidence intervals before interpreting them, and check whether the resulting interval contains — that single check determines whether your result is statistically compatible with no effect.
- Which values of will be classified as positive under this rule? Answer: Values satisfying , because the logit link maps a probability of to a linear predictor of , and solving yields this bound. 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 , where is the linear predictor. The key insight is that if and only if . This is because the logistic function equals exactly when its argument is , and is increasing throughout. So the classification rule translates cleanly to — no messy algebra with exponentials required. Applying this to your linear predictor: you need , which gives , so . That confirms B is correct. A commits a conceptual error by substituting 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 , which is incorrect. The threshold probability of corresponds to a log-odds of , not , and the inequality runs the wrong direction anyway. D treats the intercept alone as the classification boundary, ignoring the role of and the coefficient in shifting that boundary. Study tip: Always remember that in logistic regression, . This one fact lets you convert any probability-based classification rule into a simple linear inequality instantly.