Hi,
I want to initialize and fill a sparse matrix inside my likelihood function.
I only need to fill value on the super- and subdiagonal.
I felt like the most elegant way would be
A <- Matrix::Matrix(0, m, m) # m is integer
A[cbind(1:(m-1), 2:m)] <- some advector of suitable length
but this gives
Error in .local(x, i, j, ..., value) :
not-yet-implemented 'Matrix[<-' method
Alternatively I tried
i_up <- 1:(m - 1)
i_down <- 2:m
up_vals <- ...
down_vals <- ...
i <- c(i_up, i_down)
j <- c(i_up + 1, i_down - 1)
x <- c(up_vals, down_vals)
Q <- Matrix::sparseMatrix(i = i, j = j, x = x, dims = c(m, m))
but this gives
Error in getClass(Class, where = topenv(parent.frame())) :
“gTMatrix” is not a defined class
So my question is simply, at this point in time, is there a way to do this semi elegantly without a loop?
Cheers