1 · Start with the VariableCh 1 · IDA
| Variables | Fast display |
|---|---|
| 1 qualitative | barplot(table(x)) |
| 2 qualitative | stacked / beside bars |
| 1 quantitative | histogram, boxplot |
| 2 quantitative | scatter plot |
| quantitative by group | boxplot(y ~ group) |
Qualitative: nominal or ordered. Quantitative: counted (discrete) or measured (continuous). A numerical code can still be qualitative; inspect str(), then use as.factor() or as.numeric(). Replace missing-value codes by NA.
Unequal histogram intervalsarea = count ÷ total
height = area ÷ interval width
count = height × width × total
Area carries proportion; height carries crowding. With right=F, intervals include the left edge and exclude the right. Conditions act elementwise: ==, !=, &, |, !; sum(condition) counts TRUE values.
First-look sequence: establish the data source and research question → inspect dimensions and one row per subject → classify columns → repair storage and missing codes → select plots and numerical summaries → look for impossible values, outliers, skew and group differences. An anonymous ID is not an analysis variable.
| R pattern | Meaning |
|---|---|
| table(x); barplot(...) | counts for a qualitative variable |
| hist(x, breaks=..., right=F) | left-closed classes |
| boxplot(y ~ group) | quantitative response across groups |
| sum(x>5 & x<=10) | count values in (5,10] |
| d[d$x>5 & d$g=="A", ] | filter matching rows |
Trap + fix Digits do not make an ID quantitative: if averaging is meaningless, treat it as qualitative and use as.factor(). With right=F, a boundary enters the interval starting there; predict its bin first.
3 · Normal Curve: Area or Cut-off?Ch 3
X ~ N(μ, σ²) · z=(x−μ)/σ
P(X<a)=pnorm(a, μ, σ)
P(X>a)=pnorm(a, μ, σ, lower.tail=F)
P(a<X<b)=pnorm(b,μ,σ)−pnorm(a,μ,σ)
Qₚ=qnorm(p, μ, σ)=μ+σqnorm(p)
Second entry of N(μ,σ²) is variance; pnorm/qnorm expect SD. Total area = 1. Symmetry: P(Z<−a)=P(Z>a). The 68–95–99.7 rule places about 68%, 95%, 99.7% within 1, 2, 3 SDs.
| Question gives | Question asks | Use |
|---|---|---|
| measurement | proportion / chance | pnorm |
| proportion | measurement / cut-off | qnorm |
| Band / percentile | Normal shortcut |
|---|---|
| μ±σ | about 68% inside; 16% in each tail |
| μ±2σ | about 95% inside; 2.5% each tail |
| μ±3σ | about 99.7% inside; 0.15% each tail |
| 16th, 50th, 84th | approximately μ−σ, μ, μ+σ |
Sketch before code: mark μ, the cut-off(s), and shade. One pnorm call returns one lower tail; an upper tail uses the complement, an interval subtracts two lower tails, and two outer tails must be added. For a standardised calculation, the area is unchanged when x becomes z.
4 · Correlation: Calculate, Then DistrustCh 4
r = average(zₓzᵧ), −1≤r≤1 · cor(x,y)
Plot first. r measures only straight-line association. Its sign must match the visible slope. Adding a constant, multiplying by a positive constant, or swapping x/y leaves r unchanged; multiplying one variable by a negative constant flips its sign.
| Trap | Consequence |
|---|---|
| one outlier | can dominate r |
| curved relation | r can be 0 despite exact dependence |
| group averages | ecological r tends to inflate |
| association | does not establish causation |
5 · Regression: Five Numbers → LineCh 5
Predict y from xb = r·SDᵧ/SDₓ · a = ȳ−b x̄ · ŷ = a + bx
e=y−ŷ · SSE=Σe² · SST=Σ(y−ȳ)²
R²=1−SSE/SST=r²
The line passes through (x̄,ȳ), minimises squared residuals, and moves only r SDs in y for each SD in x. Baseline prediction is always ȳ. Do not rearrange the y-on-x line to predict x; refit lm(x ~ y).
| Residual plot | Decision |
|---|---|
| random band around 0; equal strip spreads | linear fit, homoscedastic; prediction defensible |
| curve | line misses shape |
| fan / changing strip spread | heteroscedastic; do not trust prediction |
Check Residuals from the fitted line average zero. Positive residual = observed point above the line. R² is the share of y-variation removed relative to predicting ȳ.
R output decoder
| Object / quantity | Read as |
|---|---|
| lm(y ~ x) | response first; predictor after ~ |
| coef: (Intercept) | a, fitted y when x=0 |
| coef: x | b, change in fitted y per x unit |
| fitted.values | ŷᵢ on the line |
| residuals | yᵢ−ŷᵢ |
| cor(x,y)^2 | R² for simple linear regression |
Reverse prediction: y-on-x slope is rSDᵧ/SDₓ; x-on-y slope is rSDₓ/SDᵧ. Rearranging the first gives SDₓ/(rSDᵧ), which is different unless |r|=1. Extrapolation beyond the observed x-range also lacks data support.
12 · Chi-squaredCh 12
T=Σ(O−E)²/E · P=pchisq(T,df,lower.tail=F)
GOF: Eⱼ=np₀ⱼ, df=k−1
independence: Eᵢⱼ=rowᵢ total×colⱼ total/n
df=(r−1)(c−1)
5b · Regression Answer Pathmarks
- Name response y and predictor x.
- Compute b; its sign must equal r.
- Compute a from the point of averages.
- Substitute x; retain units.
- Residual = observed − fitted; interpret sign.
- Square r; write “% of variation in y explained”.
- Inspect residual pattern before endorsing prediction.
SSE versus SST: SST is the squared error from always predicting ȳ; SSE remains after using x. Thus 1−SSE/SST is improvement over the baseline. A large R² does not repair curvature, unequal spread, an influential point or a causal interpretation.
6 · Probability RulesCh 6
P(Aᶜ)=1−P(A)
P(A|B)=P(A∩B)/P(B)
P(A∩B)=P(A)P(B|A)
P(A∪B)=P(A)+P(B)−P(A∩B)
| Condition | Meaning | Shortcut |
|---|---|---|
| mutually exclusive | cannot both occur | P(A∩B)=0 |
| independent | one does not change the other | P(A∩B)=P(A)P(B) |
Exclusive positive-probability events are dependent. For “at least one”, use 1 − P(none). Before multiplying, write the second chance as “given”; drop the condition only after independence is established.
sample(..., replace=FALSE) changes the box after each draw; draws are dependent. Use set.seed() for reproducibility, put one experiment in a function, repeat with replicate(), and take mean() of logical results.
Set language: A∩B means both; A∪B means at least one; Aᶜ means not A. Conditional probability restricts the denominator to B. Independence can be checked by P(A|B)=P(A) or P(A∩B)=P(A)P(B); one equality failure is enough to reject it.
| Prompt | First move |
|---|---|
| at least one success | 1−P(no successes) |
8 · Law of Averages + CLTCh 8
The sample average concentrates at μ because σ/√n→0. The sum's absolute error grows as σ√n; the proportion's error shrinks as σ/√n. The original ticket box does not become normal: the distribution of sums or averages does.
CLT for a sumz=(s−nμ)/(σ√n)
P(S≤s) ≈ pnorm(s, nμ, σ√n)
Approximation quality depends on box shape: a very skewed box needs more draws. Always identify sum versus average, write E and SE, then sketch the requested tail before calling pnorm.
Kerrich check: for 10,000 fair tosses, E(heads)=5,000 and SE(count)=0.5√10,000=50. The observed 5,067 is +67, or 1.34 SEs, from expectation. The count error is visibly large, but the proportion error is only 0.0067.
| Multiply n by 10 | Effect |
|---|---|
| SE of count | ×√10 ≈ ×3.16 |
| SE of proportion / average | ÷√10 ≈ ÷3.16 |
| shape of sums | usually closer to normal |
9 · One Proportion: Interval + ZCh 9
p̂=x/n · SE(p̂|p)=√[p(1−p)/n]
prediction interval (known p): p ± 1.96SE
Wilson CI: binom.confint(x,n,method="wilson")
z=(p̂−p₀)/√[p₀(1−p₀)/n]
| H₁ | P-value |
|---|---|
| p<p₀ | pnorm(z) |
| p>p₀ | pnorm(z, lower.tail=F) |
| p≠p₀ | 2×pnorm(|z|, lower.tail=F) |
Wilson collects candidate p values whose prediction intervals contain p̂; because SE depends on p, it is not centred exactly on p̂. A 95% procedure captures the true p in 95% of repeated samples, not a 95% probability statement about this fixed interval.
10 · HATPC: Write the Whole TestCh 9–12
- Hypotheses: parameter and direction fixed before data.
- Assumptions: random / independent draws; distribution or large-sample condition; study design.
- Test statistic: estimate minus null, divided by null SE.
- P-value: assuming H₀, chance of evidence at least this strong in H₁'s direction.
- Conclusion: compare with α; reject or do not reject H₀; answer in context.
A large P-value does not prove or “accept” H₀. A two-sided level-α test and a (1−α) CI give the same decision about the null value.
Tail logic: the statistic measures distance from H₀ in SEs. “Less” uses a lower tail; “greater” an upper tail; “different” both. The P-value is computed under H₀ and is not the probability that H₀ is true. α is the long-run false-alarm probability when H₀ is true.
11 · Choose the Mean TestCh 10–11
| Design / information | Statistic / R |
|---|---|
| one mean; σ known | Z=(x̄−μ₀)/(σ₀/√n) |
| one mean; σ unknown; near normal | T=(x̄−μ₀)/(s/√n), df=n−1; t.test(x,mu=) |
| 2 independent; unequal spreads | Welch: t.test(x,y) default |
| 2 independent; common SD justified | pooled: var.equal=T; df=m+n−2 |
| same units twice / matched | differences d=x−y; paired=T; df=n−1 |
| clear skew / doubtful normality | bootstrap T from centred surrogate box |
Two independent meansSE(x̄−ȳ)=√(σₓ²/m+σᵧ²/n)
pooled sₚ=√([(m−1)sₓ²+(n−1)sᵧ²]/(m+n−2))
T=(x̄−ȳ)/[sₚ√(1/m+1/n)]
Student's t has fatter tails than N(0,1); use pt/qt, not pnorm/qnorm. QQ points near a line support a near-normal box. For paired data, argument order fixes difference sign.