match_restrictions {rspa} | R Documentation |
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,
...
)
Arguments
dat |
A |
restrictions |
An object of class |
adjust |
(optional) A |
weight |
A weight vector of length |
remove_tag |
if a value position indicator is present, remove it? |
... |
arguments passed to |
Value
dat
, with values adapted.
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
Examples
# 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)