## Add one column
## generate sample data
set.seed(1234)
n <- 12
p <- 5
X <- matrix(rnorm(n * p, 1), n, p)
## get the initial QR factorization
output <- fastQR::qr(X, complete = TRUE)
Q <- output$Q
R <- output$R
R1 <- R[1:p,]
## create column to be added
u <- matrix(rnorm(n), n, 1)
X1 <- cbind(X, u)
## update the R decomposition
R2 <- fastQR::rupdate(X = X, R = R1, U = u,
fast = FALSE, type = "column")
## check
max(abs(crossprod(R2) - crossprod(X1)))
## Add m columns
## generate sample data
set.seed(1234)
n <- 10
p <- 5
X <- matrix(rnorm(n * p, 1), n, p)
## get the initial QR factorization
output <- fastQR::qr(X, complete = TRUE)
Q <- output$Q
R <- output$R
R1 <- R[1:p,]
## create the matrix of columns to be added
m <- 2
U <- matrix(rnorm(n*m), n, m)
X1 <- cbind(X, U)
# QR update
R2 <- fastQR::rupdate(X = X, R = R1, U = U,
fast = FALSE, type = "column")
## check
max(abs(crossprod(R2) - crossprod(X1)))
## Add one row
## generate sample data
set.seed(1234)
n <- 12
p <- 5
X <- matrix(rnorm(n * p, 1), n, p)
## get the initial QR factorization
output <- fastQR::qr(X, complete = TRUE)
Q <- output$Q
R <- output$R
R1 <- R[1:p,]
## create the row u to be added
u <- matrix(data = rnorm(p), p, 1)
k <- 5
if (k<=n) {
X1 <- rbind(rbind(X[1:(k-1), ], t(u)), X[k:n, ])
} else {
X1 <- rbind(rbind(X, t(u)))
}
## update the R decomposition
R2 <- fastQR::rupdate(R = R1, X = X,
U = u,
type = "row")
## check
max(abs(crossprod(R2) - crossprod(X1)))
## Add m rows
## generate sample data
set.seed(1234)
n <- 12
p <- 5
X <- matrix(rnorm(n * p, 1), n, p)
## get the initial QR factorization
output <- fastQR::qr(X, complete = TRUE)
Q <- output$Q
R <- output$R
R1 <- R[1:p,]
## create the matrix of rows to be added
m <- 2
U <- matrix(data = rnorm(p*m), p, m)
k <- 5
if (k<=n) {
X1 <- rbind(rbind(X[1:(k-1), ], t(U)), X[k:n, ])
} else {
X1 <- rbind(rbind(X, t(U)))
}
## update the R decomposition
R2 <- fastQR::rupdate(R = R1, X = X,
U = U,
fast = FALSE,
type = "row")
## check
max(abs(crossprod(R2) - crossprod(X1)))
Run the code above in your browser using DataLab