qr-decomposition

qr function in R and matlab

好久不见. 提交于 2020-05-15 04:44:39
问题 I have a question about converting a matlab function into R, and I was hoping that someone could help. The standard QR decomposition used in both matlab and R is referred to as qr(). To my understanding, the standard way of performing a qr decomposition in both languages is: Matlab: [Q,R] = qr(A) satisfying QR=A R: z <- qr(A) Q <- qr.Q(z) R <- qr.R(z) Both of which provide me with the same results, unfortunately, this is not what I need. What I need is this: Matlab: [Q,R,e] = qr(A,0) which

Writing a Householder QR factorization function in R code

╄→尐↘猪︶ㄣ 提交于 2020-01-01 18:20:13
问题 I am working on a piece of code to find the QR factorization of a matrix in R. X <- structure(c(0.8147, 0.9058, 0.127, 0.9134, 0.6324, 0.0975, 0.2785, 0.5469, 0.9575, 0.9649, 0.1576, 0.9706, 0.9572, 0.4854, 0.8003 ), .Dim = c(5L, 3L)) myqr <- function(A) { n <- nrow(A) p <- ncol(A) Q <- diag(n) Inp <- diag(nrow = n, ncol = p) for(k in c(1:ncol(A))) { # extract the kth column of the matrix col<-A[k:n,k] # calculation of the norm of the column in order to create the vector norm1<-sqrt(sum(col^2

How to get the Q from the QR factorization output?

偶尔善良 提交于 2019-12-21 02:38:09
问题 DGEQRF and SGEQRF from LAPACK return the Q part of the QR factorization in a packed format. Unpacking it seems to require O(k^3) steps (k low-rank products), and doesn't seem to be very straightforward. Plus, the numerical stability of doing k sequential multiplications is unclear to me. Does LAPACK include a subroutine for unpacking Q, and if not, how should I do it? 回答1: Yes, LAPACK indeed offers a routine to retrieve Q from the elementary reflectors (i.e. the part of data returned by

QR decomposition different in lm and biglm?

我与影子孤独终老i 提交于 2019-12-19 09:55:39
问题 I'm trying to recover the R matrix from the QR decomposition used in biglm. For this I am using a portion of the code in vcov.biglm and put it into a function like so: qr.R.biglm <- function (object, ...) { # Return the qr.R matrix from a biglm object object$qr <- .Call("singcheckQR", object$qr) p <- length(object$qr$D) R <- diag(p) R[row(R) > col(R)] <- object$qr$rbar R <- t(R) R <- sqrt(object$qr$D) * R dimnames(R) <- list(object$names, object$names) return(R) } More specifically, I'm

Get hat matrix from QR decomposition for weighted least square regression

一笑奈何 提交于 2019-12-18 17:15:33
问题 I am trying to extend the lwr() function of the package McSptial , which fits weigthed regressions as non-parametric estimation. In the core of the lwr() function, it inverts a matrix using solve() instead of a QR decomposition, resulting in numerical instability. I would like to change it but can't figure out how to get the hat matrix (or other derivatives) from the QR decomposition afterward. With data : set.seed(0); xmat <- matrix(rnorm(500), nrow=50) ## model matrix y <- rowSums(rep(2:11

Get hat matrix from QR decomposition for weighted least square regression

纵然是瞬间 提交于 2019-12-18 17:15:22
问题 I am trying to extend the lwr() function of the package McSptial , which fits weigthed regressions as non-parametric estimation. In the core of the lwr() function, it inverts a matrix using solve() instead of a QR decomposition, resulting in numerical instability. I would like to change it but can't figure out how to get the hat matrix (or other derivatives) from the QR decomposition afterward. With data : set.seed(0); xmat <- matrix(rnorm(500), nrow=50) ## model matrix y <- rowSums(rep(2:11

Multiple regression analysis in R using QR decomposition

我们两清 提交于 2019-12-18 12:08:48
问题 I am trying to write a function for solving multiple regression using QR decomposition. Input: y vector and X matrix; output: b, e, R^2. So far I`ve got this and am terribly stuck; I think I have made everything way too complicated: QR.regression <- function(y, X) { X <- as.matrix(X) y <- as.vector(y) p <- as.integer(ncol(X)) if (is.na(p)) stop("ncol(X) is invalid") n <- as.integer(nrow(X)) if (is.na(n)) stop("nrow(X) is invalid") nr <- length(y) nc <- NCOL(X) # Householder for (j in seq_len

QR decomposition and Choleski decomposition in R

左心房为你撑大大i 提交于 2019-12-14 04:19:17
问题 I recently read about how the R matrix of QR decomposition can be calculated using the Choleski decomposition. The relation is: R = Choleski-decomposition(A^TA) Example: > A=matrix(c(1,2,3,2,3,5,1,3,2), nrow=3) > A [,1] [,2] [,3] [1,] 1 2 1 [2,] 2 3 3 [3,] 3 5 2 > AtA = t(A)%*%A > AtA [,1] [,2] [,3] [1,] 14 23 13 [2,] 23 38 21 [3,] 13 21 14 Now calculating QR and Choleski decomposition: > chol(AtA) [,1] [,2] [,3] [1,] 3.741657 6.147009 3.4743961 [2,] 0.000000 0.462910 -0.7715167 [3,] 0.000000

Writing a Householder QR factorization function in R code

醉酒当歌 提交于 2019-12-04 18:37:02
I am working on a piece of code to find the QR factorization of a matrix in R. X <- structure(c(0.8147, 0.9058, 0.127, 0.9134, 0.6324, 0.0975, 0.2785, 0.5469, 0.9575, 0.9649, 0.1576, 0.9706, 0.9572, 0.4854, 0.8003 ), .Dim = c(5L, 3L)) myqr <- function(A) { n <- nrow(A) p <- ncol(A) Q <- diag(n) Inp <- diag(nrow = n, ncol = p) for(k in c(1:ncol(A))) { # extract the kth column of the matrix col<-A[k:n,k] # calculation of the norm of the column in order to create the vector norm1<-sqrt(sum(col^2)) # Define the sign positive if a1 > 0 (-) else a1 < 0(+) sign <- ifelse(col[1] >= 0, -1, +1) #

Thin QR decomposition in c++

倖福魔咒の 提交于 2019-12-04 14:43:20
Is there an easy to use c++ library for "thin" QR decomposition of a rectangular matrix? Eigen seems to only support full Q matrices. I can take a full Q and discard some columns, but would it be more efficient to not compute them to begin with? Newmat does exactly what you want. To decompose A into QR, you can do: Matrix Q = A; UpperTriangularMatrix R; QRZ(Q, R) If A is a 3x5 matrix, R will be 3x3 and Q will be 3x5 as well. Even though this question is a bit old, for the record: Eigen does not explicitly compute the Q matrix, but a sequence of Householder vectors, which can directly be