Reading R Output
Exam questions in this course show R code and its printed output, and ask what the numbers mean. They do not ask you to write R. The skill is reading: identify what was computed, then convert it into a statistical statement.
1. The four function families
Distributions in R follow one naming scheme. Each distribution has a suffix, and each prefix does one job.
| Prefix | Computes | Example |
|---|---|---|
d | density or mass function, or | dnorm(0) |
p | CDF, | pnorm(1.96) |
q | quantile, the inverse CDF | qnorm(0.975) |
r | random draws | rpois(10, 5) |
The suffixes: norm normal, pois Poisson, exp exponential, binom binomial, unif uniform, t for the , chisq for chi-squared.
p and q are inverses, and confusing them is the standard error. pnorm takes a value and returns a probability; qnorm takes a probability and returns a value. The two calls pnorm(1.96) and qnorm(0.975) say the same thing from opposite ends.
Two conventions that trip people up:
- Tail probabilities need .
pnorm(q)is the left tail. A right-tail probability is1 - pnorm(q), and a discrete right tail strictly above 40 is1 - pbinom(40, 100, .3), becausepbinom(40, ...)already includes 40. - Critical values take the upper quantile. For a two-sided 95% interval, and the critical value is
qnorm(1 - 0.05/2)qnorm(0.975). For a 90% interval,qt(0.95, n - 1), since leaves 0.95 below.
2. Reading a simulation
The commonest question shows a loop that simulates an estimator many times, prints the mean and variance, and asks for the bias, the MSE, or which estimator to prefer.
The structure is always the same: an empty vector, a loop generating a sample and computing the estimator, then summary statistics across replications.
set.seed(238)
t1 <- numeric(10000)
t2 <- numeric(10000)
for (i in 1:10000) {
x <- sample(1:100, 5, replace = TRUE)
t1[i] <- 2 * mean(x) - 1
t2[i] <- max(x)
}
round(c(mean(t1), mean(t2)), 1)
round(c(var(t1), var(t2)), 1)[1] 100.0 83.8
[1] 666.4 199.8Read it in four steps.
- Find the true parameter.
sample(1:100, ...)means . It is in the code, not the output. - Identify what each line prints. The
c(...)order gives first, second. The first line is means, the second variances. - Bias is the simulated mean minus the truth. : , unbiased. : , negative bias.
- MSE is variance plus squared bias. . .
Prefer : biased, but the smaller variance more than compensates. That is the bias-variance tradeoff.
A simulated mean is an estimate of , not itself. A simulated bias of exactly 0.0 is consistent with unbiasedness rather than proof of it, and a small non-zero value may be simulation noise. Say “consistent with unbiased” unless a theoretical argument is available.
3. Checking a model against its own constraints
A distributional family imposes relationships among its moments, and printed summary statistics can refute the family.
> mean(y)
[1] 3.95
> var(y)
[1] 9.638462Is a Poisson model appropriate?
No. Under both the mean and variance equal , so the sample mean and sample variance should be close. Here the variance is about 2.4 times the mean, far more spread than a Poisson allows. That is overdispersion, and the usual cause is a rate varying across observations, which violates the identically-distributed assumption.
The constraints worth knowing:
- Poisson: mean variance .
- Exponential: mean , variance , so variance mean, and the standard deviation equals the mean.
- Bernoulli: mean , variance , so the variance is at most .
- Binomial: variance mean , so a binomial is under-dispersed.
4. Logical vectors
R treats TRUE as 1 and FALSE as 0, so mean() of a logical vector is a proportion and sum() is a count. Several exam questions turn on this.
m <- c(419.2, 420.8, 419.9, 421.3, 420.1)
p <- mean(m > 420)
k <- sum(abs(m - 420) > 1)m > 420 gives FALSE TRUE FALSE TRUE TRUE, three of five true, so p .
abs(m - 420) gives 0.8, 0.8, 0.1, 1.3, 0.1; exactly one exceeds 1, so k .
The second part of the question is what the quantity estimates. p is the relative frequency of measurements above 420, and it estimates , the probability that a single measurement exceeds 420. It is the relative-frequency estimate of that feature of the model distribution.
The same idiom builds estimators of probabilities generally: mean(sales == 0) estimates , and mean(abs(boot_dist) > .5) estimates a tail probability from a bootstrap distribution.
5. Exact against approximate
Questions often show two computations of the same probability.
1 - pnorm(.4, mean = .3, sd = sqrt(.3 * .7 / 100))
[1] 0.01454817
1 - pbinom(40, 100, .3)
[1] 0.01249841The first is the central limit theorem approximation, treating as normal with mean and variance . The second is the exact binomial.
They differ because is finite. The central limit theorem is a limit statement, so the approximation improves with and is not exact at any finite value. Naming that as the reason is what the question wants.
6. Confidence interval output
qt(0.975, df = n - 1)
c(x_bar - t0975 * s_n / sqrt(n), x_bar + t0975 * s_n / sqrt(n))
[1] -0.414433612 0.002680943Read the pieces: qt means the distribution, so is unknown; df = n - 1 confirms the degrees of freedom; 0.975 means and a 95% level; and s_n / sqrt(n) is the standard error.
Comparing against the known-variance version on the same data:
known variance: -0.401872733 -0.009879936 (width 0.392)
unknown variance: -0.414433612 0.002680943 (width 0.417)The interval is wider. With added uncertainty about the variance, the interval widens, and that sentence is the expected answer.
For a bootstrap interval, quantile(t_boot, alpha/2) and quantile(t_boot, 1 - alpha/2) are the empirical critical values, and both endpoints subtract, with the upper critical value in the lower endpoint.
7. Simulation code idioms
Recognizing the shapes lets you say what a loop computes without tracing it.
Allocate then fill. numeric(m) makes an empty vector of length m, and the loop fills it. The length is the number of replications.
Nested loops sweep a parameter. An outer loop over lambdas with an inner loop over replications produces bias and variance as functions of the parameter.
set.seed(238) makes the draws reproducible. It affects nothing statistical.
A discrepancy worth knowing. Some course code allocates numeric(m) but loops for (i in seq(n)) with n smaller than m, so only the first n entries are filled and the rest stay zero. If asked about such code, the allocation length and the loop length should match, and a mismatch leaves trailing zeros that corrupt mean() and var().
8. The reading procedure
- Find the true parameter value, usually in the code rather than the output.
- Match each printed line to the expression that produced it, in order.
- Name what each number is: a mean, a variance, a probability, a quantile, a bound.
- Convert into the statistical quantity asked for, via bias or MSE variance bias.
- State the conclusion in words, with a reason. A bare number is not an answer to “is this model appropriate.”
Vocabulary to deploy
d/p/q/rprefixes: density, CDF, quantile, random draws.pnormagainstqnormas inverse operations;1 - p...for the right tail.- Logical-vector arithmetic:
mean()gives a proportion,sum()gives a count. - Simulated mean as an estimate of ; simulated variance as an estimate of .
- Overdispersion: sample variance materially exceeding the sample mean.
- Relative-frequency estimate of a probability.