From STA238: Probability, Statistics and Data Analysis II

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.

PrefixComputesExample
ddensity or mass function, f(x)f(x) or P(X=x)P(X=x)dnorm(0)
pCDF, P(Xq)P(X \le q)pnorm(1.96) =0.975= 0.975
qquantile, the inverse CDFqnorm(0.975) =1.96= 1.96
rrandom drawsrpois(10, 5)

The suffixes: norm normal, pois Poisson, exp exponential, binom binomial, unif uniform, t for the tt, 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 11-{}. pnorm(q) is the left tail. A right-tail probability is 1 - pnorm(q), and a discrete right tail strictly above 40 is 1 - pbinom(40, 100, .3), because pbinom(40, ...) already includes 40.
  • Critical values take the upper quantile. For a two-sided 95% interval, α=0.05\alpha = 0.05 and the critical value is qnorm(1 - 0.05/2) == qnorm(0.975) =1.96= 1.96. For a 90% interval, qt(0.95, n - 1), since α/2=0.05\alpha/2 = 0.05 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.

r
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)
txt
[1] 100.0  83.8
[1] 666.4 199.8

Read it in four steps.

  1. Find the true parameter. sample(1:100, ...) means N=100N = 100. It is in the code, not the output.
  2. Identify what each line prints. The c(...) order gives T1T_1 first, T2T_2 second. The first line is means, the second variances.
  3. Bias is the simulated mean minus the truth. T1T_1: 100.0100=0100.0 - 100 = 0, unbiased. T2T_2: 83.8100=16.283.8 - 100 = -16.2, negative bias.
  4. MSE is variance plus squared bias. MSE(T1)666.4+0=666.4\mathrm{MSE}(T_1) \approx 666.4 + 0 = 666.4. MSE(T2)199.8+262.4=462.2\mathrm{MSE}(T_2) \approx 199.8 + 262.4 = 462.2.

Prefer T2T_2: biased, but the smaller variance more than compensates. That is the bias-variance tradeoff.

A simulated mean is an estimate of E[T]\mathbb{E}[T], not E[T]\mathbb{E}[T] 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.

r
> mean(y)
[1] 3.95
> var(y)
[1] 9.638462

Is a Poisson model appropriate?

No. Under Pois(λ)\mathrm{Pois}(\lambda) both the mean and variance equal λ\lambda, 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 =λ= \lambda.
  • Exponential: mean =1/λ= 1/\lambda, variance =1/λ2= 1/\lambda^2, so variance == mean2^2, and the standard deviation equals the mean.
  • Bernoulli: mean =p= p, variance =p(1p)= p(1-p), so the variance is at most 0.250.25.
  • Binomial(n,p)(n,p): variance =np(1p)<= np(1-p) < mean =np= np, 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.

r
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 =3/5=0.6= 3/5 = 0.6.

abs(m - 420) gives 0.8, 0.8, 0.1, 1.3, 0.1; exactly one exceeds 1, so k =1= 1.

The second part of the question is what the quantity estimates. p is the relative frequency of measurements above 420, and it estimates P(Mi>420)P(M_i > 420), 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 P(X=0)P(X = 0), 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.

r
1 - pnorm(.4, mean = .3, sd = sqrt(.3 * .7 / 100))
[1] 0.01454817
1 - pbinom(40, 100, .3)
[1] 0.01249841

The first is the central limit theorem approximation, treating Gˉn\bar{G}_n as normal with mean p=0.3p = 0.3 and variance p(1p)/np(1-p)/n. The second is the exact binomial.

They differ because n=100n = 100 is finite. The central limit theorem is a limit statement, so the approximation improves with nn and is not exact at any finite value. Naming that as the reason is what the question wants.

6. Confidence interval output

r
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.002680943

Read the pieces: qt means the tt distribution, so σ\sigma is unknown; df = n - 1 confirms the degrees of freedom; 0.975 means α=0.05\alpha = 0.05 and a 95% level; and s_n / sqrt(n) is the standard error.

Comparing against the known-variance version on the same data:

txt
known variance:    -0.401872733 -0.009879936    (width 0.392)
unknown variance:  -0.414433612  0.002680943    (width 0.417)

The tt 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

  1. Find the true parameter value, usually in the code rather than the output.
  2. Match each printed line to the expression that produced it, in order.
  3. Name what each number is: a mean, a variance, a probability, a quantile, a bound.
  4. Convert into the statistical quantity asked for, via bias =E[T]θ= \mathbb{E}[T] - \theta or MSE == variance ++ bias2^2.
  5. 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/r prefixes: density, CDF, quantile, random draws.
  • pnorm against qnorm as 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 E[T]\mathbb{E}[T]; simulated variance as an estimate of Var(T)\mathrm{Var}(T).
  • Overdispersion: sample variance materially exceeding the sample mean.
  • Relative-frequency estimate of a probability.