Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 变换矩阵(以便使用ggplot2绘制回归线)_R_Matrix_Ggplot2 - Fatal编程技术网

R 变换矩阵(以便使用ggplot2绘制回归线)

R 变换矩阵(以便使用ggplot2绘制回归线),r,matrix,ggplot2,R,Matrix,Ggplot2,我运行了一个多级回归,现在有一个系数矩阵,由回归中每组(=一个因子变量)的值+标准误差组成,例如,我的矩阵(截距+一个称为Beta1)如下所示: Group Intercept Beta1 Intercept.se Beta1.se 11 0.044357458 0.4381340 0.08358735 0.1572632 12 -0.007072542 0.1242737 0.09317142 0.1643544 21

我运行了一个多级回归,现在有一个系数矩阵,由回归中每组(=一个因子变量)的值+标准误差组成,例如,我的矩阵(截距+一个称为
Beta1
)如下所示:

Group    Intercept      Beta1 Intercept.se      Beta1.se
   11  0.044357458  0.4381340   0.08358735     0.1572632
   12 -0.007072542  0.1242737   0.09317142     0.1643544
   21  0.021075871  0.3727055   0.12050036     0.2459456
   22  0.023895981  0.6786013   0.11207848     0.3188887
   31 -0.115713481  0.3547718   0.09760681     0.1454787
   32 -0.004081244 -0.1954594   0.09993201     0.1953406
      Group         int       slo
1        11  0.09484568 0.3005997
2        11  0.12364749 0.5758899
3        11 -0.02942938 0.4821841
4        11  0.17226587 0.2413752
5        11  0.02923023 0.4251419
6        11  0.14650632 0.4541752
7        12  0.06784996 0.0356669
8        12 -0.02832304 0.2214471
...
ggplot(myData, aes(x=Beta1, y=Outcome)) +
  geom_jitter() +
  facet_wrap(~ Group) +
  geom_abline(aes(intercept=int, slope=slo), data=lines)
我想要实现的是绘制一张图表,显示每组可能的回归线。我提出了以下代码,为每个组生成15行(
coef.mtx
是上面提到的矩阵):

这将生成如下数据帧:

Group    Intercept      Beta1 Intercept.se      Beta1.se
   11  0.044357458  0.4381340   0.08358735     0.1572632
   12 -0.007072542  0.1242737   0.09317142     0.1643544
   21  0.021075871  0.3727055   0.12050036     0.2459456
   22  0.023895981  0.6786013   0.11207848     0.3188887
   31 -0.115713481  0.3547718   0.09760681     0.1454787
   32 -0.004081244 -0.1954594   0.09993201     0.1953406
      Group         int       slo
1        11  0.09484568 0.3005997
2        11  0.12364749 0.5758899
3        11 -0.02942938 0.4821841
4        11  0.17226587 0.2413752
5        11  0.02923023 0.4251419
6        11  0.14650632 0.4541752
7        12  0.06784996 0.0356669
8        12 -0.02832304 0.2214471
...
ggplot(myData, aes(x=Beta1, y=Outcome)) +
  geom_jitter() +
  facet_wrap(~ Group) +
  geom_abline(aes(intercept=int, slope=slo), data=lines)
然后我可以用ggplot画出这些线,像这样:

Group    Intercept      Beta1 Intercept.se      Beta1.se
   11  0.044357458  0.4381340   0.08358735     0.1572632
   12 -0.007072542  0.1242737   0.09317142     0.1643544
   21  0.021075871  0.3727055   0.12050036     0.2459456
   22  0.023895981  0.6786013   0.11207848     0.3188887
   31 -0.115713481  0.3547718   0.09760681     0.1454787
   32 -0.004081244 -0.1954594   0.09993201     0.1953406
      Group         int       slo
1        11  0.09484568 0.3005997
2        11  0.12364749 0.5758899
3        11 -0.02942938 0.4821841
4        11  0.17226587 0.2413752
5        11  0.02923023 0.4251419
6        11  0.14650632 0.4541752
7        12  0.06784996 0.0356669
8        12 -0.02832304 0.2214471
...
ggplot(myData, aes(x=Beta1, y=Outcome)) +
  geom_jitter() +
  facet_wrap(~ Group) +
  geom_abline(aes(intercept=int, slope=slo), data=lines)
最终结果如下所示:

Group    Intercept      Beta1 Intercept.se      Beta1.se
   11  0.044357458  0.4381340   0.08358735     0.1572632
   12 -0.007072542  0.1242737   0.09317142     0.1643544
   21  0.021075871  0.3727055   0.12050036     0.2459456
   22  0.023895981  0.6786013   0.11207848     0.3188887
   31 -0.115713481  0.3547718   0.09760681     0.1454787
   32 -0.004081244 -0.1954594   0.09993201     0.1953406
      Group         int       slo
1        11  0.09484568 0.3005997
2        11  0.12364749 0.5758899
3        11 -0.02942938 0.4821841
4        11  0.17226587 0.2413752
5        11  0.02923023 0.4251419
6        11  0.14650632 0.4541752
7        12  0.06784996 0.0356669
8        12 -0.02832304 0.2214471
...
ggplot(myData, aes(x=Beta1, y=Outcome)) +
  geom_jitter() +
  facet_wrap(~ Group) +
  geom_abline(aes(intercept=int, slope=slo), data=lines)


有没有更好的方法来变换系数矩阵而不是使用这个循环?我想不出更好的办法。。。或者:您将如何可视化可能的回归线(而不仅仅是点估计值)?

您能否澄清哪些位是统计问题(此处的主题)哪些不是?如果您在其他地方发布了您的编程问题,您需要说明为什么要避免for循环。@mdewey:统计问题(用于交叉验证):是否有更好的方法来可视化可能的回归线范围(无论是编程还是编程语言)?以及在进行时(StackExchange问题):有没有办法避免循环?(循环本身没有问题,我只是R方面的一个相对初学者,所以我想知道我是否忽略了一些明显的、简单的转换函数…)