如何为循环中的每个文件迭代保存来自grangertest()函数的数据?

时间:2018-10-07 14:41:02

标签: r statistics

我的代码如下。我想保存循环中每个文件的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()

}

0 个答案:

没有答案