Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

wcrossprod

Weighted Matrix Crossproduct


Description

Given matrices x and y as arguments and an optional matrix or vector of weights, w, return a weighted matrix cross-product, t(x) w y. If no weights are supplied, or the weights are constant, the function uses crossprod for speed.

Usage

wcrossprod(x, y, w)

Arguments

x,y

x, y numeric matrices; missing(y) is taken to be the same matrix as x. Vectors are promoted to single-column or single-row matrices, depending on the context.

w

A numeric vector or matrix of weights, conformable with x and y.

Value

A numeric matrix, with appropriate dimnames taken from x and y.

Author(s)

Michael Friendly, John Fox jfox@mcmaster.ca

See Also

Examples

set.seed(12345)
n <- 24
drop <- 4
sex <- sample(c("M", "F"), n, replace=TRUE)
x1 <- 1:n
x2 <- sample(1:n)
extra <- c( rep(0, n - drop), floor(15 + 10 * rnorm(drop)) )
y1 <- x1 + 3*x2 + 6*(sex=="M") + floor(10 * rnorm(n)) + extra
y2 <- x1 - 2*x2 - 8*(sex=="M") + floor(10 * rnorm(n)) + extra
# assign non-zero weights to 'dropped' obs
wt <- c(rep(1, n-drop), rep(.2,drop))

X <- cbind(x1, x2)
Y <- cbind(y1, y2)
wcrossprod(X)
wcrossprod(X, w=wt)

wcrossprod(X, Y)
wcrossprod(X, Y, w=wt)

wcrossprod(x1, y1)
wcrossprod(x1, y1, w=wt)

car

Companion to Applied Regression

v3.0-10
GPL (>= 2)
Authors
John Fox [aut, cre], Sanford Weisberg [aut], Brad Price [aut], Daniel Adler [ctb], Douglas Bates [ctb], Gabriel Baud-Bovy [ctb], Ben Bolker [ctb], Steve Ellison [ctb], David Firth [ctb], Michael Friendly [ctb], Gregor Gorjanc [ctb], Spencer Graves [ctb], Richard Heiberger [ctb], Pavel Krivitsky [ctb], Rafael Laboissiere [ctb], Martin Maechler [ctb], Georges Monette [ctb], Duncan Murdoch [ctb], Henric Nilsson [ctb], Derek Ogle [ctb], Brian Ripley [ctb], William Venables [ctb], Steve Walker [ctb], David Winsemius [ctb], Achim Zeileis [ctb], R-Core [ctb]
Initial release
2020-09-23

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.