## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----setup--------------------------------------------------------------------
library(fgls)

## ----serial-------------------------------------------------------------------
sim <- simulate_serial(500, seed = 1)
fit <- serial_fgls(y ~ x, data = sim)
summary(fit)

## ----serial-compare-----------------------------------------------------------
compare_fgls(OLS = lm(y ~ x, data = sim), FGLS = fit)

## ----hetero-------------------------------------------------------------------
het <- simulate_hetero(200, skedastic = "log2", seed = 2)
fit_h <- lasso_fgls(y ~ x, data = het, seed = 1)
fit_h$skedastic$selected
compare_fgls(OLS = lm(y ~ x, data = het), FGLS = fit_h)

## ----combined-----------------------------------------------------------------
both <- simulate_combined(200, ar = 0.5, skedastic = "x2", seed = 3)
fit_b <- fgls(y ~ x, data = both, seed = 1)
fit_b
compare_fgls(`F-C` = fit_b$stages$serial, `F-CH` = fit_b)

## ----methods------------------------------------------------------------------
coef(fit)
confint(fit, level = 0.9)
predict(fit, newdata = data.frame(x = c(-1, 0, 1)), interval = "confidence")
vcov(fit, type = "naive") # PG26 eq. (27), valid only for exogenous regressors

## ----ts-----------------------------------------------------------------------
dates <- seq(as.Date("2000-01-01"), by = "month", length.out = 500)
df <- data.frame(date = dates, sim)
serial_fgls(y ~ x, data = df, time = "date")$ar_order

