gpt4 book ai didi

r - 循环创建错误数量的图

转载 作者:行者123 更新时间:2023-12-04 12:17:13 24 4
gpt4 key购买 nike

我是 R 的新手,已经在互联网上大量搜索了解决方案,但没有找到对我有帮助的东西。

我对从 for 循环创建的图有疑问。我想在 3x2 网格中创建六个图表,其中所有图表一起显示,并且每个图表都以数据的列标签作为标题。然而,R 似乎并没有创建 6 个,而是创建了两倍数量的图表,而其中 6 个的标题是错误的(例如系列 R[,i] 而不是 DJINDUS)。

我的数据集(这是一个名为“R”的矩阵)如下所示,有几千行:

         DJINDUS       GSCITOT      NASCOMP       DJTRSPT       DJUTILS     PORTFOLIO
[1,] -0.0051797207 0.0038918968 -0.014959142 -0.0100216161 0.0025465706 -0.0047444023
[2,] 0.0103448153 -0.0023765810 0.013946279 0.0164086084 0.0073205395 0.0091287322
[3,] 0.0038053079 -0.0131512085 0.004045313 -0.0046264989 0.0053159370 -0.0009222299
[4,] 0.0234136525 -0.0107384963 0.013765670 0.0120004619 0.0099954557 0.0096873488
[5,] -0.0019038671 -0.0064305092 0.006087090 0.0026823416 0.0046855706 0.0010241253
[6,] 0.0103801289 0.0012181536 0.009976454 0.0081995006 0.0015569907 0.0062662457
[7,] -0.0005007307 0.0075137168 0.004886162 0.0041719526 -0.0009155833 0.0030311035
[8,] 0.0058533353 0.0066382973 0.003438548 0.0047356660 0.0011900949 0.0043711882
[9,] 0.0058077604 0.0080331085 0.003620382 0.0058304700 -0.0024733215 0.0041636798
[10,] -0.0039335841 0.0120232095 0.006624884 0.0074794664 -0.0043200582 0.0035747834

par(mfcol=c(3,2))

for (i in 1:6){
ACF.R <-acf(R[,i], lag.max=20, type="correlation", na.action = na.fail)
header <- colnames(R)[i]
plot(ACF.R, ci=0.95, type = "h", xlab = "Lag",
ylab = "ACF", ci.type = "white", main = header)
}

这是(错误的)图形输出 looks like 的方式

enter image description here

请您检查我的代码并告诉我问题出在哪里好吗?

最佳答案

只需在您的 acf 调用中添加 plot=FALSE:

for (i in 1:6){
ACF.R <-acf(R[,i], lag.max=20, type="correlation", na.action = na.fail,
plot=FALSE)
header <- colnames(R)[i]
plot(ACF.R, ci=0.95, type = "h", xlab = "Lag",
ylab = "ACF", ci.type = "white", main = header)
}

这应该产生以下情节:

enter image description here

关于r - 循环创建错误数量的图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20827044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com