创建一个特定的选择矩阵

huangapple go评论197阅读模式
英文:

Create a specific selection matrix

问题

如果我有n个主题,每个主题重复t次。如果我想创建一个选择矩阵,它将如下所示:

  1. n = 5
  2. t = 3
  3. Select_M = diag(n) %x% matrix(1, t)
  4. Select_M
  5. [,1] [,2] [,3] [,4] [,5]
  6. [1,] 1 0 0 0 0
  7. [2,] 1 0 0 0 0
  8. [3,] 1 0 0 0 0
  9. [4,] 0 1 0 0 0
  10. [5,] 0 1 0 0 0
  11. [6,] 0 1 0 0 0
  12. [7,] 0 0 1 0 0
  13. [8,] 0 0 1 0 0
  14. [9,] 0 0 1 0 0
  15. [10,] 0 0 0 1 0
  16. [11,] 0 0 0 1 0
  17. [12,] 0 0 0 1 0
  18. [13,] 0 0 0 0 1
  19. [14,] 0 0 0 0 1
  20. [15,] 0 0 0 0 1

我希望为每个主题创建不同的时间段。换句话说,第一个主题重复7次,第二个主题重复11次,依此类推。

我如何高效地为这些特定的重复创建选择矩阵?

英文:

Say, I have n subjects and each is repeated t times. If I want to create a selection matrix, it would be as follows

  1. n = 5
  2. t = 3
  3. Select_M = diag(n) %x% matrix(1, t)
  4. Select_M
  5. [,1] [,2] [,3] [,4] [,5]
  6. [1,] 1 0 0 0 0
  7. [2,] 1 0 0 0 0
  8. [3,] 1 0 0 0 0
  9. [4,] 0 1 0 0 0
  10. [5,] 0 1 0 0 0
  11. [6,] 0 1 0 0 0
  12. [7,] 0 0 1 0 0
  13. [8,] 0 0 1 0 0
  14. [9,] 0 0 1 0 0
  15. [10,] 0 0 0 1 0
  16. [11,] 0 0 0 1 0
  17. [12,] 0 0 0 1 0
  18. [13,] 0 0 0 0 1
  19. [14,] 0 0 0 0 1
  20. [15,] 0 0 0 0 1

My interest is to have different time periods for each subject. In other words, the first subject is repeated 7 times, the second subject is repeated 11 times, and so on.

How can I efficiently create a selection matrix for these specific repeats?

答案1

得分: 2

你可以定义一个表示不同时间段的因子列,然后使用 model.matrix() 来创建一个设计矩阵,不包括截距项。

例如,第一个主题重复了2次,第二个主题重复了3次,第三个主题重复了4次,你可以尝试如下:

  1. t = c(2, 3, 4)
  2. data.frame(sub = factor(rep(seq_along(t), t))) |>
  3. model.matrix(~ sub - 1, data = _)
  4. # sub1 sub2 sub3
  5. # 1 1 0 0
  6. # 2 1 0 0
  7. # 3 0 1 0
  8. # 4 0 1 0
  9. # 5 0 1 0
  10. # 6 0 0 1
  11. # 7 0 0 1
  12. # 8 0 0 1
  13. # 9 0 0 1

注意:我只提供翻译,请不要再问我需要翻译的内容。

英文:

You can define a factor column indicating different time periods, and then use model.matrix() to create a design matrix from it without the intercept term.

For example, the first subject is repeated 2 times, the second subject 3 times, and the third subject 4 times, you can try as follows:

  1. t = c(2, 3, 4)
  2. data.frame(sub = factor(rep(seq_along(t), t))) |>
  3. model.matrix(~ sub - 1, data = _)
  4. # sub1 sub2 sub3
  5. # 1 1 0 0
  6. # 2 1 0 0
  7. # 3 0 1 0
  8. # 4 0 1 0
  9. # 5 0 1 0
  10. # 6 0 0 1
  11. # 7 0 0 1
  12. # 8 0 0 1
  13. # 9 0 0 1

答案2

得分: 2

使用 bdiag 函数

  1. library(Matrix)
  2. t <- 2:4
  3. as.matrix(bdiag(lapply(as.list(t), \(x) rep(1, x))))

输出

  1. [,1] [,2] [,3]
  2. [1,] 1 0 0
  3. [2,] 1 0 0
  4. [3,] 0 1 0
  5. [4,] 0 1 0
  6. [5,] 0 1 0
  7. [6,] 0 0 1
  8. [7,] 0 0 1
  9. [8,] 0 0 1
  10. [9,] 0 0 1
英文:

Using bdiag

  1. library(Matrix)
  2. t &lt;- 2:4
  3. as.matrix(bdiag(lapply(as.list(t), \(x) rep(1, x))))

-output

  1. [,1] [,2] [,3]
  2. [1,] 1 0 0
  3. [2,] 1 0 0
  4. [3,] 0 1 0
  5. [4,] 0 1 0
  6. [5,] 0 1 0
  7. [6,] 0 0 1
  8. [7,] 0 0 1
  9. [8,] 0 0 1
  10. [9,] 0 0 1

答案3

得分: 2

可以使用以下基本的R代码:

  1. > v <- 2:4
  2. > +(col(m <- replicate(length(v), rep(seq_along(v), v))) == m)
  3. [,1] [,2] [,3]
  4. [1,] 1 0 0
  5. [2,] 1 0 0
  6. [3,] 0 1 0
  7. [4,] 0 1 0
  8. [5,] 0 1 0
  9. [6,] 0 0 1
  10. [7,] 0 0 1
  11. [8,] 0 0 1
  12. [9,] 0 0 1
英文:

We can use the following base R code

  1. &gt; v &lt;- 2:4
  2. &gt; +(col(m &lt;- replicate(length(v), rep(seq_along(v), v))) == m)
  3. [,1] [,2] [,3]
  4. [1,] 1 0 0
  5. [2,] 1 0 0
  6. [3,] 0 1 0
  7. [4,] 0 1 0
  8. [5,] 0 1 0
  9. [6,] 0 0 1
  10. [7,] 0 0 1
  11. [8,] 0 0 1
  12. [9,] 0 0 1

huangapple
  • 本文由 发表于 2023年5月26日 09:28:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定