| Title: | Matrices for Repeat-Sales Price Indexes |
|---|---|
| Description: | Calculate the matrices in Shiller (1991, <doi:10.1016/S1051-1377(05)80028-2>) that serve as the foundation for many repeat-sales price indexes. |
| Authors: | Steve Martin [aut, cre, cph] (ORCID: <https://orcid.org/0000-0003-2544-9480>) |
| Maintainer: | Steve Martin <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.3.0.9001 |
| Built: | 2026-10-18 06:28:38 UTC |
| Source: | https://codeberg.org/marberts/rsmatrix |
Create a function to compute the , , , and
matrices in Shiller (1991, section I-II) from sales-pair data in
order to calculate a repeat-sales price index.
rs_matrix(t2, t1, p2, p1, f = NULL, sparse = FALSE)rs_matrix(t2, t1, p2, p1, f = NULL, sparse = FALSE)
t2, t1
|
|
p2, p1
|
|
f |
|
sparse |
|
The function returned by rs_matrix() computes a generalization of the
matrices in Shiller (1991, section I-II) that are applicable to
grouped data. These are useful for calculating separate indexes for many,
say, cities without needing an explicit loop.
The , , and matrices are not well defined if either
t1 or t2 have missing values, and an error is thrown in this
case. Similarly, it should always be the case that t2 > t1, otherwise
a warning is given.
A function that takes a single argument naming the desired matrix.
It returns one of two matrices ( and ) or two vectors
( and ), either regular matrices if sparse = FALSE, or sparse
matrices of class dgCMatrix if sparse = TRUE.
Bailey MJ, Muth RF, Nourse HO (1963). “A regression method for real estate price index construction.” Journal of the American Statistical Association, 53(304), 933-942.
Shiller RJ (1991). “Arithmetic repeat sales price estimators.” Journal of Housing Economics, 1(1), 110-126. doi:10.1016/S1051-1377(05)80028-2.
rs_pairs() for turning sales data into sales pairs.
# Make some data x <- data.frame( date = c(3, 2, 3, 2, 3, 3), date_prev = c(1, 1, 2, 1, 2, 1), price = 6:1, price_prev = 1 ) # Calculate matrices mat <- with(x, rs_matrix(date, date_prev, price, price_prev)) Z <- mat("Z") # Z matrix X <- mat("X") # X matrix y <- mat("y") # y vector Y <- mat("Y") # Y vector # Calculate the GRS index in Bailey, Muth, and Nourse (1963) b <- solve(crossprod(Z), crossprod(Z, y))[, 1] # or b <- qr.coef(qr(Z), y) (grs <- exp(b) * 100) # Standard errors vcov <- rs_var(y - Z %*% b, Z) sqrt(diag(vcov)) * grs # delta method # Calculate the ARS index in Shiller (1991) b <- solve(crossprod(Z, X), crossprod(Z, Y))[, 1] # or b <- qr.coef(qr(crossprod(Z, X)), crossprod(Z, Y)) (ars <- 100 / b) # Standard errors vcov <- rs_var(Y - X %*% b, Z, X) sqrt(diag(vcov)) * ars^2 / 100 # delta method # Works with grouped data x <- data.frame( date = c(3, 2, 3, 2), date_prev = c(2, 1, 2, 1), price = 4:1, price_prev = 1, group = c("a", "a", "b", "b") ) mat <- with(x, rs_matrix(date, date_prev, price, price_prev, group)) b <- solve(crossprod(mat("Z"), mat("X")), crossprod(mat("Z"), mat("Y")))[, 1] 100 / b# Make some data x <- data.frame( date = c(3, 2, 3, 2, 3, 3), date_prev = c(1, 1, 2, 1, 2, 1), price = 6:1, price_prev = 1 ) # Calculate matrices mat <- with(x, rs_matrix(date, date_prev, price, price_prev)) Z <- mat("Z") # Z matrix X <- mat("X") # X matrix y <- mat("y") # y vector Y <- mat("Y") # Y vector # Calculate the GRS index in Bailey, Muth, and Nourse (1963) b <- solve(crossprod(Z), crossprod(Z, y))[, 1] # or b <- qr.coef(qr(Z), y) (grs <- exp(b) * 100) # Standard errors vcov <- rs_var(y - Z %*% b, Z) sqrt(diag(vcov)) * grs # delta method # Calculate the ARS index in Shiller (1991) b <- solve(crossprod(Z, X), crossprod(Z, Y))[, 1] # or b <- qr.coef(qr(crossprod(Z, X)), crossprod(Z, Y)) (ars <- 100 / b) # Standard errors vcov <- rs_var(Y - X %*% b, Z, X) sqrt(diag(vcov)) * ars^2 / 100 # delta method # Works with grouped data x <- data.frame( date = c(3, 2, 3, 2), date_prev = c(2, 1, 2, 1), price = 4:1, price_prev = 1, group = c("a", "a", "b", "b") ) mat <- with(x, rs_matrix(date, date_prev, price, price_prev, group)) b <- solve(crossprod(mat("Z"), mat("X")), crossprod(mat("Z"), mat("Y")))[, 1] 100 / b
Turn repeat-sales data into sales pairs that are suitable for making repeat-sales matrices.
rs_pairs(period, product, match_first = TRUE)rs_pairs(period, product, match_first = TRUE)
period |
|
product |
|
match_first |
|
A numeric vector of indices giving the position of the previous sale
for each product, with the convention that the previous sale for the
first sale is itself if match_first = TRUE, NA otherwise. Ties are
resolved according to the order they appear in period.
base::order() is the workhorse of rs_pairs(), so performance can be
sensitive to the types of period and product, and can be slow for large
character vectors.
rs_matrix() for using sales pairs to make a repeat-sales index.
rtCreateTrans() in the hpiR package for a feature-rich but
slower and less flexible function to make sales pairs.
# Make sales pairs x <- data.frame( id = c(1, 1, 1, 3, 2, 2, 3, 3), date = c(1, 2, 3, 2, 1, 3, 4, 1), price = c(1, 3, 2, 3, 1, 1, 1, 2) ) pairs <- rs_pairs(x$date, x$id) x[c("date_prev", "price_prev")] <- x[c("date", "price")][pairs, ] x# Make sales pairs x <- data.frame( id = c(1, 1, 1, 3, 2, 2, 3, 3), date = c(1, 2, 3, 2, 1, 3, 4, 1), price = c(1, 3, 2, 3, 1, 1, 1, 2) ) pairs <- rs_pairs(x$date, x$id) x[c("date_prev", "price_prev")] <- x[c("date", "price")][pairs, ] x
Convenience function to compute a cluster-robust variance matrix for a linear regression, with or without instruments, where clustering occurs along one dimension. Useful for calculating a variance matrix when a regression is calculated manually.
rs_var(u, Z, X = Z, ids = seq_len(nrow(X)), df = NULL)rs_var(u, Z, X = Z, ids = seq_len(nrow(X)), df = NULL)
u |
|
Z |
|
X |
|
ids |
|
df |
|
This function calculates the standard robust variance matrix for a linear
regression, as in Manski (1988, section 8.1.2);
that is, . It is useful
when a regression is calculated by hand. This generalizes the variance
matrix proposed by Shiller (1991, section II) when a property sells
more than twice.
This function gives the same result as vcovHC(x, type = "sss", cluster = 'group') from the plm package.
A covariance matrix.
Manski C (1988). Analog Estimation Methods in Econometrics. Chapman and Hall.
Shiller RJ (1991). “Arithmetic repeat sales price estimators.” Journal of Housing Economics, 1(1), 110-126. doi:10.1016/S1051-1377(05)80028-2.
# Makes some groups in mtcars mtcars$clust <- letters[1:4] # Matrices for regression x <- model.matrix(~ cyl + disp, mtcars) y <- matrix(mtcars$mpg) # Regression coefficients b <- solve(crossprod(x), crossprod(x, y)) # Residuals r <- y - x %*% b # Robust variance matrix vcov <- rs_var(r, x, ids = mtcars$clust) ## Not run: # Same as plm library(plm) mdl <- plm(mpg ~ cyl + disp, mtcars, model = "pooling", index = "clust") vcov2 <- vcovHC(mdl, type = "sss", cluster = "group") vcov - vcov2 ## End(Not run)# Makes some groups in mtcars mtcars$clust <- letters[1:4] # Matrices for regression x <- model.matrix(~ cyl + disp, mtcars) y <- matrix(mtcars$mpg) # Regression coefficients b <- solve(crossprod(x), crossprod(x, y)) # Residuals r <- y - x %*% b # Robust variance matrix vcov <- rs_var(r, x, ids = mtcars$clust) ## Not run: # Same as plm library(plm) mdl <- plm(mpg ~ cyl + disp, mtcars, model = "pooling", index = "clust") vcov2 <- vcovHC(mdl, type = "sss", cluster = "group") vcov - vcov2 ## End(Not run)