Spatial first differences
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Code for computing the spatial first differences (SFD) estimator. SFD is a research design that exploits the spatial structure of the data to address unobservable heterogeneity in cross section. Please see Druckenmiller & Hsiang, 2018.


Currently, this package exists in a development version on GitHub. To use the package, you need to install it directly from GitHub using the install_github function from devtools.



The package can be used to compute spatial first differences (SFD) or spatial double differences (SDD). For example, you can compute SFD in your variables of interest using the SFD_vars function.

SFD_data <- SFD_vars(spatial_df =, 
    n_channel = 50, 
    obs_var = "fips", 
    dependent_var = "logYield", 
    independent_vars = c("temp", "prec"))

The above code calculates SFD for the variables logYield, temp, and prec. The inputs are a SpatialPolygonsDataFrame, a unique identifer for the observational unit fips, and a specification for the number of sampling channels 50. The number sampling channels should be chosen such that the height of your sampling channels is approximately equal to the height of your observational units. The n_channels function inputs your SpatialPolygonsDataFrame and provides a recommendation for the number of sampling channels.

n_channels <- SFD_vars(spatial_df =

Alternatively, the user can directly compute the SFD estimator using the function SFD_lm. This function performs a linear regression on the data after taking SFD.

sfdmodel <- SFD_lm(spatial_df =, 
    n_channel = 50, 
    obs_var = "fips", 
    dependent_var = "logYield", 
    independent_vars = c("temp", "prec"), 
    plot = TRUE)

The plot=TRUE option produces a plot the adjacent observations used to calculate SFD in your sample. We recommend checking this plot to make sure the algorithm identifies reasonable neighbors.