###---------------------------------------------------------------------------- ### Determinant ###---------------------------------------------------------------------------- A = matrix(rnorm(100), 10, 10) det(A) B = matrix(c(1:4), 2, 2) det(B) determinant(B, logarithm = TRUE) det(solve(A)) 1/det(A) ###---------------------------------------------------------------------------- ### Eigenvalues ###---------------------------------------------------------------------------- A eigen(B) sum(eigen(B)\$values) == sum(diag(B)) prod(eigen(B)\$values) == det(B) all.equal(prod(eigen(B)\$values), det(B) ) abs(prod(eigen(B)\$values)- det(B))< 1e-8 ## Trace of hat matrix beta <- matrix(c(1, 2, 3, 4)) n = 1000 X = matrix(rnorm(n*4), n) epsilon <- rnorm(n) Y = X%*%beta + epsilon beta_hat <- solve(t(X)%*%X)%*%t(X)%*%Y H = X%*%solve(t(X)%*%X)%*%t(X) sum(eigen(H)\$values) sum(diag(H)) F <- matrix(c(1, 2, 1, 2), 2) det(F) solve(F) eigen(F) ###---------------------------------------------------------------------------- ### Upper triangular matrix ###---------------------------------------------------------------------------- n <- 25 nrow <- sqrt(n) whichcols <- ceiling((1:n)/nrow) whichrows <- (1:n)%%nrow whichrows[whichrows == 0] <- nrow T <- matrix(rnorm(25), 5, 5) T[whichrows