Skip to learning content

Apply what you learned

Work with data

Four guided labs connect the algebra to fitted models. Download the synthetic data, try the tasks, then compare your reasoning.

These deliberately constructed datasets contain no personal data. Use R with no additional packages, or another statistical tool. The labs are self-study exercises and do not contribute to your scored practice evidence.

Download all datasets and R solutions

Extract the zip into one folder. In R, run the command below and choose solutions.R from that folder. It reads the four datasets alongside the script.

r
source(file.choose(), chdir = TRUE)

Lab 1

Fit a line and examine its residuals

Use the eight observations to compute the centred cross-product slope, fit y on x with an intercept, and compare the two results. Check the residual sum and residual–x cross-product. Change to a unit one hundredth as large, so the recorded variable becomes w=100 xw = 100\,x, and explain how its coefficient changes.

Show a starting hint

Start with the sample means. Compute Sxy and Sxx, then divide. Use lm(y ~ x, data = d), coef(), resid() and sum(). A known rescaling changes coefficient units; it does not make the information noisier.

Compare with the worked interpretation

The sample means are xˉ=4.5\bar{x} = 4.5 and yˉ=9.5\bar{y} = 9.5. Centre each observation around its sample mean:

Sxy=67\mathrm{Sxy} = 67

and

Sxx=42.\mathrm{Sxx} = 42.

The slope is therefore

6742≈1.595238.\frac{67}{42} \approx 1.595238.

Substitute it into the intercept formula:

9.5−(6742)×4.5=6528≈2.321429.9.5 - \bigl(\frac{67}{42}\bigr) \times 4.5 = \frac{65}{28} \approx 2.321429.

The fitted regression gives the same two coefficients. The residual sum and residual–x cross-product are zero up to rounding. With w=100 xw = 100\,x, one w unit is only one hundredth of an original x unit, so the coefficient becomes

6742100≈0.0159524;\frac{\frac{67}{42}}{100} \approx 0.0159524;

predictions stay the same. These sample fitting identities do not prove structural exogeneity.

Lab 2

Interpret two fitted group lines

Fit y on x, a binary group and their interaction. Write each group’s line. Compare groups at x=2x = 2. Calculate the group-one slope standard error using the fitted covariance matrix, including the covariance term.

Show a starting hint

In R, y ~ x * group expands to main effects and their product. Substitute

group=0\mathrm{group} = 0

and

group=1\mathrm{group} = 1

before interpreting coefficients. For the slope sum, use

Var⁡(b+d)=Var⁡(b)+Var⁡(d)+2 Cov⁡(b, d).\operatorname{Var}\left(b + d\right) = \operatorname{Var}\left(b\right) + \operatorname{Var}\left(d\right) + 2\,\operatorname{Cov}\left(b,\,d\right).
Compare with the worked interpretation

The fit is

y^=20+2 x+5 group+3 x×group.\hat{y} = 20 + 2\,x + 5\,\mathrm{group} + 3\,x \times \mathrm{group}.

Substitute

group=0\mathrm{group} = 0

to obtain 20+2 x20 + 2\,x, and

group=1\mathrm{group} = 1

to obtain 25+5 x25 + 5\,x. At x=2x = 2 the fitted values are 24 and 35, so the group gap is 11. The group-one slope is the sum 2+3=52 + 3 = 5. For that slope, the fitted conventional covariance matrix gives

Var⁡(bx)=215,\operatorname{Var}\left(b_{x}\right) = \frac{2}{15}, Var⁡(binteraction)=415\operatorname{Var}\left(b_{\mathrm{interaction}}\right) = \frac{4}{15}

and

Cov⁡(bx, binteraction)=−215.\operatorname{Cov}\left(b_{x},\,b_{\mathrm{interaction}}\right) = \frac{-2}{15}.

Thus its estimated variance is

215+415+2×(−215)=215,\frac{2}{15} + \frac{4}{15} + 2 \times \bigl(\frac{-2}{15}\bigr) = \frac{2}{15},

and its standard error is

215≈0.365148.\sqrt{\frac{2}{15}} \approx 0.365148.

This is the SE of the group-one slope, not the SE of the group gap at x=2x = 2. The interaction coefficient 3 is the slope difference. These data are synthetic balanced cells; exact recovered coefficients are an algebra exercise, not evidence from a real intervention.

Lab 3

See omitted-variable bias and FWL

Fit the short regression y on x and the full regression y on x and z. Residualise y and x on the same intercept and z, then obtain the through-origin residual slope. Explain why the short slope differs.

Show a starting hint

Use the same rows in every fit. Compare the cross-product residual slope with the full-model coefficient. The synthetic construction deliberately makes z related to x and gives z an outcome contribution.

Compare with the worked interpretation

The full-model x coefficient is 3; the short coefficient is 5.4; the residual-on-residual slope is 3. The omitted component is

6×Cov⁡(x, z)Var⁡(x)=6×(25)=2.4.\frac{6 \times \operatorname{Cov}\left(x,\,z\right)}{\operatorname{Var}\left(x\right)} = 6 \times \bigl(\frac{2}{5}\bigr) = 2.4.

FWL reproduces the adjusted coefficient. It does not guarantee that all real-world confounding has been measured, and naive shortcut-regression degrees of freedom need not reproduce the full-model inference.

Lab 4

Recover a signal with a valid simulated instrument

Regress y on observed x and compare with the regression using the supplied latent x_star. Calculate Cov⁡(z, y)Cov⁡(z, x)\frac{\operatorname{Cov}\left(z,\,y\right)}{\operatorname{Cov}\left(z,\,x\right)}. Use the supplied simulation truth to compute reliability and explain the attenuation.

Show a starting hint

In real datasets x_star is generally unobserved. It is supplied here only to audit the simulation. The design balances independent sign components, deliberately making z relevant and orthogonal to both the outcome noise and measurement error.

Compare with the worked interpretation

The latent-regressor slope is 4. The observed-x OLS slope is

103≈3.333333,\frac{10}{3} \approx 3.333333,

equal to four times reliability 56\frac{5}{6}. Scalar IV returns 4. The simulation’s construction supplies the required moments; a real instrument would need a substantive validity argument. If it shared reporting noise or had a direct outcome path, this conclusion would not follow.