title: “BSTA 2237 Econometrics I - Practical One” author: “CLIFF ODHHIAMBO” date: “2026-04-14” output: html_document ———————
This practical analyzes economic data on:
if (!require(lmtest)) install.packages("lmtest")
## Loading required package: lmtest
## Warning: package 'lmtest' was built under R version 4.5.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.5.3
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(lmtest)
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)
## 'data.frame': 15 obs. of 3 variables:
## $ date : Date, format: "1967-07-01" "1967-08-01" ...
## $ pce : num 507 510 516 512 517 ...
## $ psavert: num 12.6 12.6 11.9 12.9 12.8 11.8 11.7 12.3 11.7 12.3 ...
summary(data)
## date pce psavert
## Min. :1967-07-01 Min. :506.7 Min. :10.50
## 1st Qu.:1967-10-16 1st Qu.:516.5 1st Qu.:11.70
## Median :1968-02-01 Median :533.6 Median :11.90
## Mean :1968-01-31 Mean :536.3 Mean :11.87
## 3rd Qu.:1968-05-16 3rd Qu.:553.0 3rd Qu.:12.45
## Max. :1968-09-01 Max. :568.2 Max. :12.90
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")
model <- lm(pce ~ psavert, data = data)
summary(model)
##
## Call:
## lm(formula = pce ~ psavert, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -20.0421 -9.3741 0.2298 5.6751 17.8267
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 817.34 48.89 16.72 3.6e-10 ***
## psavert -23.67 4.11 -5.76 6.6e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 11.86 on 13 degrees of freedom
## Multiple R-squared: 0.7185, Adjusted R-squared: 0.6968
## F-statistic: 33.18 on 1 and 13 DF, p-value: 6.596e-05
residuals_model <- resid(model)
plot(data$date, residuals_model, type = "l",
main = "Residuals Over Time",
xlab = "Date", ylab = "Residuals")
dwtest(model)
##
## Durbin-Watson test
##
## data: model
## DW = 0.87479, p-value = 0.002894
## alternative hypothesis: true autocorrelation is greater than 0
e <- resid(model)
dw_stat <- sum(diff(e)^2) / sum(e^2)
dw_stat
## [1] 0.8747937
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)
## Residual_t Residual_t_1 Difference Squared_Diff
## 1 -12.3717945 NA NA NA
## 2 -9.2717945 -12.3717945 3.100000 9.610000
## 3 -20.0420842 -9.2717945 -10.770290 115.999141
## 4 0.2297583 -20.0420842 20.271842 410.947598
## 5 3.0625740 0.2297583 2.832816 8.024845
## 6 -12.9092684 3.0625740 -15.971842 255.099752
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)
## Durbin-Watson Statistic: 0.8747937
## Positive Serial Correlation