首页 > 解决方案 > 如何为循环中的每个文件迭代保存来自 grangertest() 函数的数据?

问题描述

我的代码如下。我想从循环中运行的每个文件中保存 F 和 P 值。我想这必须发生在“i”循环中。对于每个文件,即每次迭代,我想将 F 和 P 值输出到一个文件夹。

library(rhdf5)
library(lmtest)
library(ggplot2)
library(zoo)
library(reshape2)


setwd("...")
myFiles <- list.files()



for(j in 1:length(myFiles)) {  

  setwd(...")
  workingFile <- h5read(myFiles[j], 'data')



 MO <- c(1:2)
 GC1 <- vector(len=2)
 GC2 <- vector(len=2)


 for (i in 1:2) {
   GC1[i] <- grangertest(IPs, DLPFc, i)
   GC2[i] <- grangertest(DLPFc, IPs, i)

   }

setwd(...)

 df1 <- data.frame(MO, -log(GC1.p), -log(GC2.p))

 colnames(df1) <- c("MO", "IPs -> DLPFc", "DLPFc -> IPs")
 data1 <- melt(df1, id.vars = "MO", measure.vars = c("IPs -> DLPFc", 
 "DLPFc -> IPs"))
 ggplot(data1, aes(x = MO, y = value)) +
 geom_line(aes(colour=variable, group=variable), size=2) +
labs(x = "Model Length (ms)", y = "-Log(P-Value)") + theme(axis.title 
 = element_text(size = 20)) +
geom_hline(yintercept = 3, col='green', size = 1.5) + 
theme(legend.position="none") + theme(axis.text.x = 
element_text(face="bold", color="black",
size=15, angle=0), axis.text.y = element_text(face="bold", 
color="black", size=15, angle=0)) +
scale_y_continuous(limits = c(0,30), breaks = c(0, 3.0))
ggsave(paste0(myFiles[j], ".png"), path = 
".../")
dev.off()

}

标签: rstatistics

解决方案


推荐阅读