1

我对 66 名患有内源性或外源性抑郁症的患者(endo)重复测量数据,并在 0-5 周内每周测量抑郁评分(hdrs,因此每名患者进行六次测量,包括基线)。数据为长格式:

mydata <- structure(list(id = c(101, 101, 101, 101, 101, 101, 103, 103, 
103, 103, 103, 103, 104, 104, 104, 104, 104, 104, 105, 105, 105, 
105, 105, 105, 106, 106, 106, 106, 106, 106, 107, 107, 107, 107, 
107, 107, 108, 108, 108, 108, 108, 108, 113, 113, 113, 113, 113, 
113, 114, 114, 114, 114, 114, 114, 115, 115, 115, 115, 115, 115, 
117, 117, 117, 117, 117, 117, 118, 118, 118, 118, 118, 118, 120, 
120, 120, 120, 120, 120, 121, 121, 121, 121, 121, 121, 123, 123, 
123, 123, 123, 123, 302, 302, 302, 302, 302, 302, 303, 303, 303, 
303, 303, 303, 304, 304, 304, 304, 304, 304, 305, 305, 305, 305, 
305, 305, 308, 308, 308, 308, 308, 308, 309, 309, 309, 309, 309, 
309, 310, 310, 310, 310, 310, 310, 311, 311, 311, 311, 311, 311, 
312, 312, 312, 312, 312, 312, 313, 313, 313, 313, 313, 313, 315, 
315, 315, 315, 315, 315, 316, 316, 316, 316, 316, 316, 318, 318, 
318, 318, 318, 318, 319, 319, 319, 319, 319, 319, 322, 322, 322, 
322, 322, 322, 327, 327, 327, 327, 327, 327, 328, 328, 328, 328, 
328, 328, 331, 331, 331, 331, 331, 331, 333, 333, 333, 333, 333, 
333, 334, 334, 334, 334, 334, 334, 335, 335, 335, 335, 335, 335, 
337, 337, 337, 337, 337, 337, 338, 338, 338, 338, 338, 338, 339, 
339, 339, 339, 339, 339, 344, 344, 344, 344, 344, 344, 345, 345, 
345, 345, 345, 345, 346, 346, 346, 346, 346, 346, 347, 347, 347, 
347, 347, 347, 348, 348, 348, 348, 348, 348, 349, 349, 349, 349, 
349, 349, 350, 350, 350, 350, 350, 350, 351, 351, 351, 351, 351, 
351, 352, 352, 352, 352, 352, 352, 353, 353, 353, 353, 353, 353, 
354, 354, 354, 354, 354, 354, 355, 355, 355, 355, 355, 355, 357, 
357, 357, 357, 357, 357, 360, 360, 360, 360, 360, 360, 361, 361, 
361, 361, 361, 361, 501, 501, 501, 501, 501, 501, 502, 502, 502, 
502, 502, 502, 504, 504, 504, 504, 504, 504, 505, 505, 505, 505, 
505, 505, 507, 507, 507, 507, 507, 507, 603, 603, 603, 603, 603, 
603, 604, 604, 604, 604, 604, 604, 606, 606, 606, 606, 606, 606, 
607, 607, 607, 607, 607, 607, 608, 608, 608, 608, 608, 608, 609, 
609, 609, 609, 609, 609, 610, 610, 610, 610, 610, 610), week = structure(c(0, 
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 
4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 
4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 
4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 
4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 
4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 
4, 5, 0, 1, 2,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, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 
    1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("Week 0", 
    "Week 1", "Week 2", "Week 3", "Week 4", "Week 5"), class = "factor"), 
    endo = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Exogenous", 
    "Endogenous"), class = "factor"), hdrs = c(26, 22, 18, 7, 
    4, 3, 33, 24, 15, 24, 15, 13, 29, 22, 18, 13, 19, 0, 22, 
    12, 16, 16, 13, 9, 21, 25, 23, 18, 20, NA, 21, 21, 16, 19, 
    NA, 6, 21, 22, 11, 9, 9, 7, 21, 23, 19, 23, 23, NA, NA, 17, 
    11, 13, 7, 7, NA, 16, 16, 16, 16, 11, 19, 16, 13, 12, 7, 
    6, NA, 26, 18, 18, 14, 11, 20, 19, 17, 18, 16, 17, 20, 22, 
    19, 19, 12, 14, 15, 15, 15, 13, 5, 5, 18, 22, 16, 8, 9, 12, 
    21, 21, 13, 14, 10, 5, 21, 27, 29, NA, 12, 24, 19, 17, 15, 
    11, 5, 1, 22, 21, 18, 17, 12, 11, 22, 22, 16, 19, 20, 11, 
    24, 19, 11, 7, 6, NA, 20, 16, 21, 17, NA, 15, 17, NA, 18, 
    17, 17, 6, 21, 19, 10, 11, 11, 8, 27, 21, 17, 13, 5, NA, 
    32, 26, 23, 26, 23, 24, 17, 18, 19, 21, 17, 11, 24, 18, 10, 
    14, 13, 12, 28, 21, 25, 32, 34, NA, 17, 18, 15, 8, 19, 17, 
    22, 24, 28, 26, 28, 29, 19, 21, 18, 16, 14, 10, 23, 20, 21, 
    20, 24, 14, 31, 25, NA, 7, 8, 11, 21, 21, 18, 15, 12, 10, 
    27, 22, 23, 21, 12, 13, 22, 20, 22, 23, 19, 18, 27, NA, 14, 
    12, 11, 12, NA, 21, 12, 13, 13, 18, 29, 27, 27, 22, 22, 23, 
    25, 24, 19, 23, 14, 21, 18, 15, 14, 10, 8, NA, 24, 21, 12, 
    13, 12, 5, 17, 19, 15, 12, 9, 13, 22, 25, 12, 16, 10, 16, 
    30, 27, 23, 20, 12, 11, 21, 19, 18, 15, 18, 19, 27, 21, 24, 
    22, 16, 11, 28, 27, 27, 26, 23, NA, 22, 26, 20, 13, 10, 7, 
    27, 22, 24, 25, 19, 19, 21, 28, 27, 29, 28, 33, 30, 22, 11, 
    8, 7, 19, 29, 30, 26, 22, 19, 24, 21, 22, 13, 11, 2, 1, 19, 
    17, 15, 16, 12, 12, 21, 11, 18, 0, 0, 4, 27, 26, 26, 25, 
    24, 19, 28, 22, 18, 20, 11, 13, 27, 27, 13, 5, 7, NA, 19, 
    33, 12, 12, 3, 1, 30, 39, 30, 27, 20, 4, 24, 19, 14, 12, 
    3, 4, NA, 25, 22, 14, 15, 2, 34, NA, 33, 23, NA, 11)), row.names = c(NA, 
-396L), class = "data.frame")

看起来像这样:

head(reisby_long)

   id week week_fact      endo hdrs
1 101    0    Week 0 Exogenous   26
2 101    1    Week 1 Exogenous   22
3 101    2    Week 2 Exogenous   18
4 101    3    Week 3 Exogenous    7
5 101    4    Week 4 Exogenous    4
6 101    5    Week 5 Exogenous    3

我的问题是我是否可以从该数据集中获得方差-协方差矩阵,而无需先将其转换为宽格式。我问是因为我有另一个数据集,将其转换为宽格式需要很长时间(因为我在这方面经验不足),这样做的唯一原因是获得方差-协方差矩阵.

谢谢!

4

1 回答 1

0

不确定您是否可以直接使用它,但您不必手动将数据更改为宽格式。您可以acast从 reshape2 库中使用。

library(reshape2)
mat <- reshape2::acast(data = mydata, formula = id ~ week)

您可以更改公式以获取所需的任何行和列数据。

于 2021-10-08T23:47:34.173 回答