Learn R Programming

rspa (version 0.2.8)

match_restrictions: Alter numeric data records to match linear (in)equality constraints.

Description

Apply the successive projection algorithm to adjust each record in dat to satisfy a set of linear (in)equality constraints.

Usage

match_restrictions(
  dat,
  restrictions,
  adjust = NULL,
  weight = rep(1, ncol(dat)),
  remove_tag = TRUE,
  ...
)

Value

dat, with values adapted.

Arguments

dat

A data.frame

restrictions

An object of class validator

adjust

(optional) A logical matrix of dimensions dim(dat) where TRUE indicates that a value may be adjusted. When missing, the tagged_values are used. If no tagging was applied, adjust will default to an all TRUE matrix with dimensions equal to dim(dat).

weight

A weight vector of length ncol(dat) or a matrix of dimensions dim(dat).

remove_tag

if a value position indicator is present, remove it?

...

arguments passed to project.

Note on inequality restrictions

All inequality restrictions of the form \(a.x < b\) are treated as \(a.x \leq b\). The idea is to project the original record \(x\) onto the boundary defined by the (in)equations. Projection on a boundary defined by a strict inequation is illdefined sice the value \(b\) in the restriction \(a.x < b\) is strictly outside the valid region.

See Also

tag_missing

Examples

Run this code

# a very simple adjustment example

v <- validate::validator(
	x + y == 10,
	x > 0,
	y > 0
)

# x and y will be adjusted by the same amount
match_restrictions(data.frame(x=4,y=5), v)

# One of the inequalies violated
match_restrictions(data.frame(x=-1,y=5), v)

# Weighted distances: 'heavy' variables change less
match_restrictions(data.frame(x=4,y=5), v, weight=c(100,1))

# if w=1/x0, the ratio between coefficients of x0 stay the same (to first order)
x0 <- data.frame(x=4,y=5)
x1 <- match_restrictions(x0, v, weight=1/as.matrix(x0))

x0[,1]/x0[,2]
x1[,1] / x1[2]

# example of tag usage
v <- validate::validator(x + y == 1, x>0,y>0)
d <- data.frame(x=NA,y=0.5)
d <- tag_missing(d)
# impute
d[1,1] <- 1

# only the tagged values will be altered. The tag is
# removed afterwards.
match_restrictions(d,v)


Run the code above in your browser using DataLab