lundi 3 mai 2021

Is there a way to generate in R correlated Binomial, Gaussian and Poisson variables?

I am wondering if there is a way to automatically generate random variables which are correlated (even a Binomial with a Gaussian, not only variables belonging to the same family). The variables need to belong to the Gaussian, Poisson or Binomial family. Here, there is a not very automatic way to create variables which are correlated within the same family.

x1<-rbinom(100, 1, 0.5)
index<-sort(sample(1:100, 10, replace=F))
x2<-x1
for(i in 1:length(index)){
  if(x2[index[i]]==0){
    x2[index[i]]<-1
  }else{
    x2[index[i]]<-0
  }
}
normal<-as.data.frame(mvrnorm(n = 100, mu = c(1,2), Sigma = matrix(c(1,0.6, 0.6,1), nrow = 2, byrow = T)))
x3<-normal$V1
x4<-normal$V2
x5.1<- rpois(100, 0.5)
x5.2<-rpois(100, 2)
x5.3<-rpois(100, 1)
x5<- x5.1+x5.2
x6<-x5.1+x5.3
x<-as.data.frame(cbind(x1,x2,x3,x4,x5,x6))
cor(x)

My goal is to create a dataset with mixed-type correlated variables.




Aucun commentaire:

Enregistrer un commentaire