lm {complexlm}R Documentation

Linear Model Fitting for Complex or Numeric Variables

Description

An adaptation of lm that is compatible with complex variables. If the response is not complex, it calls the standard stats::lm() Note: It is not capable of dealing with contrasts in the complex case. The formula interpretation is also unable of handle algebraic expressions in formula.

Usage

lm(
  formula,
  data,
  subset,
  weights,
  na.action,
  method = "qr",
  model = TRUE,
  x = TRUE,
  y = FALSE,
  qr = TRUE,
  singular.ok = TRUE,
  contrasts = NULL,
  offset,
  ...
)

Arguments

formula

an object of class "formula" (or one that can be coerced to that class): a symbolic description of the model to be fitted. For complex variables there are restrictions on what kinds of formula can be comprehended. See zmodel.matrix for details.

data

an optional data frame, list or environment (or object coercible by as.data.frame to a data frame) containing the variables in the model. If not found in data, the variables are taken from environment(formula), typically the environment from which lm is called.

subset

an optional vector specifying a subset of observations to be used in the fitting process.

weights

an optional vector of weights to be used in the fitting process. Should be NULL or a numeric vector. If non-NULL, weighted least squares is used with weights weights (that is, minimizing sum(w*e^2)); otherwise ordinary least squares is used. See also ‘Details’,

na.action

a function which indicates what should happen when the data contain NAs. The default is set by the na.action setting of options, and is na.fail if that is unset. The ‘factory-fresh’ default is na.omit. Another possible value is NULL, no action. Value na.exclude can be useful.

method

the method to be used; for fitting, currently only method = "qr" is supported; method = "model.frame" returns the model frame (the same as with model = TRUE, see below).

model

logicals. If TRUE the corresponding components of the fit (the model frame, the model matrix, the response, the QR decomposition) are returned.

x

logical. If TRUE return the model matrix of the fit. Default is TRUE.

y

logicals. If TRUE the corresponding components of the fit (the model frame, the model matrix, the response, the QR decomposition) are returned.

qr

logicals. If TRUE the corresponding components of the fit (the model frame, the model matrix, the response, the QR decomposition) are returned.

singular.ok

logical. If FALSE (the default in S but not in R) a singular fit is an error.

contrasts

Not implemented for complex variables. See zmodel.matrix for details. Default is NULL

offset

this can be used to specify an a priori known component to be included in the linear predictor during fitting. This should be NULL or a numeric vector or matrix of extents matching those of the response. One or more offset terms can be included in the formula instead or as well, and if more than one are specified their sum is used. See model.offset.

...

additional arguments to be passed to the low level regression fitting functions (see below).

Details

Like stats::lm the models are specified symbolically using the standard R formula notation: response ~ terms Meaning response is a linear combination of the predictor variables in terms. For compatibility with complex numbers complexlm::lm uses zmodel.matrix to build the model matrix from the model formula. As such it is limited to terms consisting of predictor names connected by + and/or : operators. Anything more complicated will result in an error.

If response is a matrix, then then lm() fits a model to each column, and returns an object with class "mlm".

For complex input, this function calls zlm.wfit.

Value

Returns an object of class c("zlm", "lm"), or for multiple responses c("zlm", "mlm", "lm").

The functions summary and anova are used to obtain and print a summary and analysis of variance table of the results. The generic functions coefficients, effects, fitted.values and residuals extract various useful features of the value returned by lm. Of course these things can also be accessed by simply using the get element symbol $.

Objects of class "zlm" are lists with the following components.

coefficients

a named vector of coefficients.

residuals

the residuals, that is response minus fitted values.

fitted.values

The fitted values, which are response values obtained by feeding the predictors into the model.

rank

The numeric rank of the fitted linear model.

weights

Numeric. The user supplied weights for the linear fit. If none were given, a vector of 1s of length equal to that of the input data.

df.residual

The residual degrees of freedom.

call

The matched call.

terms

the terms object used.

contrasts

The contrasts used, as these are not supported this component will probably be NULL.

xlevels

(only where relevant) a record of the levels of the factors used in fitting.

offset

the offset used (missing if none were used).

y

if requested, the response used.

x

the model matrix used, unless requested to not return it.

model

if requested, the model frame used.

na.action

(where relevant) information returned by model.frame on the special handling of NAs.

assign

Used by extractor functions like summary and effects to understand variable names. Not included in null fits.

effects

Complex list. See effects for explanation. Not included in null fits.

qr

unless declined, the output of the qr object created in the process of fitting. Not included in null fits.

Note

In complexlm, the x parameter defaults to TRUE so that followup methods such as predict.lm have access to the model matrix.

See Also

lm.fit, lm.wfit, zlm.wfit, zmodel.matrix, complexdqlrs, stats::lm

Examples

set.seed(4242)
n <- 8
slop <- complex(real = 4.23, imaginary = 2.323)
interc <- complex(real = 1.4, imaginary = 1.804)
e <- complex(real=rnorm(n)/6, imaginary=rnorm(n)/6)
xx <- complex(real= rnorm(n), imaginary= rnorm(n))
tframe <- data.frame(x= xx, y= slop*xx + interc + e)
lm(y ~ x, data = tframe, weights = rep(1,n))

[Package complexlm version 1.1.2 Index]