在R中对矩阵进行 Bootstrap ,重复100次( Bootstrap 相关矩阵)

67up9zun  于 8个月前  发布在  Bootstrap
关注(0)|答案(1)|浏览(108)

在这里,我展示了数据集。

library(igraph)
vetor <- c(1, 5, 3, 8, 2, 9, 3, 2:15, 1, 5, 3, 6, 5, 9, 3 )
matrix(vetor, 7, 7)
matrix<-matrix(vetor, 7, 7)
matcor <- cor(matrix, method = "spearman")

matrixnetwork = graph.adjacency(matcor, mode="undirected", weighted = 
TRUE, add.colnames=NULL, diag=FALSE)
plot(matrixnetwork)

我需要通过随机排列每个感兴趣区域内的值的顺序来计算 Bootstrap 相关矩阵,然后计算每个 Bootstrap 矩阵的图形值。我将运行随后的比较分析(t检验)

Corr_fun = function(matrix, indices) {
return(cor(matrix, method= "spearman")) }
BIboot= boot(matrix, statistic= Corr_fun, R = 100)

AIBOOT<-BIboot[["t"]]

这个形式返回一个49 x100的矩阵,当我运行下面的命令时,说:图形中的错误。邻接。密集(adjmatrix,mode = mode,weighted = weighted,:而不是一个方阵。

g_boot <- graph_from_adjacency_matrix(AIBOOT, mode = "undirected", weighted = TRUE)

此外,它返回重复的值

我也试着:

library(bootstrap)
theta = function(matrix, indices) {
return(as.matrix(matrix)) }  #funcao para centralidade
BOOTSAVE<-bootstrap(matrix, nboot = 100, theta)

结果如下:

我也尝试置换图,这里的“matrixnetwork”是来自comand的adjacency_matrix,但这种形式我需要做1乘1,我需要它100倍的方式,使它很容易测量图,并进行后续比较:

matrixnetwork2<-permute(matrixnetwork, sample(vcount(matrixnetwork)))

我尝试这个形式,似乎工作,这里“aibi_imuno_BI_1”是我的数据。我不把“cm <- cormat[指数,指数]",因为这是重复相同的面积2x

N <- 7L set.seed(2023) 
R <- 100L 
BIboot <- vector("list", length = R) 
for(i in seq.int(R)) { indices <- sample(N, replace = TRUE) 
BIboot[[i]] <- cor(aibi_imuno_BI_1, method = "spearman") } 
 g_boot_list <- lapply(BIboot, graph_from_adjacency_matrix, mode = 
"undirected", weighted = TRUE, add.colnames=NULL, diag=FALSE) plot 
 (g_boot_list[[1]])

更正后的格式如下:

# BOOT 1
N <- 7L
set.seed(2023)
R <- 100L
BIboot <- vector("list", length = R)
for(i in seq.int(R)) {
indices <- sample(N, replace = TRUE)
BIboot[[i]] <- cor(aibi_imuno_BI_1[indices, ], method = "spearman")
}
g_boot_list <- lapply(BIboot, graph_from_adjacency_matrix, mode = 
"undirected", weighted = TRUE, add.colnames=NULL, diag=FALSE)
plot (g_boot_list[[1]])

这里我有100个 Bootstrap 相关矩阵,
最后我试着:

#############BOTPERMUTATION FORMA 2 ####################
N <- 7L
set.seed(2023)
R <- 100L
BIPERMUT<- vector("list", length = R)
for(i in seq.int(R)) {
  indices <- sample(N, replace = TRUE)
  BIPERMUT[[i]] <- permute(matrixnetwork, 
 sample(vcount(matrixnetwork)))  
}

plot (BIPERMUT[[1]])
degree(BIPERMUT[[1]], normalized = FALSE, loops = FALSE)

 #################FORMA 3 #######################
 N <- 7L
set.seed(2023)
R <- 100L
BIboot2 <- vector("list", length = R)
for(i in seq.int(R)) {
  indices <- sample(N, replace = TRUE)
  BIboot2[[i]] <- matrixnetwork
}

plot (BIboot2[[1]])
degree(BIboot2[[1]], normalized = FALSE, loops = FALSE)
nnsrf1az

nnsrf1az1#

posted bootstrap函数不是对输入相关矩阵进行子集设置,而是对整个矩阵R重复相同的计算。它的命名方式也是错误的。问题的代码是 Bootstrap 函数cor,而不是矩阵。
这里有一个解决方案。
首先创建一个结果列表BIboot。然后通过 Bootstrap 相关性填充列表:

  • 样本N行数与替换;
  • 创建这些行和列的矩阵;
  • 计算感兴趣的统计量。

创建相关列表后,使用它在最后的lapply循环中创建R无向图的列表g_boot_list

library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union

N <- 7L
fakecors <- runif(choose(N, 2))
cormat <- matrix(nrow = N, ncol = N)
cormat[lower.tri(cormat)] <- fakecors
diag(cormat) <- 0
cormat[upper.tri(cormat)] <- t(cormat)[upper.tri(t(cormat))]

set.seed(2023)
R <- 10L
BIboot <- vector("list", length = R)
for(i in seq.int(R)) {
  indices <- sample(N, replace = TRUE)
  cm <- cormat[indices, indices]
  BIboot[[i]] <- cor(cm, method = "spearman")
}
g_boot_list <- lapply(BIboot, graph_from_adjacency_matrix, mode = "undirected", weighted = TRUE)

创建于2023-10-05使用reprex v2.0.2

相关问题