[c4e594]: / Functions-GenerateData.R

Download this file

192 lines (141 with data), 9.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
Generate.Data <- function(sample_size,num_features,theta_pred,model){
####
#### To generate the treatment
####
treatment <- rbinom(sample_size,1,0.5)
####
#### To generate the labels I need to create for each model the logistic regression function
####
switch(model,
{ #### Model 1
sigma <- diag(num_features)
correl <-0
sigma[seq(1,num_features,by=2),seq(1,num_features,by=2)] <-correl # correlation between odds features
sigma[seq(2,num_features,by=2),seq(2,num_features,by=2)] <-correl # correlation between even features
diag(sigma) <- 1
covariates <- mvrnorm(sample_size, rep(0, num_features), sigma) # I need install.packages("MASS") and library(MASS)
prog_part <- apply(covariates[,1:5],1,sum); # prog_part <- prog_part - mean(prog_part);
pred_part <- apply(covariates[,4:8],1,sum);# pred_part <- pred_part - mean(pred_part);
logit_pY1 <- prog_part + (treatment+0.1) * theta_pred * pred_part ;
prog_features <- 1:5; pred_features <- 4:8;
irr_features <-setdiff(seq(1,num_features,by=1),c(prog_features,pred_features))
for (index_feature in 1:num_features){
covariates[,index_feature] = t(discretize( covariates[,index_feature], disc="equalwidth", nbins= sample(c(2,3,4,5),1)))
}
covariates <- as.data.frame.matrix(covariates)
},
{ #### Model 2
sigma <- diag(num_features)
correl <-0
sigma[seq(1,15,by=2),seq(1,15,by=2)] <-correl # correlation between odds features
sigma[seq(2,15,by=2),seq(2,15,by=2)] <-correl # correlation between even features
diag(sigma) <- 1
covariates <- mvrnorm(sample_size, rep(0, num_features), sigma) # I need install.packages("MASS") and library(MASS)
prog_part <- apply(covariates[,1:5],1,sum); # prog_part <- prog_part - mean(prog_part);
pred_part <- apply(covariates[,6:10],1,sum);# pred_part <- pred_part - mean(pred_part);
logit_pY1 <- prog_part + (treatment+0)*theta_pred * pred_part ;
prog_features <- 1:5; pred_features <- 6:10;
irr_features <-setdiff(seq(1,num_features,by=1),c(prog_features,pred_features))
for (index_feature in 1:num_features){
covariates[,index_feature] = t(discretize( covariates[,index_feature], disc="equalwidth", nbins= sample(c(2,3,4,5),1)))
}
covariates <- as.data.frame.matrix(covariates)
},
{ #### Model 3
sigma <- diag(num_features)
correl <-0.70
sigma[seq(1,num_features,by=2),seq(1,num_features,by=2)] <-correl # correlation between odds features
sigma[seq(2,num_features,by=2),seq(2,num_features,by=2)] <-correl # correlation between even features
diag(sigma) <- 1
covariates <- mvrnorm(sample_size, rep(0, num_features), sigma) # I need install.packages("MASS") and library(MASS)
prog_part <- apply(covariates[,1:5],1,sum); # prog_part <- prog_part - mean(prog_part);
pred_part <- apply(covariates[,6:10],1,sum);# pred_part <- pred_part - mean(pred_part);
logit_pY1 <- prog_part + (treatment+0)*theta_pred * pred_part ;
prog_features <- 1:5; pred_features <- 6:10;
irr_features <-setdiff(seq(1,num_features,by=1),c(prog_features,pred_features))
for (index_feature in 1:num_features){
covariates[,index_feature] = t(discretize( covariates[,index_feature], disc="equalwidth", nbins= sample(c(2,3,4,5),1)))
}
covariates <- as.data.frame.matrix(covariates)
},
{ #### Model 4
sigma <- diag(num_features)
correl <-0.70
sigma[seq(1,num_features,by=2),seq(1,num_features,by=2)] <-correl # correlation between odds features
sigma[seq(2,num_features,by=2),seq(2,num_features,by=2)] <-correl # correlation between even features
diag(sigma) <- 1
covariates <- mvrnorm(sample_size, rep(0, num_features), sigma) # I need install.packages("MASS") and library(MASS)
prog_part <- covariates[,1] + apply(covariates[,c(2,3)],1,prod) + apply(covariates[,c(4,5)],1,prod) ;# prog_part <- prog_part - mean(prog_part);
pred_part <- ( covariates[,6] + apply(covariates[,c(7,8)],1,prod) + apply(covariates[,c(9,10)],1,prod) );# pred_part <- pred_part - mean(pred_part);
logit_pY1 <- prog_part + treatment * theta_pred * pred_part;
prog_features <- 1:5; pred_features <- 6:10;
irr_features <-setdiff(seq(1,num_features,by=1),c(prog_features,pred_features))
for (index_feature in 1:num_features){
covariates[,index_feature] = t(discretize( covariates[,index_feature], disc="equalwidth", nbins= sample(c(2,3,4,5),1)))
}
covariates <- as.data.frame.matrix(covariates)
},
{ #### Model 5
sigma <- diag(num_features)
correl <-0.70
sigma[seq(1,num_features,by=2),seq(1,num_features,by=2)] <-correl # correlation between odds features
sigma[seq(2,num_features,by=2),seq(2,num_features,by=2)] <-correl # correlation between even features
diag(sigma) <- 1
covariates <- mvrnorm(sample_size, rep(0, num_features), sigma) # I need install.packages("MASS") and library(MASS)
prog_part <- covariates[,1]*(covariates[,2]*covariates[,3]-covariates[,4]^2) ;# prog_part <- prog_part - mean(prog_part);
pred_part <- covariates[,5]*exp(covariates[,6]*covariates[,7]-covariates[,8]^2) ;# pred_part <- pred_part - mean(pred_part);
logit_pY1 <- prog_part + treatment*theta_pred * pred_part;
prog_features <- 1:4; pred_features <- 5:8;
irr_features <-setdiff(seq(1,num_features,by=1),c(prog_features,pred_features))
for (index_feature in 1:num_features){
covariates[,index_feature] = t(discretize( covariates[,index_feature], disc="equalwidth", nbins= sample(c(2,3,4,5),1)))
}
covariates <- as.data.frame.matrix(covariates)
},
{ #### Model 6
sigma <- diag(num_features)
correl <-0.70
sigma[seq(1,num_features,by=2),seq(1,num_features,by=2)] <-correl # correlation between odds features
sigma[seq(2,num_features,by=2),seq(2,num_features,by=2)] <-correl # correlation between even features
diag(sigma) <- 1
covariates <- mvrnorm(sample_size, rep(0, num_features), sigma) # I need install.packages("MASS") and library(MASS)
prog_part <- covariates[,1] + (covariates[,2]>-0.545) * (covariates[,3]<0.545) ;# prog_part <- prog_part - mean(prog_part);
pred_part <- covariates[,4] + (covariates[,5]>-0.545) * (covariates[,6]<0.545) ;# pred_part <- pred_part - mean(pred_part);
logit_pY1 <- prog_part + treatment*theta_pred * pred_part;
prog_features <- 1:3; pred_features <- 4:6;
irr_features <-setdiff(seq(1,num_features,by=1),c(prog_features,pred_features))
for (index_feature in 1:num_features){
covariates[,index_feature] = t(discretize( covariates[,index_feature], disc="equalwidth", nbins= sample(c(2,3,4,5),1)))
}
covariates <- as.data.frame.matrix(covariates)
},
{ #### Model 7
sigma <- diag(num_features)
correl <-0.70
sigma[seq(1,num_features,by=2),seq(1,num_features,by=2)] <-correl # correlation between odds features
sigma[seq(2,num_features,by=2),seq(2,num_features,by=2)] <-correl # correlation between even features
diag(sigma) <- 1
covariates <- mvrnorm(sample_size, rep(0, num_features), sigma) # I need install.packages("MASS") and library(MASS)
prog_part <- covariates[,1] + (covariates[,2]>-0.545) * (covariates[,3]<0.545)*(covariates[,4]>0);# prog_part <- prog_part - mean(prog_part);
pred_part <- covariates[,5] + (covariates[,6]>-0.545) * (covariates[,7]<0.545)*(covariates[,8]>0);# pred_part <- pred_part - mean(pred_part);
logit_pY1 <- prog_part + treatment*theta_pred * pred_part;
prog_features <- 1:4; pred_features <- 5:8;
irr_features <-setdiff(seq(1,num_features,by=1),c(prog_features,pred_features))
for (index_feature in 1:num_features){
covariates[,index_feature] = t(discretize( covariates[,index_feature], disc="equalwidth", nbins= sample(c(2,3,4,5),1)))
}
covariates <- as.data.frame.matrix(covariates)
},
stop("Wrong model")
)
pY1 <- 1/(1+exp(-logit_pY1))
labels <- rbinom(sample_size,1,pY1);
synthetic_datasets <- numeric(0)
synthetic_datasets$data <- covariates
synthetic_datasets$treatment <- treatment
synthetic_datasets$labels <- labels
synthetic_datasets$prog_features <- prog_features
synthetic_datasets$pred_features <- pred_features
synthetic_datasets$irr_features <- irr_features
return(synthetic_datasets)
}