首页 > 解决方案 > 相关分类和连续数据的模拟

问题描述

我想模拟相关的分类和连续数据。如何在 R 中实现这一点?

#For example, how to simulate the data in a way that these two variable are correlated?
x <- sample( LETTERS[1:4], 1000, replace=TRUE, prob=c(0.1, 0.2, 0.65, 0.05) ) #Categorical variable
y <- runif(1000,1,5) #Continuous variable

任何想法将不胜感激!

标签: rsimulationcorrelationcategorical-datacontinuous

解决方案


这会给您带来您正在寻找的东西吗?您可以更改 sd 值来修改相关量。

k <- 1:4
n <- 1000
x <- sample( LETTERS[k], n, replace=TRUE, prob=c(0.1, 0.2, 0.65, 0.05) ) 
y <- as.vector(sapply(k,function(x) rnorm(round(n/length(k)),mean=x,sd=2)))

推荐阅读