英文:
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 <- 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
> 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论