Linear regression assumes a set of coordinates $(x_i, y_i),\ i=1,\cdots,N$ fits the relation

which the error term $\epsilon$ should be i.i.d. normally distributed with zero mean.

In general, with one dependend variable $y_{i}$ and various independent variable $x_{ji}$, we can define the linear regression model as

or further make this into GLM by considering $\mathbf{y}$ a $q$-column matrix instead of column vector, which $\mathbf{b}$ and $\mathbf{e}$ are also matrices of coefficient and error terms correspondingly.

The solution to simple linear model, $\mathbf{y} = \mathbf{X}\mathbf{b} + \mathbf{e}$, using ordinary least square (OLS) is

which is obtained by evaluating the derivative of the above norm term equals to zero.

The variance estimator is proportional to variance of $\epsilon$

This is called the variance-covariance matrix and the standard error (used in the t-tests below) on the diagonal.

Although in the above, we have multiple indepdent variables $x_{ji}$, in practice they can be correlated to each other. For example, by setting $x_{ki} = x_{1i}^k$ for $k>1$, we convert a linear regression model into a $k$-order polynomial regression model.

The simplest form for linear regression model is $p=1$, which we only have coefficients $\beta_0,\beta_1$ and each data point is a ordered pair $(x_i,y_i)$. The solution would be

Such solution is in the sense that provides the BLUE — best linear unbiased estimator of the form $y=\beta_0 + \beta_1 x.$ We consider only this type of linear regression model below, with the notation $\hat{y}_i = \hat{\beta}_0 + \hat{\beta}_1 x_i$

Now the question is how well this estimator is given a particular set of input. It turns out there are different ways to answer this.

(1) Is there a linear relationship between $x$ and $y$?

If so, it should be $\beta_1\ne 0$. In general we can ask if $\beta_1 \ne \beta_1^{\ast}$ for some known $\beta_1^{\ast}$ derived from some hypothesis. In this case the t test is handy, which use the test statistic1

which $T$ should follow t-distribution with $n-2$ df under the null hypothesis (i.e. $\hat{\beta}_1 = \beta_1^{\ast}$). We can test if $T$ falls between $\pm t_{\alpha/2,n-2}$.

(2) Is the intercept $\beta_0$ significant?

Similarly, we can use the t test with statitics2

A side note: Given the confidence level $\alpha$ and SE of $\hat{\beta}_0$ and $\hat{\beta}_1$, we can use t-distribution to derive the confidence interval of each coefficient and model output

Because the SE decreases as $n$ increases, we can applying this reversely to check if (but not easy to derive due to dependency of variation of inputs $x_i$) the sample size $n$ is large enough given the error margin of $y_i$ and confidence level $\alpha$.

(3) How accurate is the regression fit the data?

This is asking if the distribution of data is really what the regresion describes. To check if a model fits data, we use ANOVA. We define

• Total sum of square: $TSS = \sum_i (y_i - \bar{y})^2$
• Regression sum of square: $RSS = \sum_i (\hat{y}_i - \bar{y})^2$
• Error sum of square: $ESS = \sum_i (y_i - \hat{y}_i)^2$

and then $TSS = RSS + ESS$. The RSS, with df of 1, explains the variability of $\hat{y}_i$ due to variability of input $x_i$. ESS, with df of $n-2$ is the remaining variability not explained by the regression model, which $\hat{\sigma}^2 = ESS/(n-2)$ is the estimator for the variation of $\epsilon$. The total variability TSS has a df of $n-1$.

We can then use the F-test3 with statistic

against the F-distribution with df $(1,n-2)$. The linear model is a good fit if $% $ or equivalently the $p$-value of the linear model is given by

Indeed, a related measure is the coefficient of determination $R^2$, defined as

which accounts for the proportion of variability in data explained by the regression model. A good fit if $R^2\to 1$.

## Interpreting Julia’s GLM package

As an example, we will see the abovementioned metrics from any statistical packages, such as GLM in Julia.

Consider the Phillips curve, namely, the empirical model that unemployment and inflation are correlated (Milton Friedman’s interpretation). To show how accurate is this hypothesis, we check the linear regression model between inflation rate (based on CPI, YoY) and unemployment rate. Trading Economics has these data since 1981.

using CSV
using DataFrames

phillips = join(cpi, ue, on=:Date, kind=:inner)
plot(phillips, x=:UE, y=:Inf, Geom.point,
Guide.XLabel("Unemployment"), Guide.YLabel("Inflation"),
Guide.Title("Inflation vs Unemployment from "
* string(minimum(cpi[:Date])) * " to " * string(maximum(cpi[:Date])))
)


Perform linear regression (ordinary least square method) against the data, and plot the regression result:

using GLM
ols = lm(@formula(Inf ~ UE), phillips)
intercept = coef(ols)[1]
slope = coef(ols)[2]
xx = collect(0:0.1:10)
yy = intercept + xx*slope
plot(layer(phillips, x=:UE, y=:Inf, Geom.point),
layer(x=xx, y=yy, Geom.line),
Guide.XLabel("Unemployment"), Guide.YLabel("Inflation"),
Guide.Title("Inflation vs Unemployment"))


The function call lm() will print the following into the console:

Formula: Inf ~ 1 + UE

Coefficients:
Estimate Std.Error  t value Pr(>|t|)
(Intercept)    11.961  0.332921  35.9274   <1e-99
UE           -2.05903 0.0822117 -25.0454   <1e-85


The estimate column above are coefficients of the constant term and the unemployment rate variable (i.e., $\hat{\beta}_0, \hat{\beta}_1$). It suggests that the inflation $I$ and unemployment $U$ are related as $I=11.961-2.05903U$. The std error column is the numerical values of $SE(\hat{\beta}_0),SE(\hat{\beta}_1)$. The t value column is for $\beta_0^{\ast}=\beta_1^{\ast}=0$ and represents the test statistic $T=\hat{\beta}_i/SE(\hat{\beta}_i)$. Finally, the probability column shows the confidence level $\alpha$ corresponding to the t value. Such value suggests that there is very strong correlation between the two.

$R^2$ is given by r2(ols), which gives the result 0.5893907142373523. From this we see there is quite significant noise that is not covered by the model.

In this exercise, we are using 439 data points. If we limit the data to, say, last 10 years, the GLM result will give a much weaker $R^2$, greater std error, smaller t value, and lower confidence level, even though the correlation is still established.

1. Wikipedia article on Student’s t-test

2. Wikipedia article on simple linear regression

3. F-test accounts for the ratio between two chi-distributions