我想获得一个矩阵数组,如下所示

发布于 2025-01-12 00:11:18 字数 1152 浏览 0 评论 0原文

我想生成一个数据矩阵数组,其中每个矩阵将引用不同的场景,出于示例的目的,我只包含 1 个

p13=0.493;p43=0.325;p25=0.335;p35=0.574;p12=0.868
std_e2=sqrt(1-p12^2);std_e2
std_e3=sqrt(1-(p13^2+p43^2));std_e3
std_e5=sqrt(1-(p25^2+p35^2+2*p25*p35*(p13*p12)));std_e5
scenario_1<-matrix(c(3,0,0,0,0),ncol = 5,nrow = 1);scenario_1

genereting_fuction<- function(n,scenario){ 
  sample <- vector("list") 
  for (i in scenario){
    x1=rnorm(n)+scenario[i,1]
    x4=rnorm(n)+scenario[i,4]
    x2=x1*p12+std_e2*rnorm(n)+scenario[i,2]
    x3=x1*p13+x4*p43+std_e3*rnorm(n)+scenario[i,3]
    x5=x2*p25+x3*p35+std_e5*rnorm(n)+scenario[i,5]
    sample[[i]]=cbind(x1,x2,x3,x4,x5)
    colnames(sample[[i]])<-c("x1","x2","x3","x4","x5")
  }
  sample
} 
array_scenari<-array(dim=c(5,5,2));array_scenari

for(j in 1:nrow(scenario_1)){
  set.seed(1234)
  dati_prova<- sapply(rep(1,5), function(x) genereting_fuction(x,scenario_1),simplify = 'array');dati_prova
  dati_prova<-do.call(rbind.data.frame, dati_prova);dati_prova<-as.matrix(dati_prova);dati_prova
  dati_prova<-as.matrix(dati_prova)
  array_scenari[,,j]<-dati_prova[]
}

我无法理解为什么它不起作用并给我一个错误

I would like to generate an array of data matrices, where each matrix will refer to a different scenario, for the purpose of example I have included only 1

p13=0.493;p43=0.325;p25=0.335;p35=0.574;p12=0.868
std_e2=sqrt(1-p12^2);std_e2
std_e3=sqrt(1-(p13^2+p43^2));std_e3
std_e5=sqrt(1-(p25^2+p35^2+2*p25*p35*(p13*p12)));std_e5
scenario_1<-matrix(c(3,0,0,0,0),ncol = 5,nrow = 1);scenario_1

genereting_fuction<- function(n,scenario){ 
  sample <- vector("list") 
  for (i in scenario){
    x1=rnorm(n)+scenario[i,1]
    x4=rnorm(n)+scenario[i,4]
    x2=x1*p12+std_e2*rnorm(n)+scenario[i,2]
    x3=x1*p13+x4*p43+std_e3*rnorm(n)+scenario[i,3]
    x5=x2*p25+x3*p35+std_e5*rnorm(n)+scenario[i,5]
    sample[[i]]=cbind(x1,x2,x3,x4,x5)
    colnames(sample[[i]])<-c("x1","x2","x3","x4","x5")
  }
  sample
} 
array_scenari<-array(dim=c(5,5,2));array_scenari

for(j in 1:nrow(scenario_1)){
  set.seed(1234)
  dati_prova<- sapply(rep(1,5), function(x) genereting_fuction(x,scenario_1),simplify = 'array');dati_prova
  dati_prova<-do.call(rbind.data.frame, dati_prova);dati_prova<-as.matrix(dati_prova);dati_prova
  dati_prova<-as.matrix(dati_prova)
  array_scenari[,,j]<-dati_prova[]
}

I can't understand why it doesn't work and gives me an error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪化雨蝶 2025-01-19 00:11:18

如果没有有效的可重现示例,很难给出具体答案,因为您的函数包含外部定义的变量。然而,错误的根源是明确的。当你用 array() 创建一个空数组时,它有一个固定的维度:

matrix_classification <- array()

dim(matrix_classification)
#> [1] 1

如果你尝试写入它的第三个维度,你会得到一个错误:

k <- 1

matrix_classification[, , k] <- "x"
#> Error in matrix_classification[, , k] <- "x": incorrect number of subscripts

如果你想写入一个数组,你应该定义首先是它的尺寸。例如,以下代码创建一个空的 5 x 5 x 5 数组:

matrix_classification <- array("", dim = c(5, 5, 5))

dim(matrix_classification)
#> [1] 5 5 5

如果我们想将矩阵写入第 k 个切片,我们可以这样做:

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"

创建于 2022 年 3 月 6 日,由reprex 包 (v2.0.1)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文