Solved – Linear regression simulation

linear modelrregressionsimulation

I would really appreciate if someone could help me with this question.

I want to simulate $n$ datasets in R with eight predictors where $β=(3,1.5, 0, 0, 2, 0, 0, 0)$ and the pairwise correlation between $x_i$ and $x_j$ is set to be $\text{corr}(i,~j) = 0.5$ for $i-j$ or for all $i=j$.

Best Answer

Here is a suggestion:

## Parameters
n     <- 20                         # number of samples
rho   <- 0.5                        # correlation between predictors
beta  <- c(0.8,3,1.5,0,0,2,0,0,0)   # regression coefficients
sigma <- 0.4                        # std error of noise (assumed Gaussian)
## Covariance matrix
d     <- length(beta)
Q     <- toeplitz(c(1, rep(rho, d - 2)))
## Design matrix
X <- cbind(1, matrix(rnorm((d-1) * n), ncol = d-1) %*% chol(Q))
## Dependent variable
Y <- X %*% beta + rnorm(n, 0, sigma)
Related Question