Construct diagonal matrix using bigalgebra - r

I'd like to construct a bigger diagonal matrix from a vector. I installed the bigalgebra package, but it don't have the diag function. In addition, how to make the inverse (solve) and transpose (t) to big matrices.
v <- runif(42109)
V <- diag(v)
Error: cannot allocate vector of size 13.2 Gb

If sparse matrices are an option, you can use the Matrix package (supplied with R).
library(Matrix)
V <- Matrix(0, nrow=42109, ncol=42109)
diag(V) <- v

Related

R pnorm function with sparse matrix

I would like to find pvalues of a large sparse matrix. All the elements in this matrix are standard normal z-scores. I want to use pnorm function, but I met a problem that pnorm does not support sparse matrix. Except for transforming sparse matrix to full matrix, is there any other more efficient way?
Any suggestions are appreciated!
If it is a sparse matrix, you can easily replace the 0 values with pnorm(0..). What remains is to calculate the non-zero values, which you can do. For example a sparse matrix:
data <- rnorm(1e5)
zero_index <- sample(1e5)[1:9e4]
data[zero_index] <- 0
mat <- matrix(data, ncol=100)
mat_sparse <- Matrix(mat, sparse=TRUE)
Create a matrix with pnorm for 0:
mat_pnorm <- matrix(pnorm(rep(0,length(mat_sparse))),ncol=ncol(mat_sparse))
nzData <- summary(mat_sparse)
mat_pnorm[as.matrix(nzData[,1:2])] <- pnorm(nzData$x)
all.equal(mat_pnorm,pnorm(mat))
[1] TRUE
You did not specify how you would like the p-values, but you can easily have it cast into a vector instead of a matrix which was used above.

Compute the null space of a sparse matrix

I found the function (null OR nullspace) to find the null space of a regular matrix in R, but I couldn't find any function or package for a sparse matrix (sparseMatrix).
Does anybody know how to do this?
If you take a look at the code of ggm::null, you will see that it is based on the QR decomposition of the input matrix.
On the other hand, the Matrix package provides its own method to compute the QR decomposition of a sparse matrix.
For example:
require(Matrix)
A <- matrix(rep(0:1, 3), 3, 2)
As <- Matrix(A, sparse = TRUE)
qr.Q(qr(A), complete=TRUE)[, 2:3]
qr.Q(qr(As), complete=TRUE)[, 2:3]

Calculate a n-byn matrix using values in 2 vectors (lengths of n) in R

I'm trying to calculate a n-by-n matrix in R using the values from 2 n vectors.
For example, let's say I have the following vectors.
formula f(x,y)=x+y
x<-c(1,2,3)
y<-c(8,9,10)
z should be a 3-by-3 matrix where z[0][0] is f(0,0) z[0][1] is f(0,1). IS there any way to perform such a calculation in R?
You can try outer
outer(x, y, FUN= f)
where
f <- function(x,y) x+y

Scalar Multiplication in R

I'm trying to perform simple scalar multiplication in R, but I'm running into a bit of an issue.
In linear algebra I would do the following:
Here's how I've implemented this in R:
A <- matrix(1:4, 2, byrow = TRUE)
c <- matrix(rep(3, 4), 2)
A * c
This produces the correct output, but creating the scalar matrix c will be cumbersome when it comes to larger matrices.
Is there a better way to do this?
In R the default is scalar. For matrix multiplication use %*%. t is transpose and solve will give you the inverse. Here are some examples:
a = matrix(1:4,2,2)
3 * a
c(1:2) %*% a
c(1:2) %*% t(a)
solve(a)
Here is a link: matrix algebra in R
Use the function drop() to convert a 1x1 variable matrix into a "real" scalar. So you can write drop(c)*A and you don't need to replace c with the value itself.

Alternative way to loop for faster computing

Let's say I have a n*p dataframe.
I have computed a list of n matrix of p*p dimensions (named listMat in the R script belowed), in which each matrix is the distance matrix between the p variables for each of the n respondants.
I want to compute a n*n matrix called normMat, with each elements corresponding to the norm of the difference between each pairwise distance matrix. For exemple : normMat[1,2] will be the norm of a matrix named "diffMat" where diffMat is the difference between the 1st distance matrix and the 2nd distance matrix of the list of Matrix "listMat".
I wrote the following script which works fine, but i'm wondering if there is a more efficient way to write it, to avoid the loops (using for exemple lapply, etc ..) and make the script execution go faster.
# exemple of n = 3 distances matrix between p = 5 variables
x <- abs(matrix(rnorm(1:25),5,5))
y <- abs(matrix(rnorm(1:25),5,5))
z <- abs(matrix(rnorm(1:25),5,5))
listMat <- list(x, y, z)
normMat <- matrix(NA,n,n)
for (numRow in 1:n){
for (numCol in 1:n){
diffMat <- listMat[[numRow]] - listMat[[numCol]]
normMat[numRow, numCol] <- norm(diffMat, type="F")
}
}
Thanks for your help.
Try:
normMat <- function(x, y) {
norm(x-y, type="F")
}
sapply(listMat, function(x) sapply(listMat, function(y) normMat(x,y)))

Resources