基于R中其他变量的模拟变量

Simulating variables based on other variables in R

我希望模拟一组与模拟数值变量相关的分类变量。更具体地说,我有变量 age,其定义如下:age <- rnorm(n=1000, mean=35, sd =9) 我希望模拟另一个变量 class,其中年龄越大 class 越高。谁能指出我正确的方向?提前致谢!

我的理解是,如果ab相关,则意味着ab是线性相关的。所以,a可以用b的线性函数表示。要生成随机变量,应添加随机噪声。

这是一种方法:

set.seed(1)
age <- rnorm(n=10, mean=35, sd =9)
beta <- runif(1, min = 1, max = 5) # or any other finite min and max values, can be positive or negative, but in your case should be positive.
class <- beta*age + rnorm(length(age), mean = 0, sd = 2) # or any other mean and sd values

# Check correlation between age and class
cor(age, class)
#[1] 0.9994416

# Check if higher age makes for higher class
data.frame(sort(age), sort(class))

   sort.age. sort.class.
1   27.47934    129.6408
2   27.61578    131.3707
3   29.36192    137.5428
4   32.25150    152.3856
5   36.65279    171.3957
6   37.96557    179.0890
7   39.38686    184.8634
8   40.18203    187.9404
9   41.64492    198.2192
10  49.35753    233.2981