是否可以在R中运行滚动调解和摘要捕获?
PerformanceAnalytics包中的apply.rolling函数由于多变量中介函数的复杂性并将摘要捕获到表中而无法正常工作。
每个中介跨越336行数据:
Afrah.timelapse.1_336.model.m <- lm(Aridia.Faction.Kills$Afrah[1:336] ~ Aridia.Ship.Jumps$Afrah[1:336])
Afrah.timelapse.1_336.model.y <- lm(Aridia.Faction.Kills$Afrah[1:336] ~ Aridia.Ship.Kills$Afrah[1:336] + Aridia.Ship.Jumps$Afrah[1:336])
set.seed(1)
Afrah.timelapse.1_336.Mediation <- mediate(Afrah.timelapse.1_336.model.m, Afrah.timelapse.1_336.model.y, treat="Aridia.Ship.Jumps$Afrah[1:336]", mediator="Aridia.Ship.Kills$Afrah[1:336]", sims=1000)
summary(Afrah.timelapse.1_336.Mediation)
summary(Afrah.timelapse.1_336.Mediation) %>%
capture.output() %>%
discard(`==`, "") -> lines
Afrah.timelapse.1_336.Mediation.DF <- data.frame(lines[which(grepl("^ ", lines)):(which(grepl("^Sample", lines))-1)] %>%
sub("^ ", "Afrah.timelapse.1_336.Mediation", .) %>%
gsub("95. CI Lower", "95_CI_Lower", .) %>%
gsub("95. CI Upper", "95_CI_Upper", .) %>%
sub(" \\(", "_(", .) %>%
sub("p-", "p_", .) %>%
sub(" ", "_", ., fixed=TRUE) %>%
textConnection() %>%
read.table(header=TRUE))
Afrah.timelapse.1_336.Mediation.table.row <- data.frame('Afrah.timelapse.1_336.Mediation',Afrah.timelapse.1_336.Mediation.DF[1,2],Afrah.timelapse.1_336.Mediation.DF[1,3],Afrah.timelapse.1_336.Mediation.DF[1,4],Afrah.timelapse.1_336.Mediation.DF[1,5],Afrah.timelapse.1_336.Mediation.DF[2,2],Afrah.timelapse.1_336.Mediation.DF[2,3],Afrah.timelapse.1_336.Mediation.DF[2,4],Afrah.timelapse.1_336.Mediation.DF[2,5],Afrah.timelapse.1_336.Mediation.DF[3,2],Afrah.timelapse.1_336.Mediation.DF[3,3],Afrah.timelapse.1_336.Mediation.DF[3,4],Afrah.timelapse.1_336.Mediation.DF[3,5],Afrah.timelapse.1_336.Mediation.DF[4,2],Afrah.timelapse.1_336.Mediation.DF[4,3],Afrah.timelapse.1_336.Mediation.DF[4,4],Afrah.timelapse.1_336.Mediation.DF[4,5])
names(Afrah.timelapse.1_336.Mediation.table.row) <- c("Med_Area","ACME_Est","ACME_95pCIL","ACME_95pCIU","ACME_pv","ADE_Est","ADE_95pCIL","ADE_95pCIU","ADE_pv","TE_Est","TE_95pCIL","TE_95pCIU","TE_pv","PM_Est","PM_95pCIL","PM_95pCIU","PM_pv")
write.table(Afrah.timelapse.1_336.Mediation.table.row, file="H:/System.Afrah.timelapse.1_336.Mediation.table.row.csv",quote=FALSE,row.names=FALSE,col.names=TRUE,sep=",")
Afrah.timelapse.table[nrow(Afrah.timelapse.table)+1,] <- c('Afrah.timelapse.1_336.Mediation',Afrah.timelapse.1_336.Mediation.DF[1,2],Afrah.timelapse.1_336.Mediation.DF[1,3],Afrah.timelapse.1_336.Mediation.DF[1,4],Afrah.timelapse.1_336.Mediation.DF[1,5],Afrah.timelapse.1_336.Mediation.DF[2,2],Afrah.timelapse.1_336.Mediation.DF[2,3],Afrah.timelapse.1_336.Mediation.DF[2,4],Afrah.timelapse.1_336.Mediation.DF[2,5],Afrah.timelapse.1_336.Mediation.DF[3,2],Afrah.timelapse.1_336.Mediation.DF[3,3],Afrah.timelapse.1_336.Mediation.DF[3,4],Afrah.timelapse.1_336.Mediation.DF[3,5],Afrah.timelapse.1_336.Mediation.DF[4,2],Afrah.timelapse.1_336.Mediation.DF[4,3],Afrah.timelapse.1_336.Mediation.DF[4,4],Afrah.timelapse.1_336.Mediation.DF[4,5])
write.table(Afrah.timelapse.table, file="H:/System.Afrah.timelapse.rolling.table.csv",quote=FALSE,row.names=FALSE,col.names=TRUE,sep=",")
我能够遇到的唯一多变量示例涉及嵌套for循环,除非我误解它们(很可能),嵌套循环不适用于中介函数或摘要捕获,但适用于表格编写部分。
到目前为止,我的抄袭方法是复制以上内容并搜索/替换&#39; 1_336&#39;和&#39; 1:336&#39;连续出现(例如&#39; 2_337&#39;和#3; 337&#39;),但这几乎不可持续或有效。