Linear model

No

Yu Cheng Hsu

Linear Regression & Diagnostics

Introduction to the Linear Regression Model

Linear regression model is formally defined as \[ \begin{aligned} y_{i} = & \beta_{0} + \beta_{1}x_{1i} + \beta_{2}x_{2i} + \dots + \beta_{p}x_{pi} + \epsilon_{i} \text{, or}\\ \mathbb{y} = &\mathbb{\beta}\mathbb{X}+ \mathbb{\epsilon} \end{aligned} \]

  • Systematic Part (Independent variable, predictor)
  • Random Part: Individual observations of the outcome vary by an error term \(\epsilon_{i}\).
  • It is assumed that \(\epsilon_{i} \sim \mathcal{N}(0, \sigma_{\epsilon}^{2})\)

Prediction in regression

Confidence Interval (CI) for the Mean Response:

  • Definition: the average expected outcome for all individuals with a specific predictor value \(x\)
  • Uncertainty: Estimated regression line

Prediction Interval (PI) for a New Observation:

  • Definition: Predict the specific outcome \(y\) for a single, new individual with a specific predictor value \(x\).
  • Uncertainty: Estimated regression line, and and random scatter of individual data points around that line

Evaluating regression

  • \(y_i\): The actual observed value.
  • \(\hat{y}_i\): The predicted value on the regression line.
  • \(\bar{y}\): The mean of all observed \(y\) values.

The following relationship holds \[TSS = ESS + RSS\] TSS (Total Sum of Squares): \[\sum (y_i - \bar{y})^2\] ESS (Explained Sum of Squares): \[\sum (\hat{y}_i - \bar{y})^2\] RSS (Residual Sum of Squares):\[\sum (y_i - \hat{y}_i)^2\]

\(R^2\): The Coefficient of Determination

  • \(R^2\) is the percentage of variance explaine dby the linear model

\[ R^2 = \frac{ESS}{TSS} = 1 - \frac{RSS}{TSS} \]

-\(R^2 = 0\): the model explains none of the variance in the outcome (the regression line is flat at the mean, \(\bar{y}\)). - \(R^2 = 1\): the model explains 100% of the variance (every data point falls exactly on the regression line, meaning RSS is zero).

Inference on linear model

  • Even we have \(R^2\), we need to know if explained variance s statistically significant.

  • Null Hypothesis \[ H_0: \beta_1 = \beta_2 = \dots = \beta_p = 0 \]

  • Test statistics

\[ F = \frac{ESS / df_{ESS}}{RSS / df_{RSS}} \sim F_{df_{ESS},df_{RSS}} \]

  • \(df_{ESS} = p\) (the number of predictor variables)
  • \(df_{RSS} = n - (p + 1)\) (the number of observations minus the total number of estimated parameters, including the intercept)

Inference on individual \(\beta_i\)

  • Investigate each predictor makes statisticaly significant contribution toward outcome

  • Null Hypothesis \[ H_0: \beta_i = 0 \]

  • Test statistics

\[ \beta_i \sim t_{n-(p+1)}(0,\frac{s_{y\vert{}x}^{2}}{(n-1)s_{x_i}^{2}(1-r_{i}^{2})}) \]

  • Implication in linear model: Including other predictors to control for confounding commonly has the effect of difficult to reject the null hypothesis
  • Some of the unadjusted association is explained by those other predictors.

Assumption on linear regression

  1. Linearity
  2. Normality of erros
  3. Independece of errors
  4. Homoscedasticity
  5. No multicollinearity

Linearity

The model assumes that the average value of the outcome changes linearly with each continuous predictor. Departures from linearity can be checked using Residual versus Predictor (RVP) plots or Component-Plus-Residual (CPR) plots. Applying a nonparametric smoother (like LOWESS) to these plots helps detect nonlinear trends in the residuals. If nonlinearity is found, you may need to apply transformations to the predictor (e.g., adding quadratic terms, log transformations) or categorize the continuous predictor.

Normality of Errors

The error term \(\epsilon\) is assumed to have a normal distribution. This can be assessed using histograms, boxplots, kernel density estimates, and normal quantile-quantile (Q-Q) plots applied to the residuals. In a normal Q-Q plot, upward curvature is diagnostic of right-skewness. Transforming the outcome variable (e.g., replacing \(y\) with \(log(y)\)) is often successful for reducing the skewness of residuals.

Constant Variance (Homoscedasticity)

The model assumes that the variance of the error term, \(\sigma_{\epsilon}^{2}\), is constant across all observations. Violations of this assumption are known as heteroscedasticity, which can affect the validity of confidence intervals and P-values. Homoscedasticity is evaluated using RVP plots and Residual versus Fitted (RVF) plots. A horizontal funnel shape in these scatterplots is a signal of heteroscedasticity.

Influential Points and Outliers

High leverage points are x-outliers with the potential to exert undue influence on regression coefficient estimates. Influential points are specific observations that have actually exerted an undue influence on the estimates. DFBETA statistics are recommended to quantify how much each coefficient would change if a specific observation were omitted from the dataset. IV. Code Illustrations: Python and RBelow are examples of how to fit a multiple linear regression model and generate the necessary diagnostic plots in both Python

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      Y   R-squared:                       0.810
Model:                            OLS   Adj. R-squared:                  0.808
Method:                 Least Squares   F-statistic:                     420.6
Date:                Fri, 24 Jul 2026   Prob (F-statistic):           7.92e-72
Time:                        16:58:45   Log-Likelihood:                -821.84
No. Observations:                 200   AIC:                             1650.
Df Residuals:                     197   BIC:                             1660.
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         25.7620      7.528      3.422      0.001      10.917      40.607
X1             2.2881      0.114     20.148      0.000       2.064       2.512
X2            -1.2151      0.054    -22.687      0.000      -1.321      -1.110
==============================================================================
Omnibus:                        1.716   Durbin-Watson:                   1.916
Prob(Omnibus):                  0.424   Jarque-Bera (JB):                1.565
Skew:                           0.217   Prob(JB):                        0.457
Kurtosis:                       3.012   Cond. No.                         822.
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

# 1. Generate Synthetic Data
set.seed(42)
n <- 200
X1 <- rnorm(n, mean=50, sd=10)
X2 <- rnorm(n, mean=100, sd=20)
# Create outcome with normal noise
Y <- 15 + 2.5*X1 - 1.2*X2 + rnorm(n, mean=0, sd=15)

data <- data.frame(Y, X1, X2)

# 2. Fit the Linear Regression Model
model <- lm(Y ~ X1 + X2, data=data)

# Print model summary (Contains F-test, RSS, and coefficients)
print(summary(model))

# 3. Residual Diagnostics
# R has built-in diagnostic plots that are generated by plotting the model object
par(mfrow=c(2,2)) # Set up a 2x2 grid for plots

# Plot 1: Residuals vs Fitted (checks linearity and constant variance)
# Plot 2: Normal Q-Q (checks normality of residuals)
# Plot 3: Scale-Location (checks homoscedasticity)
# Plot 4: Residuals vs Leverage (checks for influential points / high leverage)
plot(model)