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

## ----setup, fig.width=7-------------------------------------------------------
library(SpatialInference)
library(sf); library(ggplot2)
library(lfe)
library(modelsummary)
data("US_counties_centroids")

ggplot(US_counties_centroids) + geom_sf(size = .1) + theme_bw()

## ----fig.show='hold', fig.width=7---------------------------------------------
ggplot(US_counties_centroids) + geom_sf(aes(col = noise1), size = .1) + theme_bw()
ggplot(US_counties_centroids) + geom_sf(aes(col = noise2), size = .1) + theme_bw()

## -----------------------------------------------------------------------------
ggplot(US_counties_centroids) + geom_sf(aes(col = dist), size = .1) + theme_bw()

## -----------------------------------------------------------------------------
spuriouslm <- fixest::feols(noise1 ~ noise2, data = US_counties_centroids, vcov = "HC1")
spuriouslm

## -----------------------------------------------------------------------------
US_counties_centroids$resid <- spuriouslm$residuals
ggplot(US_counties_centroids) +
  geom_sf(aes(col = resid), size = .1) + theme_bw() + scale_color_viridis_c()

## ----fig.width=7--------------------------------------------------------------
covgm_range(US_counties_centroids) + theme_bw()

## -----------------------------------------------------------------------------
# Compute the covariogram manually
covgm <- gstat::variogram(
  spuriouslm$residuals ~ 1,
  data = sf::as_Spatial(US_counties_centroids),
  covariogram = TRUE,
  width = 2e4,
  cutoff = as.numeric(max(sf::st_distance(US_counties_centroids))) * (2/3)
)
estimated_range <- extract_corr_range(covgm)
estimated_range

## ----fig.width=7--------------------------------------------------------------
inverseu_plot_conleyrange(US_counties_centroids, seq(1, 2501, by = 200)) + theme_bw()

## -----------------------------------------------------------------------------
spuriouslm_fe <- felm(noise1 ~ noise2 | unit + year | 0 | lat + lon,
                       data = US_counties_centroids, keepCX = TRUE)
regfe_conley <- conley_SE(reg = spuriouslm_fe, unit = "unit", time = "year",
                          lat = "lat", lon = "lon",
                          kernel = "epanechnikov", dist_fn = "Haversine",
                          dist_cutoff = 831)

conley <- sapply(regfe_conley, function(x) diag(sqrt(x)))[2] %>% round(5)
conley

## -----------------------------------------------------------------------------
spuriouslm_fe$coefficients[1] / as.numeric(conley)

## -----------------------------------------------------------------------------
compute_conley_lfe(spuriouslm_fe, cutoff = 831, kernel_choice = "epanechnikov")

## -----------------------------------------------------------------------------
lmsac.out <- lm_sac("noise1 ~ noise2 | unit + year | 0 | lat + lon",
                     US_counties_centroids,
                     conley_cutoff = 831, conley_kernel = "epanechnikov")

lmsac.out$conley_SE

gm.param <- list(
  list("raw" = "nobs", "clean" = "Observations", "fmt" = 0),
  list("raw" = "Moran_y", "clean" = "Moran's I [y]", "fmt" = 3),
  list("raw" = "Moran_resid", "clean" = "Moran's I [resid]", "fmt" = 3),
  list("raw" = "y_mean", "clean" = "Mean [y]", "fmt" = 3),
  list("raw" = "y_SD", "clean" = "Std.Dev. [y]", "fmt" = 3)
)

modelsummary(lmsac.out,
             estimate = c("{estimate}"),
             statistic = c("({std.error})", "([{conley}])"),
             gof_omit = "Model|Range_resid|Range_resp|Range_y",
             gof_map = gm.param)

## -----------------------------------------------------------------------------
compute_conley_lfe(spuriouslm_fe, cutoff = 831, kernel_choice = "uniform")
compute_conley_lfe(spuriouslm_fe, cutoff = 831, kernel_choice = "epanechnikov")
compute_conley_lfe(spuriouslm_fe, cutoff = 831, kernel_choice = "bartlett")
compute_conley_lfe(spuriouslm_fe, cutoff = 831, kernel_choice = "parzen")
compute_conley_lfe(spuriouslm_fe, cutoff = 831, kernel_choice = "gaussian")
compute_conley_lfe(spuriouslm_fe, cutoff = 831, kernel_choice = "biweight")

