How to create random invertible arrays in R

4

One of the ways to find the estimator using the ordinary least squares method 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 X array has 3 columns, the first 1 being the second x and the third w , and 1000 rows. After generating this random matrix, a forcing must be performed for this procedure to be repeated 1000 times. However, since I did not guarantee that the (X' X) product generated an invertible array, the for gave me 1000 equal results, thus not varying the x , w and y .

I'm trying to accomplish this condition by if but I'm not getting success. I would like to know if there is any command that guarantees that the array has an inverse.

    
asked by anonymous 23.03.2014 / 15:46

1 answer

2

I do not know if I understood correctly what the problem was, but it is almost impossible (X'X) not to have a reverse in this case, because you will generate 1000 random numbers and only have 2 variables. So I guess the problem must have occurred when it came to generating the random numbers, maybe by posting the code you used it would be easier to help.

In any case, there is an example that will generate the array X with (X'X) having an inverse ( set.seed commands are not required, they 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 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
    
23.03.2014 / 17:58