r - 如何为循环中的每个文件迭代保存来自 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()
}
解决方案
推荐阅读
- python - 全局 eventFilter 不会在 Qt 中的 QThread 中捕获目标事件
- r - if (length(scores.temp) == 1 && scores.temp == 0) { 中的错误:需要 TRUE/FALSE 的缺失值
- arrays - 在 Dart/Flutter 中反序列化 json 数组
- mysql - 将错误消息列值与查询中使用的列名匹配
- javascript - 预加载器在某些浏览器中不会停止。如何解决这个问题?
- python - 尝试连接到无效的主机和端口时,我的程序崩溃
- java - IDE 无法识别 Spring 框架导入
- python - 带有 Win32Com 的 Python 脚本未在任务调度程序中运行
- gitlab - 与其他变量连接时从 GitLab 中的秘密变量中获取值
- mysql - 在sql中列的值之间创建行