Lisa Han
Lisa Han

Reputation: 33

How to generate matrix with certain rank in R

Does anyone know how to generate matrix with certain rank in R?

I ultimately want to create data matrix Y = X + E

where rank(X)=k and E~i.i.d.N(0,sigma^2).

Upvotes: 3

Views: 630

Answers (1)

user1966337
user1966337

Reputation: 115

The easiest is the identity matrix, which has always full rank. So e.g. use:

k <- 10
mymatrix <- diag(k)

Here, rows and columns are equal to the rank you specify

I suppose you want to mimic a regression model, so you might want to have more rows (meaning 'observations') than columns, (e.g. 'variables'). The following code allows you to specify both:

k <- 5 # rank of your matrix
nobs <- 10 # number of lines within X
X <- rbind(diag(k), matrix(rep(0,k*(nobs-k)), ncol=k))
y <- X + rnorm(nobs)

Note, that X - and therefore also y - now have full column rank. there is no multicollinearity in this 'model'.

Upvotes: 1

Related Questions