R 中的复杂复制函数
Complex Replicate function in R
我在 R 中有一个代码,它最终生成一个名为 sigma
的向量和 3 个子集,即 sub1.sigma
、sb2.sigma
、sub3.sigma
.我想将此过程复制 n 次,比方说 10 次,然后观察上述向量上的值。我按如下方式使用复制功能
set.seed(2021)
code <- replicate(10,{
data<-matrix(rnorm(100*5,mean=0,sd=1), 100, 5)
colnames(data) <- c("X1", "X2", "X3", "X4", "X5")
data <- as.data.frame(data)
a <- 5
b <- 0.8
c <- 100
data[,2] <- a*data[,1] - b*rnorm(c)
data[,3] <- a*data[,1] + b*rnorm(c)
data[,4] <- a*data[,1] - b*rnorm(c)
library(glmnet)
library(coefplot)
A <- as.matrix(data)
set.seed(1)
results <- lapply(seq_len(ncol(A)), function(i) {
list(
cvfit = cv.glmnet(A[, -i] , A[, i] , standardize = TRUE , type.measure = "mse" , nfolds = 10 , alpha = 1)
)
})
lam <- as.data.frame(`names<-`(
lapply(results, function(x) (x$cvfit$lambda.min)),
paste0("X", seq_along(results))
))
sigma<- matrix(rnorm(1*5,mean=0,sd=1), 1, 5)
colnames(sigma) <- c("X1", "X2", "X3", "X4", "X5")
as.vector(sigma)
sub1.sigma <- subset(sigma, select = sigma <= sum(lam))
sub2.sigma <- subset(sigma, select = sigma <= 2*sum(lam))
sub3.sigma <- subset(sigma, select = sigma <= 3*sum(lam))
}, simplify = FALSE)
code[1:10]
以上产生了以下结果,我不知道它们对应的是什么。是 sigma
还是 sub.sigma
。我想在每个副本 运行 的每一行中创建 4 个包含 sigma
、sub1.sigma
、sb2.sigma
和 sub3.sigma
值的数据帧。我怎样才能在 R 中实现它?我应该使用另一个循环函数吗?
> code[1:10]
[[1]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[2]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[3]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[4]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[5]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[6]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[7]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[8]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[9]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[10]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
因为您没有return从replicate
中获取任何东西,它是return从代码sub3.sigma
中获取最后一行。你可以return输出列表。
library(glmnet)
library(coefplot)
set.seed(2021)
code <- replicate(10,{
data<-matrix(rnorm(100*5,mean=0,sd=1), 100, 5)
colnames(data) <- c("X1", "X2", "X3", "X4", "X5")
data <- as.data.frame(data)
a <- 5
b <- 0.8
c <- 100
data[,2] <- a*data[,1] - b*rnorm(c)
data[,3] <- a*data[,1] + b*rnorm(c)
data[,4] <- a*data[,1] - b*rnorm(c)
A <- as.matrix(data)
set.seed(1)
results <- lapply(seq_len(ncol(A)), function(i) {
list(
cvfit = cv.glmnet(A[, -i] , A[, i] , standardize = TRUE , type.measure = "mse" , nfolds = 10 , alpha = 1)
)
})
lam <- as.data.frame(`names<-`(
lapply(results, function(x) (x$cvfit$lambda.min)),
paste0("X", seq_along(results))
))
sigma<- matrix(rnorm(1*5,mean=0,sd=1), 1, 5)
colnames(sigma) <- c("X1", "X2", "X3", "X4", "X5")
sub1.sigma <- subset(sigma, select = sigma <= sum(lam))
sub2.sigma <- subset(sigma, select = sigma <= 2*sum(lam))
sub3.sigma <- subset(sigma, select = sigma <= 3*sum(lam))
dplyr::lst(sigma, sub1.sigma, sub2.sigma, sub3.sigma)
}, simplify = FALSE)
要在它自己的列表中合并数据帧,您可以使用:
result <- lapply(purrr::transpose(code), function(x) do.call(rbind, x))
我在 R 中有一个代码,它最终生成一个名为 sigma
的向量和 3 个子集,即 sub1.sigma
、sb2.sigma
、sub3.sigma
.我想将此过程复制 n 次,比方说 10 次,然后观察上述向量上的值。我按如下方式使用复制功能
set.seed(2021)
code <- replicate(10,{
data<-matrix(rnorm(100*5,mean=0,sd=1), 100, 5)
colnames(data) <- c("X1", "X2", "X3", "X4", "X5")
data <- as.data.frame(data)
a <- 5
b <- 0.8
c <- 100
data[,2] <- a*data[,1] - b*rnorm(c)
data[,3] <- a*data[,1] + b*rnorm(c)
data[,4] <- a*data[,1] - b*rnorm(c)
library(glmnet)
library(coefplot)
A <- as.matrix(data)
set.seed(1)
results <- lapply(seq_len(ncol(A)), function(i) {
list(
cvfit = cv.glmnet(A[, -i] , A[, i] , standardize = TRUE , type.measure = "mse" , nfolds = 10 , alpha = 1)
)
})
lam <- as.data.frame(`names<-`(
lapply(results, function(x) (x$cvfit$lambda.min)),
paste0("X", seq_along(results))
))
sigma<- matrix(rnorm(1*5,mean=0,sd=1), 1, 5)
colnames(sigma) <- c("X1", "X2", "X3", "X4", "X5")
as.vector(sigma)
sub1.sigma <- subset(sigma, select = sigma <= sum(lam))
sub2.sigma <- subset(sigma, select = sigma <= 2*sum(lam))
sub3.sigma <- subset(sigma, select = sigma <= 3*sum(lam))
}, simplify = FALSE)
code[1:10]
以上产生了以下结果,我不知道它们对应的是什么。是 sigma
还是 sub.sigma
。我想在每个副本 运行 的每一行中创建 4 个包含 sigma
、sub1.sigma
、sb2.sigma
和 sub3.sigma
值的数据帧。我怎样才能在 R 中实现它?我应该使用另一个循环函数吗?
> code[1:10]
[[1]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[2]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[3]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[4]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[5]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[6]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[7]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[8]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[9]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
[[10]]
X1 X4 X5
[1,] 0.8032832 0.6772685 0.3799627
因为您没有return从replicate
中获取任何东西,它是return从代码sub3.sigma
中获取最后一行。你可以return输出列表。
library(glmnet)
library(coefplot)
set.seed(2021)
code <- replicate(10,{
data<-matrix(rnorm(100*5,mean=0,sd=1), 100, 5)
colnames(data) <- c("X1", "X2", "X3", "X4", "X5")
data <- as.data.frame(data)
a <- 5
b <- 0.8
c <- 100
data[,2] <- a*data[,1] - b*rnorm(c)
data[,3] <- a*data[,1] + b*rnorm(c)
data[,4] <- a*data[,1] - b*rnorm(c)
A <- as.matrix(data)
set.seed(1)
results <- lapply(seq_len(ncol(A)), function(i) {
list(
cvfit = cv.glmnet(A[, -i] , A[, i] , standardize = TRUE , type.measure = "mse" , nfolds = 10 , alpha = 1)
)
})
lam <- as.data.frame(`names<-`(
lapply(results, function(x) (x$cvfit$lambda.min)),
paste0("X", seq_along(results))
))
sigma<- matrix(rnorm(1*5,mean=0,sd=1), 1, 5)
colnames(sigma) <- c("X1", "X2", "X3", "X4", "X5")
sub1.sigma <- subset(sigma, select = sigma <= sum(lam))
sub2.sigma <- subset(sigma, select = sigma <= 2*sum(lam))
sub3.sigma <- subset(sigma, select = sigma <= 3*sum(lam))
dplyr::lst(sigma, sub1.sigma, sub2.sigma, sub3.sigma)
}, simplify = FALSE)
要在它自己的列表中合并数据帧,您可以使用:
result <- lapply(purrr::transpose(code), function(x) do.call(rbind, x))