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

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

## ----example------------------------------------------------------------------
sim <- simulate_combined(200, ar = c(1.34, -0.42), skedastic = "exp", seed = 5)
fit <- fgls(y ~ x, data = sim, seed = 1)
summary(fit)
compare_fgls(OLS = lm(y ~ x, data = sim), `F-C` = fit$stages$serial, `F-CH` = fit)

## ----first-stage--------------------------------------------------------------
sim_ne <- simulate_combined(300, ar = 0.8, skedastic = "x2", gamma = 1, seed = 6)
with_fs <- fgls(y ~ x, data = sim_ne, seed = 1)
without_fs <- fgls(y ~ x, data = sim_ne, seed = 1, first_stage_correction = FALSE)
c(with = sqrt(vcov(with_fs)[2, 2]), without = sqrt(vcov(without_fs)[2, 2]))

## ----homo---------------------------------------------------------------------
hom <- simulate_combined(200, ar = 0.5, skedastic = "homoskedastic", seed = 4)
fit_h <- fgls(y ~ x, data = hom, seed = 1)
fit_h$skedastic$selected
all.equal(coef(fit_h), coef(fit_h$stages$serial))

## ----bootstrap----------------------------------------------------------------
fb <- fgls(y ~ x, data = sim, seed = 1, vcov = "bootstrap", control = fgls_control(n_boot = 19))
cbind(analytic = sqrt(diag(vcov(fit))), bootstrap = sqrt(diag(vcov(fb))))

