© 2026 Dr. Debashis Chatterjee.
All rights reserved.
This laboratory manual is prepared exclusively for educational use.
Configure the R environment and explore datasets.
par(mfrow = c(1,3), mar = c(4,4,3,1))
plot(AirPassengers, col="dodgerblue", lwd=2,
main="AirPassengers\n(Trend & Seasonality)")
plot(uspop, col="firebrick", lwd=2,
main="US Population\n(Growth Curve)")
plot(nhtemp, col="darkgreen", lwd=2,
main="NH Temperature\n(Stationary)")\[ Y_t = a b^{t} \]
Taking logs:
\[ \ln(Y_t) = \ln(a) + t\ln(b) \]
y_exp <- as.numeric(uspop)[1:10]
t_exp <- 1:length(y_exp)
fit_exp <- lm(log(y_exp) ~ t_exp)
a <- exp(coef(fit_exp)[1])
b <- exp(coef(fit_exp)[2])
fitted_exp <- a * b^t_exp
plot(t_exp, y_exp, type="o", pch=16, col="black",
main="Modified Exponential Fit (US Pop 1790–1880)",
xlab="Decade Index", ylab="Population (Millions)")
lines(t_exp, fitted_exp, col="darkorange", lwd=3)
legend("topleft",
legend=c("Actual","Fitted"),
col=c("black","darkorange"),
lty=1, lwd=c(1,3), pch=c(16,NA))##
## Call:
## lm(formula = log(y_exp) ~ t_exp)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.060499 -0.012490 0.008677 0.012272 0.043371
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.111133 0.021312 52.14 2.03e-11 ***
## t_exp 0.286538 0.003435 83.42 4.76e-13 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.0312 on 8 degrees of freedom
## Multiple R-squared: 0.9989, Adjusted R-squared: 0.9987
## F-statistic: 6959 on 1 and 8 DF, p-value: 4.755e-13
\[ Y_t = K \exp(-A e^{-bt}) \]
y_pop <- as.numeric(uspop)
t_pop <- 1:length(y_pop)
gompertz_model <- nls(
y_pop ~ K * exp(-A * exp(-b * t_pop)),
start=list(K=400, A=4, b=0.15)
)
plot(t_pop, y_pop, pch=16,
main="Gompertz Fit",
xlab="Decade Index", ylab="Population")
lines(t_pop, predict(gompertz_model),
col="dodgerblue", lwd=3)\[ Y_t = \frac{K}{1 + A e^{-bt}} \]
logistic_model <- nls(
y_pop ~ K/(1 + A*exp(-b*t_pop)),
start=list(K=400, A=100, b=0.25)
)
plot(t_pop, y_pop, pch=16,
main="Gompertz vs Logistic",
xlab="Decade Index", ylab="Population")
lines(t_pop, predict(gompertz_model),
col="blue", lwd=2)
lines(t_pop, predict(logistic_model),
col="red", lwd=2, lty=2)
legend("topleft",
legend=c("Actual","Gompertz","Logistic"),
col=c("black","blue","red"),
pch=c(16,NA,NA),
lty=c(NA,1,2), lwd=2)trend_ma <- ma(AirPassengers, order=12, centre=TRUE)
plot(AirPassengers, col="gray60",
main="12-Month Centered Moving Average",
ylab="Passengers")
lines(trend_ma, col="red", lwd=3)t_air <- 1:length(AirPassengers)
fit_trend <- lm(log(AirPassengers) ~ t_air)
math_trend <- ts(exp(fitted(fit_trend)),
start=start(AirPassengers),
frequency=12)
ratio_to_trend <- AirPassengers / math_trend
si_raw <- tapply(ratio_to_trend,
cycle(AirPassengers),
mean)
si_trend <- si_raw / mean(si_raw)
kable(data.frame(Month=month.abb,
Index=round(si_trend,3)))| Month | Index |
|---|---|
| Jan | 0.910 |
| Feb | 0.891 |
| Mar | 1.016 |
| Apr | 0.984 |
| May | 0.982 |
| Jun | 1.109 |
| Jul | 1.230 |
| Aug | 1.219 |
| Sep | 1.054 |
| Oct | 0.918 |
| Nov | 0.796 |
| Dec | 0.892 |
ratio_ma <- AirPassengers / trend_ma
si_ma_raw <- tapply(ratio_ma,
cycle(AirPassengers),
mean, na.rm=TRUE)
si_ma <- si_ma_raw / mean(si_ma_raw)
barplot(si_ma, names.arg=month.abb,
col="steelblue",
main="Seasonal Indices (Ratio-to-MA)")
abline(h=1, col="red", lwd=2, lty=2)ap_lag <- stats::lag(AirPassengers, -1)
lr <- (AirPassengers / ap_lag) * 100
plot(lr, col="darkmagenta",
main="Link Relatives",
ylab="Percentage vs Previous Month")
abline(h=100, lty=2)| coef(fit_arma) | |
|---|---|
| ar1 | 0.9150693 |
| ma1 | -0.7088387 |
| intercept | 51.1689524 |
fit_ses <- ses(nhtemp, h=10)
plot(fit_ses,
main="Simple Exponential Smoothing",
ylab="Temperature")
lines(fitted(fit_ses),
col="red", lty=2)fit_auto <- auto.arima(AirPassengers)
forecast_auto <- forecast(fit_auto, h=24)
plot(forecast_auto,
main="Auto ARIMA Forecast",
ylab="Passengers")fit_holt <- holt(AirPassengers, h=24)
plot(fit_holt,
main="Holt's Linear Trend Forecast",
ylab="Passengers")## R version 4.4.0 (2024-04-24 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows 11 x64 (build 26100)
##
## Matrix products: default
##
##
## locale:
## [1] LC_COLLATE=English_United States.utf8
## [2] LC_CTYPE=English_United States.utf8
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.utf8
##
## time zone: Asia/Calcutta
## tzcode source: internal
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] knitr_1.51 tseries_0.10-60 forecast_9.0.1
##
## loaded via a namespace (and not attached):
## [1] sass_0.4.10 generics_0.1.4 lattice_0.22-9 digest_0.6.35
## [5] magrittr_2.0.3 evaluate_1.0.5 grid_4.4.0 RColorBrewer_1.1-3
## [9] fastmap_1.2.0 jsonlite_1.8.8 scales_1.4.0 jquerylib_0.1.4
## [13] cli_3.6.2 rlang_1.1.4 cachem_1.1.0 yaml_2.3.12
## [17] otel_0.2.0 tools_4.4.0 parallel_4.4.0 dplyr_1.1.4
## [21] colorspace_2.1-1 ggplot2_4.0.2 curl_5.2.1 vctrs_0.6.5
## [25] R6_2.6.1 zoo_1.8-15 lifecycle_1.0.5 pkgconfig_2.0.3
## [29] urca_1.3-4 pillar_1.11.1 bslib_0.10.0 gtable_0.3.6
## [33] glue_1.7.0 quantmod_0.4.28 Rcpp_1.0.12 xfun_0.56
## [37] tibble_3.2.1 tidyselect_1.2.1 rstudioapi_0.18.0 dichromat_2.0-0.1
## [41] farver_2.1.2 htmltools_0.5.9 nlme_3.1-168 rmarkdown_2.30
## [45] xts_0.14.1 timeDate_4052.112 fracdiff_1.5-3 compiler_4.4.0
## [49] S7_0.2.0 quadprog_1.5-8 TTR_0.24.4