w_tidy: Tidy a pre-specified spatial weights matrix

Description Usage Arguments Value Examples

View source: R/w_tidy.R

Description

This function tidies a pre-specified spatial weights matrix. Using a unique ID from a data frame as well as a model formula, this function subsets an existing W-matrix by dropping rows and columns which correspond to missing values in the data.

Usage

1
w_tidy(formula, data, id, w)

Arguments

formula

A model formula to identify appropriate observations for subsetting

data

A data frame containing variables to be used in a spatial model

id

A unique ID variable that corresponds to row and column names in W

w

A spatial weights matrix containing observations that correspond to data rows with missing values

Value

A subset w which has dropped rows and columns that have missing values on variables in the supplied formula.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
n   <- 20
dat <- data.frame(obs_id = 1:n, x1 = rnorm(n))
dat$x1[sample(1:20, size = 3)] <- NA

w   <- w_sim(n)
rownames(w) <- colnames(w) <- dat$obs_id

w2 <- w_tidy(formula = ~x1,
             data    = dat,
             id      = "obs_id",
             w       = w)

loganstundal/spatialhelpers documentation built on Dec. 21, 2021, 11:46 a.m.