R stuff
Abajo he colocado algunas cosas simples de R que me han ayudado a resolver problemas más elaborados, junto a algunos links que me han aparecido interesantes. Por otro lado, tienes un Snippet Box de R por si quieres probar algunos códigos!
Snippet Box de R:
Efficient computations in R:
Matrix crossproducts: Use crossprod(A, B) instead of t(A) %*% B (or tcrossprod(A, B) if you want to compute A %*% t(B)).
Trace of the product of two matrices A and B: Use sum(A*t(B)) instead of sum(diag(A %*% B)). Another option is crossprod(c(A), c(t(B))).
Inverse of a symmetric matrix A: Use chol2inv(chol(A)) instead of solve(A).
Logarithm of the determinant of a symmetric matrix A: Use sum(log(eigen(A, symmetric = TRUE, only.values = TRUE)$values)) instead of log(diag(A)).
Reconstruction of a two-by-two partitioned matrix A:
bblock2 <- function(A11, A12, A21, A22){
block <- rbind(cbind(A11, A12), cbind(A21, A22))
unname(block, force = FALSE)
}
Inverse of a two-by-two partitioned matrix A:
inv_bblock2 <- function(A11, A12, A21, A22){
# Assumptions:
# A11 and A22 are symmetric matrices
# A21 = t(A12)
M1 <- chol2inv(chol(A11))
M2 <- crossprod(A12, M1)
M3 <- tcrossprod(M2, A21)
S22 <- chol2inv(chol(A22 - M3))
S21 <- - S22 %*% M2
S12 <- t(S21)
S11 <- M1 - crossprod(M2, S21)
S <- bblock2(S11, S21, S21, S22)
output <- list(S11 = S11, S12 = S12, S21 = S21, S22 = S22, S = S)
return(output)
}
Building R packages: