The code described below cannot be reproduced because it calls up a series of functions I have created, I would need to obtain an array of matrices (1 for each scenario referred to by the function's outermost loop,the scenario is a table with the combinations of 5 variables) like the following array:
matrix_classification
var1 var2 var3 var4 var5
[1,] "OC" "OC" "IC" "IC" "IC"
[2,] "OC" "OC" "IC" "IC" "IC"
[3,] "OC" "IC" "IC" "IC" "IC"
[4,] "OC" "IC" "IC" "IC" "IC"
[5,] "OC" "IC" "IC" "IC" "IC"
I can't figure out what my error is because the function reports problems for the
Error in matrix_classification[, , k] <- cbind(var1, var2, var3, var4, :
incorrect number of subscripts
Below is the function:
function_classification<-function(n){
matrix_classification <- array()
for (k in nrow(scenario_1)){
x1=rnorm(n) scenario_1[k,1]
x4=rnorm(n) scenario_1[k,4]
x2=x1*p12 std_e2*rnorm(n) scenario_1[k,2]
x3=x1*p13 x4*p43 std_e3*rnorm(n) scenario_1[k,3]
x5=x2*p25 x3*p35 std_e5*rnorm(n) scenario_1[k,5]
x1<-as.data.frame(x1);x1
var1<-vector()
for(i in x1[,]){
marg<-function_marginal("X1","T1",i)
marginali<-marg$marginals$T1
names(marginali)<-c("IC","OC")
if(marginali["OC"]>0.5){
var1<-append(var1,"OC")
} else{
var1<-append(var1,"IC")
}
}
x2<-as.data.frame(x2);x2
var2<-vector()
for(i in x2[,]){
marg<-function_marginal("X2","T2",i)
marginali<-marg$marginals$T2
names(marginali)<-c("OC","IC")
if(marginali["OC"]>0.5){
var2<-append(var2,"OC")
} else{
var2<-append(var2,"IC")
}
}
x3<-as.data.frame(x3);x3
var3<-vector()
for(i in x3[,]){
marg<-function_marginal("X3","T3",i)
marginali<-marg$marginals$T3
names(marginali)<-c("IC","OC")
if(marginali["OC"]>0.5){
var3<-append(var3,"OC")
} else{
var3<-append(var3,"IC")
}
}
x4<-as.data.frame(x4);x4
var4<-vector()
for(i in x4[,]){
marg<-function_marginal("X4","T4",i)
marginali<-marg$marginals$T4
names(marginali)<-c("IC","OC")
if(marginali["OC"]>0.5){
var4<-append(var4,"OC")
} else{
var4<-append(var4,"IC")
}
}
x5<-as.data.frame(x5);x5
var5<-vector()
for(i in x5[,]){
marg<-function_marginal("X5","T5",i)
marginali<-marg$marginals$T5
names(marginali)<-c("IC","OC")
if(marginali["OC"]>0.5){
var5<-append(var5,"OC")
} else{
var5<-append(var5,"IC")
}
}
matrix_classification[,,k]<-cbind(var1,var2,var3,var4,var5)
colnames(matrix_classification[,,k])<-c("var1","var2","var3","var4","var5")
matrix_classification[,,k]<-as.matrix(matrix_classification[,,k])
}
return(matrix_classification)
}
I would call this function with the sapply function so that I can get both N unit samples and N samples of size N
dati_prova<-sapply(rep(1,5), function(x) function_classification(x))
CodePudding user response:
It's hard to give a concrete answer without a working reproducible example, since your function contains externally defined variables. However, the source of the error is clear. When you create an empty array with array()
it has a single fixed dimension:
matrix_classification <- array()
dim(matrix_classification)
#> [1] 1
And if you try to write into its third dimension you get an error:
k <- 1
matrix_classification[, , k] <- "x"
#> Error in matrix_classification[, , k] <- "x": incorrect number of subscripts
If you want to write into an array you should define its dimensions first. For example, the following creates an empty 5 x 5 x 5 array:
matrix_classification <- array("", dim = c(5, 5, 5))
dim(matrix_classification)
#> [1] 5 5 5
And if we want to write a matrix into the kth slice we can do:
matrix_classification[, , k] <- matrix(sample(letters, 25), nrow = 5)
matrix_classification[,,1]
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] "a" "d" "k" "t" "c"
#> [2,] "f" "b" "n" "m" "s"
#> [3,] "u" "q" "y" "o" "j"
#> [4,] "l" "g" "h" "w" "v"
#> [5,] "r" "i" "e" "p" "z"
Created on 2022-03-06 by the reprex package (v2.0.1)