Assignment 6
R² doesn't mean what you think it means
Every R linear-model summary prints an \(R^2\), and many scientists treat it as a grade for the model: high means “good model”, low means “bad model”. The uncomfortable truth is that \(R^2\) is a variance bookkeeping number: it says what fraction of the variance of the response is accounted for by the fitted model. It says nothing about whether the model has the right shape, how strong the effect is, or how well the model will predict data it has never seen.
In this assignment you will watch \(R^2\) lie — by simulating data from models whose truth you know, and then fitting models to that data and seeing what \(R^2\) reports. Four classic failures are possible:
- Shape blindness. A perfectly deterministic curve (a parabola) can have \(R^2 \sim 0\) when fitted with a line, while a single bad point can slash a genuine \(R^2\) from \(\sim 0.9\) to \(\sim 0.2\). \(R^2\) certifies nothing about whether the shape of your model is right.
- Design dependence. The same slope and the same noise give \(R^2 \sim 0.9\) when your \(x\)’s span [−10, 10] and \(R^2 \sim 0.05\) when they span [−0.5, 0.5]. \(R^2\) depends on the range of \(x\) you happened to sample — it measures your sampling design, not the effect.
- Inflatable by overfitting. Regress pure random noise on k random “predictors” and \(R^2\) grows like \(\frac{k}{(n−1)}\): throw in enough terms and pure noise “explains” almost anything. A big \(R^2\) is not evidence that anything is real.
- Not predictive. Fit polynomials of increasing degree to a training set: the in-sample \(R^2\) climbs relentlessly toward 1, while the out-of-sample \(R^2\) (measured on data the model has never seen) peaks at the right degree and then collapses — often to negative values.
We will focus on the second failure mode in this assignment.
Create a script. Your script will receive three command-line arguments: \(n\), the number of data, and \(x_{\rm max}\), the maximum \(x\) value being considered, and \(m\), the number of equally spaced x values between 0.5 and \(x_{\rm max}\). For each value of \(x_i\) between 0.5 and \(x_{\rm max}\), your script should
- create \(n\) fake \(x\) and \(y\) data:
- The \(x\) data should be sampled from the uniform distribution, between \(-x_i\) and \(x_i\),
- The \(y\) data should be linear functions of \(x\), with a slope of 2 (only hard-coded in your driver script) and with additional normally distributed noise with a standard deviation of 2 (only hard-coded in your driver script),
- build a linear model using that data,
- calculate the theoretical \(R^2\) value for that data (lecture 14, slide 13). Note that for a uniform distribution the theoretical variance is \((x_{\rm max} - x_{\rm min})^2 / 12\).
- Prints out the
- \(x\)-range,
- empirical and theoretical \(R^2\)
- fit slope
- \(p\)-value
- plot the theoretical and empirical \(R^2\) versus \(x_{\rm max}\).
Your code should be modular, and use coding best-practices. You should also implement defensive programming; as much as is needed.
Running the script should produce output in the spirit of:
$ Rscript Rsquared.Analysis.R 30 10 20
============================================================
True slope = 2, true noise sigma = 2, n = 30
x-range empirical R^2 theoretical R^2 slope p-value
------------------ ------------- --------------- --------- ----------
[ -0.5, 0.5] 0.152 0.077 2.815 3.34e-02
[ -1.0, 1.0] 0.289 0.250 1.499 2.20e-03
[ -1.5, 1.5] 0.588 0.429 1.977 7.63e-07
[ -2.0, 2.0] 0.687 0.571 2.049 1.54e-08
[ -2.5, 2.5] 0.631 0.676 1.913 1.59e-07
[ -3.0, 3.0] 0.878 0.750 1.839 2.67e-14
[ -3.5, 3.5] 0.809 0.803 2.065 1.46e-11
[ -4.0, 4.0] 0.882 0.842 1.933 1.69e-14
[ -4.5, 4.5] 0.897 0.871 2.065 2.45e-15
[ -5.0, 5.0] 0.895 0.893 2.068 3.04e-15
[ -5.5, 5.5] 0.945 0.910 2.052 3.66e-19
[ -6.0, 6.0] 0.924 0.923 1.889 3.46e-17
[ -6.5, 6.5] 0.920 0.934 1.947 7.22e-17
[ -7.0, 7.0] 0.957 0.942 1.963 1.26e-20
[ -7.5, 7.5] 0.955 0.949 2.098 2.49e-20
[ -8.0, 8.0] 0.957 0.955 2.001 1.07e-20
[ -8.5, 8.5] 0.947 0.960 1.934 2.06e-19
[ -9.0, 9.0] 0.971 0.964 1.996 4.80e-23
[ -9.5, 9.5] 0.962 0.968 2.037 2.21e-21
[ -10.0, 10.0] 0.974 0.971 1.985 9.41e-24
Assignments will be graded on a 10 point basis.
Due date: October 29th, 11:59 p.m., with 0.5 point penalty per day for late submission until the cut-off date, November 5th, 9:00am.