How to create invertible random matrices in R

Asked

Viewed 941 times

4

One of the ways to find the estimator using the method of least ordinary squares is by generating random matrices, given by the formula: B = (X’ X)^-1 X’Y

Where Y = a+bx+cw+u, being x and w random vectors of size 1000 and distribution N(1,2).

The matrix X has 3 columns, the first 1 being the second x and the third w, and 1000 lines. After the generation of this random matrix it is necessary to perform one for this procedure to be repeated 1000 times. However, as I did not guarantee that the product (X' X) generated an invertible matrix, the for gave me 1000 equal results, thus changing the x, the w and the y.

I am trying to realize this condition by the if but am not succeeding. I wonder if there are any commands that assure me that the matrix has inverse.

  • Livia, Voce could put what you did? It would be very difficult for the matrix not to have inverse using random numbers.

1 answer

2


I’m not sure I understand what the problem was, but it’s almost impossible (X'X) do not have inverse in this case, because you will generate 1000 random numbers and only has 2 variables. So I think the problem must have occurred at the time of generating the random numbers, maybe posting the code you used will be easier to help.

Anyway, follows an example that will generate the matrix X with (X'X) having an inverse (the commands set.seed are not necessary, are only to ensure that the example is reproducible):

### gera o vetor da constante
c <- rep(1, 1000)

### gera 1000 observações de x ~ N(1,2)
set.seed(1)
x <-rnorm(1000, 1,2) 

### gera 1000 observações de w ~ N(1,2)
set.seed(2)
w <- rnorm(1000, 1, 2)

### construindo a matrix X
X = cbind(c,x,w)

See that (X'X) has inverse:

solve(t(X)%*%X)
              c             x             w
c  0.0015068062 -2.163410e-04 -2.629055e-04
x -0.0002163410  2.341451e-04 -1.098703e-05
w -0.0002629055 -1.098703e-05  2.434495e-04

Calculating the Betas for an example (y = 10 + 5x + 2w + u):

### y = 10 + 5x + 2w + u, u~N(0,1)
set.seed(3)
y <- 10 + 5*x +2*w + rnorm(1000)

### estimando os coeficientes por OLS
B= solve(t(X)%*%X)%*%t(X)%*%y
B
      [,1]
c 9.994012
x 5.019442
w 1.994124
  • 1

    Thank you for your attention and explanation. As you said, the problem was not in the creation of inverse matrices, but in the syntax of the for() command. Your example was very enlightening to me, thank you again.

  • For nothing! I’m glad you helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.