简体   繁体   English

R生存软件包中的岭回归错误

[英]Error with ridge regression in r survival package

I'm trying to use the survival package in R on a large dataset. 我正在尝试在大型数据集上的R中使用生存包。 When I try to enter lots of variables into the ridge function, I get an error. 当我尝试在ridge函数中输入很多变量时,出现错误。 Strangely enough, this depends on the lengths of the variable names, but even with very short variable names (ie X1..X200), I can't have more than about 100 variables entered until I get this strange error message: 奇怪的是,这取决于变量名的长度,但是即使使用非常短的变量名(例如X1..X200),在收到以下奇怪的错误消息之前,输入的变量也不能超过100个:

Error in if (any(ord > 1)) stop("Penalty terms cannot be in an interaction") : missing value where TRUE/FALSE needed if(any(ord> 1))stop(“惩罚项不能处于交互中”)错误:缺少需要TRUE / FALSE的值

Here is an code example which will generate this error: 这是将生成此错误的代码示例:

library(survival)

# Create a test data frame with random data (200 predictors)
test.data <-data.frame(outcome=rbinom(1000,1,0.1),
                       time=runif(1000,0,1000),replicate(200,rnorm(1000)))

# Create a string with ridge regression formula for 100 predictors
ridge.formula.100 <- paste0("Surv(time,outcome) ~ ridge(",
                            paste(paste0("X",1:100),collapse=","),",theta=1)")

# Run ridge regression with 100 predictors
m1 <- coxph(as.formula(ridge.formula.100),data=test.data)
summary(m1) # Yay it works!

# Create a string with ridge regression formula for 200 predictors
ridge.formula.120 <- paste0("Surv(time,outcome) ~ ridge(",
                            paste(paste0("X",1:120),collapse=","),",theta=1)")

# Run ridge regression with 120 predictors
m2 <- coxph(as.formula(ridge.formula.120),data=test.data) # Gives error
# Fails with error above

Any hints as to what I'm doing wrong? 关于我在做什么错的任何提示吗? Importantly, if the variable names are longer, even fewer variables can be entered into the ridge. 重要的是,如果变量名较长,则可以在脊中输入更少的变量。

Thanks much! 非常感谢!

Try putting all the variables into a matrix allvars <- as.matrix(test.data[,3:ncol(test.data)]) then use this in your formula ridge.formula <- as.formula(paste("Surv(time,outcome) ~ ridge(allvars,theta=1)")) . 尝试将所有变量放入矩阵中allvars <- as.matrix(test.data[,3:ncol(test.data)])然后在公式ridge.formula <- as.formula(paste("Surv(time,outcome) ~ ridge(allvars,theta=1)")) Now the call m2 = coxph(ridge.formula,data=test.data) does not give that error. 现在,调用m2 = coxph(ridge.formula,data=test.data)不会产生该错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM