创建一个特定的选择矩阵

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

Create a specific selection matrix

问题

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

n = 5
t = 3
Select_M = diag(n) %x% matrix(1, t)

Select_M
      [,1] [,2] [,3] [,4] [,5]
 [1,]    1    0    0    0    0
 [2,]    1    0    0    0    0
 [3,]    1    0    0    0    0
 [4,]    0    1    0    0    0
 [5,]    0    1    0    0    0
 [6,]    0    1    0    0    0
 [7,]    0    0    1    0    0
 [8,]    0    0    1    0    0
 [9,]    0    0    1    0    0
[10,]    0    0    0    1    0
[11,]    0    0    0    1    0
[12,]    0    0    0    1    0
[13,]    0    0    0    0    1
[14,]    0    0    0    0    1
[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

n = 5
t = 3
Select_M = diag(n) %x% matrix(1, t)

Select_M
      [,1] [,2] [,3] [,4] [,5]
 [1,]    1    0    0    0    0
 [2,]    1    0    0    0    0
 [3,]    1    0    0    0    0
 [4,]    0    1    0    0    0
 [5,]    0    1    0    0    0
 [6,]    0    1    0    0    0
 [7,]    0    0    1    0    0
 [8,]    0    0    1    0    0
 [9,]    0    0    1    0    0
[10,]    0    0    0    1    0
[11,]    0    0    0    1    0
[12,]    0    0    0    1    0
[13,]    0    0    0    0    1
[14,]    0    0    0    0    1
[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次,你可以尝试如下:

t = c(2, 3, 4)

data.frame(sub = factor(rep(seq_along(t), t))) |>
  model.matrix(~ sub - 1, data = _)

#   sub1 sub2 sub3
# 1    1    0    0
# 2    1    0    0
# 3    0    1    0
# 4    0    1    0
# 5    0    1    0
# 6    0    0    1
# 7    0    0    1
# 8    0    0    1
# 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:

t = c(2, 3, 4)

data.frame(sub = factor(rep(seq_along(t), t))) |>
  model.matrix(~ sub - 1, data = _)

#   sub1 sub2 sub3
# 1    1    0    0
# 2    1    0    0
# 3    0    1    0
# 4    0    1    0
# 5    0    1    0
# 6    0    0    1
# 7    0    0    1
# 8    0    0    1
# 9    0    0    1

答案2

得分: 2

使用 bdiag 函数

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

输出

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

Using bdiag

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

-output

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

答案3

得分: 2

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

> v <- 2:4

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

We can use the following base R code

&gt; v &lt;- 2:4

&gt; +(col(m &lt;- replicate(length(v), rep(seq_along(v), v))) == m)
      [,1] [,2] [,3]
 [1,]    1    0    0
 [2,]    1    0    0
 [3,]    0    1    0
 [4,]    0    1    0
 [5,]    0    1    0
 [6,]    0    0    1
 [7,]    0    0    1
 [8,]    0    0    1
 [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:

确定