I think the issue you're having is with the matrix multiplication, (which is done by %*%). If the matrices are not in format m x n and n x p it wont work. Since you did not create a reprex, I can't run your code to be sure, but this might be the issue. Also, make sure that the order of the multiplication is correct, because A x B is not the same as B x A for matrix multiplication (different rows and columns)

For next time, consider building a reprex to get faster answers. A reprex consists of the minimal code and data needed to recreate the issue/question you're having. You can find instructions how to build and share one here: