简体   繁体   中英

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~iidN(0,sigma^2).

The easiest is the identity matrix, which has always full rank. So eg 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, (eg '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'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM