Title: | Bivariate Linear Errors-In-Variables Estimation |
---|---|
Description: | Estimate the slope and intercept of a bivariate linear relationship by calculating a posterior density that is invariant to interchange and scaling of the coordinates. |
Authors: | David Leonard [aut, cre] |
Maintainer: | David Leonard <[email protected]> |
License: | GPL (>= 2) |
Version: | 2.0-7 |
Built: | 2025-03-01 04:51:03 UTC |
Source: | https://github.com/cran/leiv |
Generates a linear errors-in-variables object.
leiv(formula, data, subset, prior = NULL, n = NULL, cor = NULL, sdRatio = NULL, xMean = 0, yMean = 0, probIntCalc = FALSE, level = 0.95, subdivisions = 100, rel.tol = .Machine$double.eps^0.25, abs.tol = 0.1*rel.tol, ...) ## S4 method for signature 'leiv' print(x, digits = max(3, getOption("digits") - 3), ...) ## S4 method for signature 'leiv,missing' plot(x, plotType = "density", xlim = NULL, ylim = NULL, xlab = NULL, ylab = NULL, col = NULL, lwd = NULL, ...)
leiv(formula, data, subset, prior = NULL, n = NULL, cor = NULL, sdRatio = NULL, xMean = 0, yMean = 0, probIntCalc = FALSE, level = 0.95, subdivisions = 100, rel.tol = .Machine$double.eps^0.25, abs.tol = 0.1*rel.tol, ...) ## S4 method for signature 'leiv' print(x, digits = max(3, getOption("digits") - 3), ...) ## S4 method for signature 'leiv,missing' plot(x, plotType = "density", xlim = NULL, ylim = NULL, xlab = NULL, ylab = NULL, col = NULL, lwd = NULL, ...)
formula |
an optional object of class |
data |
an optional data frame (or object coercible by |
subset |
an optional vector specifying a subset of observations to be used in the fitting process. |
prior |
an optional object of class |
n |
an optional sample size (if |
cor , sdRatio
|
optional sample correlation |
xMean , yMean
|
optional sample means |
probIntCalc |
logical; if |
level |
the probability level requested (if |
subdivisions |
the maximum number of subintervals (see |
rel.tol |
the relative accuracy requested (see |
abs.tol |
the absolute accuracy requested (see |
x |
a |
digits |
controls formating of numeric objects. |
plotType |
specifies the type of plot; if |
xlim , ylim
|
x limits |
xlab , ylab
|
labels for the x and y axes of the plot. |
col , lwd
|
color and width of plotted lines. |
... |
additional argument(s) for generic methods. |
Use leiv
to estimate the slope and intercept of a bivariate linear relationship when both variables are observed with error. The method is exact when the true values and the errors are normally distributed. The posterior density depends on the data only through the correlation coefficient and ratio of standard deviations; it is invariant to interchange and scaling of the coordinates.
leiv
returns an object of class "leiv"
with the following components:
slope |
the (posterior median) slope estimate. |
intercept |
the (maximum likelihood) intercept estimate. |
slopeInt |
the shortest (100* |
interceptInt |
the shortest (100* |
density |
the posterior probability density function. |
n |
the number of (x,y) pairs. |
cor |
the sample correlation |
sdRatio |
the ratio |
xMean |
the sample mean |
yMean |
the sample mean |
call |
the matched call. |
probIntCalc |
the logical probability interval request. |
level |
the probability level of the probability interval. |
x |
the x data. |
y |
the y data. |
Numerical integration is used to normalize the posterior density. When the data is nearly linear, normalization using the default tolerance parameters may fail. Specifying abs.tol = 1e-6
(or smaller) may help, but expect a longer run time. In general, rel.tol
cannot be less than max(50*.Machine$double.eps, 0.5e-28)
if abs.tol <= 0
. In addition, when using a sharply peaked leiv
object as a prior density, normalization may fail. In this case, an alternative is to first fit using the default Cauchy prior, then multiply by the appropriate ratio of prior densities and tackle the normalization outside of the leiv
environment.
David Leonard
Leonard, David. (2011). “Estimating a Bivariate Linear Relationship.” Bayesian Analysis, 6:727-754. DOI:10.1214/11-BA627.
Zellner, Arnold. (1971). An Introduction to Bayesian Inference in Econometrics, Chapter 5. John Wiley & Sons.
lm
for formula syntax; integrate
for control parameters.
## generate artificial data set.seed(1123) n <- 20 X <- rnorm(n, mean=5, sd=4) # true x x <- X + rnorm(n, mean=0, sd=5) # observed x Y <- 2 + X # true y y <- Y + rnorm(n, mean=0, sd=3) # observed y ## fit with default options fit <- leiv(y ~ x) print(fit) plot(fit) # density plot dev.new() plot(fit,plotType="scatter") ## calculate a density to use as an informative prior density of ## the scale invariant slope in a subsequent fit fit0 <- leiv(n=10, cor=0.5, sdRatio=1.0) print(fit0) ## refit the data using the informative prior density fit1 <- leiv(y ~ x, prior=fit0, abs.tol=1e-6) print(fit1)
## generate artificial data set.seed(1123) n <- 20 X <- rnorm(n, mean=5, sd=4) # true x x <- X + rnorm(n, mean=0, sd=5) # observed x Y <- 2 + X # true y y <- Y + rnorm(n, mean=0, sd=3) # observed y ## fit with default options fit <- leiv(y ~ x) print(fit) plot(fit) # density plot dev.new() plot(fit,plotType="scatter") ## calculate a density to use as an informative prior density of ## the scale invariant slope in a subsequent fit fit0 <- leiv(n=10, cor=0.5, sdRatio=1.0) print(fit0) ## refit the data using the informative prior density fit1 <- leiv(y ~ x, prior=fit0, abs.tol=1e-6) print(fit1)
p50
calculates the median of the leiv
posterior probability density. probInt
calculates the shortest probability interval of the leiv
posterior probability density for a given probability level.
p50(p, interval, subdivisions = 100, rel.tol = .Machine$double.eps^0.25, abs.tol = rel.tol) probInt(p, interval, level, subdivisions = 100, rel.tol = .Machine$double.eps^0.25, abs.tol = rel.tol)
p50(p, interval, subdivisions = 100, rel.tol = .Machine$double.eps^0.25, abs.tol = rel.tol) probInt(p, interval, level, subdivisions = 100, rel.tol = .Machine$double.eps^0.25, abs.tol = rel.tol)
p |
a normalized probability density function. |
interval |
a vector containing the endpoints of the interval to be searched. |
level |
the probability level requested. |
subdivisions |
the maximum number of subintervals (see |
rel.tol |
the relative accuracy requested (see |
abs.tol |
the absolute accuracy requested (see |
Internal functions for integrating the posterior density returned by the function leiv
. These functions are not meant to be called by the user.
p50
returns a numeric scalar. probInt
returns a 2-dimensional numeric vector of interval endpoints.
p
must accept a vector of inputs and produce a vector of function evaluations at those points.
rel.tol
cannot be less than max(50*.Machine$double.eps, 0.5e-28)
if abs.tol <= 0
.
leiv
for general information; integrate
for control parameters.