Maintainer: Mark van der Loo <mark.vanderloo@gmail.com>
License: GPL-3
Title: Adapt Numerical Records to Fit (in)Equality Restrictions
Type: Package
LazyLoad: yes
Author: Mark van der Loo
Description: Minimally adjust the values of numerical records in a data.frame, such that each record satisfies a predefined set of equality and/or inequality constraints. The constraints can be defined using the 'validate' package. The core algorithms have recently been moved to the 'lintools' package, refer to 'lintools' for a more basic interface and access to a version of the algorithm that works with sparse matrices.
Version: 0.2.8
Depends: R (≥ 2.13.0)
Imports: graphics, stats, validate, lintools
Suggests: editrules, tinytest
URL: https://github.com/markvanderloo/rspa
BugReports: https://github.com/markvanderloo/rspa/issues
RoxygenNote: 7.2.1
Encoding: UTF-8
NeedsCompilation: yes
Packaged: 2022-12-22 09:46:27 UTC; mark
Repository: CRAN
Date/Publication: 2022-12-22 10:00:02 UTC

A package for minimal vector adjustment.

Description

Given a vector \boldsymbol{x}^0, and a set linear restrictions of the form \boldsymbol{a}_i\cdot\boldsymbol{x}_i=b_i and/or \boldsymbol{a}_i\cdot\boldsymbol{x}_i\leq b_i with i=1,2,\ldots,m. This package finds the nearest vector to \boldsymbol{x}^0 (in the (weighted) euclidean sense) that satisfies all restrictions.

Details

Much of this package's functionality, including algorithms for working with large, sparse problems has been moved to the lintools package. This package will serve as a front-end for application of the succsessive projection algorithm for data stored in data.frame like objects.


DEPRECATED Adjust a data to meet linear (in)equality constraints

Description

Adjust a vector \boldsymbol{x} to meet constraints \boldsymbol{Ax} \leq \boldsymbol{b}. As of version 0.2 this function is deprecated. Please use

Usage

adjust(object, ...)

## S3 method for class 'editmatrix'
adjust(object, x, w = rep(1, length(x)), method = c("dense", "sparse"), ...)

## S3 method for class 'sparseConstraints'
adjust(object, x, w = rep(1, length(x)), tol = 0.01, maxiter = 1000L, ...)

## S3 method for class 'matrix'
adjust(
  object,
  b,
  x,
  neq = length(b),
  w = rep(1, length(x)),
  tol = 0.01,
  maxiter = 1000L,
  ...
)

Arguments

object

an R object describing constraints (see details)

...

Arguments to be passed to other methods

x

The vector to be adjusted

w

A positive weight vector

method

use dense or sparse matrix method.

tol

The maximum allowed deviation from the constraints (see details).

maxiter

maximum number of iterations

b

Constant vector of the constraint system Ax\leq b

neq

the first neq linear relations are equalities.

Value

Object of class adjusted.

Details

adjust is a generic function allowing several definitions of the constraints in object.

The tolerance tol is defined as the maximum absolute value of the difference vector \boldsymbol{Ax}-\boldsymbol{b} for equalities. For inequalities, the difference vector is set to zero when it's value is lesser than zero (i.e. when the restriction is obeyed). The function keeps iterating until either the tolerance is met, the number of allowed iterations is exceeded or divergence is detected.

Note

adjust does not perform any consistency checks. When the system of constraints is contradictory (e.g. x>1 and x<0) this will result in either divergence or in exceeding the number of iterations.


DEPRECATED Adjust records in a data.frame

Description

A convenient wrapper around adjust that loops over all records in a data.frame. DEPRECATED. See match_restrictions.

Usage

adjustRecords(
  E,
  dat,
  adjust = array(TRUE, dim = dim(dat)),
  w = rep(1, ncol(dat)),
  verbose = FALSE,
  ...
)

Arguments

E

a editmatrix

