library(spam) ## Goal: Generate the complete precision matrix for the ## proposal density when updating x (Exercise 1 c) BL) load('tma4300_ex2_Rmatrix.Rdata') ## Get the number of rows (which equals the number of columns) n <- dim(R)[1] #544 ## So far the dimension of R is 544 x 544 ## The precision matrix for the proposal (and also for the full ## conditional is, however, of dimension (2*544) x (2*544), ## where the matrix R appears in the upper left block. ## Enlarge the dimension, now R is (automatically) only in the ## upper left block while the other blocks contain zeros Q1 <- R dim(Q1) <- c(2*n, 2*n) ## Alternative using a Kronecker product: ## Generate a zero matrix of dimension 2 times 2 d <- spam(0,2,2) ## Set d[1,1] to 1 d[1,1] <- 1 # or d <- as.spam(diag.spam(c(1,0))) Q1_alternative <- kronecker(d, R) ## Check via par(mfrow=c(1,3)) display(R, cex=2) title("Adjacency matrix") display(Q1, cex=2) title("Q1") display(Q1_alternative, cex=2) title("Q1 alternative") ## Combine different diag matrices Q2 <- rbind(cbind(diag.spam(n), -diag.spam(n)), cbind(-diag.spam(n), diag.spam(n))) ## generate a matrix which will later contain the entries of diag(c) ## by now the entries are 1. diagC <- as.spam(diag.spam(c(rep(0,n), rep(1,n)))) ## The entries can be changed (updated) by assigning ## diagC@entries a new vector of length 544. This will be ## necessary at each step of your sampler as the values of ## c will change. ## For example, #diagC@entries <- 1:544 ## Then construct the complete matrix kappa_u <- 0.5 # just an example value kappa_v <- 3 # just an example value Q <- kappa_u * Q1 + kappa_v * Q2 + diagC display(Q, cex=2) title("Block matrix")