简体   繁体   中英

Simulating data from linear equation in R

I have a linear regression of the form:

Y= B0 + B1*X1 + B2*X2 + Be*Xe

If I assume values for Beta (0.38,0.27,0.10) and also assume that Xi are normally distributed with N(0,1) .

How can I generate a dataset that will be a linear combination of these variables?

This should work:

beta =c(0.38,0.27,0.10)
beta0 <- 1
N <- 10           
x <- matrix(rnorm(n=N*3) ,ncol=3) ## generate (x1,x2,xe)
y <- beta0 + x %*% beta

If I'm understanding the question correctly, you can generate this with a simple expression:

Y <- .38 + .27 * rnorm(1000) + .10 * rnorm(1000)

which will give you a vector, Y , distributed based on the formula above.

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