dat

a data.frame

adjust

a nrow(dat) x ncol(dat) boolean array, indicating which fields must be adjusted.

w

a vector of length ncol(dat) or array of size adjust with adjustment weights.

verbose

print progress to console

...

extra options, passed through to adjust

Value

An object of class adjustedRecords

Details

This function is not written to be especiallty speedy or memory-efficient, but to offer a convenient interface to adjusting a data.frame of records.

See Also

adjust


Adjusted object

Description

Adjusted object

Usage

## S3 method for class 'adjusted'
print(x, maxprint = 10, ...)

Arguments

x

an object of class adjusted

maxprint

max number of output values to print

...

parameters to pass to other methds

Details

A adjusted object contains the adjusted vector as well as some information on how the adjustment was achieved. In particular, it contains the following slots (to be accessed with the dollar operator):

See Also

adjust


DEPRECATED Adjusted records

Description

As this is output from deprecated adjustRecords, this object is deprecated as of version 0.2.0 as well.

Usage

## S3 method for class 'adjustedRecords'
print(x, ...)

## S3 method for class 'adjustedRecords'
summary(object, ...)

## S3 method for class 'adjustedRecords'
plot(x, ...)

Arguments

x

object of class adjustedRecords

...

additional parameters to pass to other methods

object

object of class adjustedRecords

Details

The adjustedRecords object contains adjusted data as well as some information on the adjusting process. In particular:

When printed, only the first 10 rows of cbind(adjusted, status) are shown. Use summary for a quick overview. The plot method shows kernel density estimates of the accuracy and objective functions. To avoid densities at values below 0, the accuracy densities are evaluated under a sqrt-transform and transformed back before plotting. For the objective function values a log-transform is used.

See Also

adjustRecords


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 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.

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

tag_missing

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)



Remove cell position tags

Description

Remove cell position tags

Usage

remove_tag(dat, ...)

Arguments

dat

[data.frame]

...

Currently not used

Value

dat with tag removed

See Also

Other tagging: tag_missing(), tagged_values()


DEPRECATED Generate sparse set of constraints.

Description

This function is deprecated. Please use function sparse_constraints from package lintools instead.

Usage

sparseConstraints(x, ...)

## S3 method for class 'editmatrix'
sparseConstraints(x, tol = 1e-08, ...)

## S3 method for class 'matrix'
sparseConstraints(x, b, neq = length(b), tol = 1e-08, ...)

## S3 method for class 'data.frame'
sparseConstraints(
  x,
  b,
  neq = length(b),
  base = min(x[, 2]),
  sorted = FALSE,
  ...
)

## S3 method for class 'sparseConstraints'
print(x, range = 1L:10L, ...)

Arguments

x

R object to be translated to sparseConstraints format.

...

options to be passed to other methods

tol

Tolerance for testing where coefficients are zero

b

Constant vector

neq

The first new equations are interpreted as equality constraints, the rest as '<='

base

are the indices in x[,1:2] base 0 or base 1?

sorted

is x sorted by the first column?

range

integer vector stating which constraints to print

Value

Object of class sparseConstraints (see details).

Details

The sparseConstraints objects holds the system \boldsymbol{Ax}\leq \boldsymbol{b} in column sparse format, outside of R's memory. In R, it is a reference object. In particular, it is meaningless to


Tag currently missing elements of a data.frame

Description

Attach an attribute that marks which cells are empty (NA).

Usage

tag_missing(dat, ...)

Arguments

dat

[data.frame] to be tagged

...

Currently not used.

Value

dat, tagged for missing values.

See Also

Other tagging: remove_tag(), tagged_values()


Retrieve tagged cell positions

Description

Retrieve tagged cell positions

Usage

tagged_values(dat, ...)

Arguments

dat

[data.frame]

...

Currently not used

Value

A logical matrix, or NULL

See Also

Other tagging: remove_tag(), tag_missing()