简体   繁体   English

从R中的线性方程式模拟数据

[英]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) . 如果我假设Beta的值(0.38,0.27,0.10)并还假设XiN(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. 这将为您提供基于上述公式分布的向量Y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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