假设我有一个由以下代码生成的系数列表,并且我希望它采用下面评论中显示的形式。有什么直接的方法吗?
library(leaps)
set.seed(1)
N = 1000
P = 20
x=matrix(rnorm(N*P),N,P)
eps=rnorm(N)
beta = sample(c(0,1), P, replace=T)
y = x %*% beta + eps
regfit.full= regsubsets(y~., data=data.frame(x=x[train,], y=y[train]), nvmax=20)
coefi = coef(regfit.full, id=3)
#Output:
#> coefi
#(Intercept) x.5 x.10 x.20
# 0.03730904 1.39039580 1.68618982 1.15607983
# How do I generate from coef a vector of the form
# transformed = [0 0 0 0 1.39039580 0 0 0 0 1.68618982 0...0 1.15607983]