title: “BSTA 2237 Econometrics I - Practical One” author: “CLIFF ODHHIAMBO” date: “2026-04-14” output: html_document ———————

Introduction

This practical analyzes economic data on:

Load Required Libraries

```{r} id=“bq3m8k” # Install if missing if (!require(lmtest)) install.packages(“lmtest”)

library(lmtest)


## Load the Data

```{r} id="qk9p2d"
# Dataset created directly (no CSV needed)

data <- data.frame(
  date = as.Date(c(
    "1967-07-01","1967-08-01","1967-09-01","1967-10-01","1967-11-01",
    "1967-12-01","1968-01-01","1968-02-01","1968-03-01","1968-04-01",
    "1968-05-01","1968-06-01","1968-07-01","1968-08-01","1968-09-01"
  )),
  pce = c(506.7,509.8,515.6,512.2,517.4,525.1,530.9,533.6,544.3,544,
          549.8,556.3,563.2,567,568.2),
  psavert = c(12.6,12.6,11.9,12.9,12.8,11.8,11.7,12.3,11.7,12.3,
              12,11.7,10.7,10.5,10.6)
)

str(data)

Exploratory Analysis

```{r} id=“wq7t1n” summary(data)

plot(data\(date, data\)pce, type = “l”, main = “Personal Consumption Over Time”, xlab = “Date”, ylab = “PCE”)

plot(data\(date, data\)psavert, type = “l”, main = “Savings Rate Over Time”, xlab = “Date”, ylab = “Savings Rate”)


## Question 1: Estimate Serial Correlation

```{r} id="h3t6yz"
model <- lm(pce ~ psavert, data = data)
summary(model)

```{r} id=“m8k4vd” residuals_model <- resid(model)

plot(data$date, residuals_model, type = “l”, main = “Residuals Over Time”, xlab = “Date”, ylab = “Residuals”)


```{r} id="n2x7sp"
dwtest(model)

{r} id="c5z9lj" e <- resid(model) dw_stat <- sum(diff(e)^2) / sum(e^2) dw_stat

```{r} id=“p1v8rm” lag_resid <- c(NA, e[-length(e)])

dw_table <- data.frame( Residual_t = e, Residual_t_1 = lag_resid, Difference = e - lag_resid, Squared_Diff = (e - lag_resid)^2 )

head(dw_table)


## Question 2: R Program

```{r} id="k7d3fa"
estimate_serial_correlation <- function(model) {
  e <- resid(model)
  dw_stat <- sum(diff(e)^2) / sum(e^2)
  
  cat("Durbin-Watson Statistic:", dw_stat, "\n")
  
  if (dw_stat < 2) {
    cat("Positive Serial Correlation\n")
  } else if (dw_stat > 2) {
    cat("Negative Serial Correlation\n")
  } else {
    cat("No Serial Correlation\n")
  }
}

estimate_serial_correlation(model)

Conclusion