我在做数据模拟的时候也遇到了这个问题,下面是我的程序,当我把变量个数设为5000时,程序就会报告:错误,无法分配大小为95.4M的矢量。这是怎么回事,我用了rm(),gc(),还是不管用啊,像这样应该怎么解决呢
rm(list=ls())
library(MASS)
library(stats)
library(utils)
library(graphics)
library(splines)
library(survival)
library(penalized)
library(MASS)
library(SIS)
library(lattice)
library(Matrix)
gendata <- function(n,cor,beta)
{
#design matrix
X=mvrnorm(n,rep(0,p),cor)
#observed survivial time
#set.seed(seed+1000)
logS=log(matrix(runif(n,0,1),n,1)) #log[S(t)]
T=-logS/(exp(X%*%beta))
#censored time
C <- rexp(n, rate =0.1)
#survival time and state
time <- apply(cbind(C,T),1,min)
stat <- as.numeric(T<C)
#combin data
return(list("X"=X,"surv"=Surv(time,stat),"time"=time,"stat"=stat))
}
n=100 #sample size
sample=2 #simulation times
#beta vector
beta1<-c(1.8762,-2.2746,1.9497,-1.9826,1.956277,-2.2077,1.9929,-2.4966,1.8992,-1.9458)
beta2<-rep(0,4990)
beta<-matrix(c(beta1,beta2))
beta11<-which(beta!=0)
p <- length(beta)
d <- p-length(beta[beta==0])
#generate correlation matrix ,第一种方案,相关系数为零,即个自变量间相互独立
cor=matrix(1,p,p)
for (i in 1:p)
{
for (j in 1:p)
{
if(i!=j)
{
cor[i,j]=0 #correlation matrix
}
}
}
i=1
for (i in 1:sample){
if (i>1 ){rm(simcox,opt1pf,lambda,coxlasso,opt1,fit2,lambda22,fit1,lambda11,pen)
gc()}#删除上一次循环中的结果,避免影响下一次
###############generate data
simcox <-gendata(n,cor,beta)
attach(simcox)
#Cox: lasso
opt1pf <- profL1(surv, penalized = X, fold = 10, data=simcox)
lambda <- opt1pf
lambda[opt1pfcvl==max(opt1pf$cvl)]
coxlasso <- penalized(surv, penalized = X, data=simcox, lambda1=lambda)
}
暂无数据