我试图使用rms::lrm函数来拟合逻辑回归模型,其中我将rcs函数应用于一个变量,并使用数据集中的其余变量。
例如,我想做以下工作:
fit = rms::lrm(y~ rcs(a) + ., data=myData, x=TRUE, y=TRUE)但是,这将返回以下错误:
错误的if (!length(fname) \x !any(fname==zname)) {:TRUE/FALSE需要的缺失值
我尝试了几种不同的排列公式,但没有其他任何错误。
以下示例工作正常,没有错误:
fit = rms::lrm(y~ rcs(a), data=myData, x=TRUE, y=TRUE)
fit = rms::lrm(y~ ., data=myData, x=TRUE, y=TRUE)是否有任何方便的方法将rcs()应用于特定的变量,但仍然使用“。操作符,还是仅仅因为我使用的是rcs(),就需要手动键入所有其他变量?
谢谢!
发布于 2018-04-26 17:43:18
它不使用“。”运算符,但我有时使用粘贴/折叠来生成要包含的变量列表。在你的例子中是这样的:
#get all the columns except for 'a'
linearvars <- setdiff(colnames(data),'a')
linearphrase <- paste(linearvars, collapse=" + ")
#combine the linear terms with the rest of the formula
fullformula <- as.formula( paste0('y~rcs(a) + ', linearphrase) )或者作为单行:
fullformula <- as.formula(paste0('y~rcs(a) + ', paste(setdiff(colnames(data), 'a'), collapse=" + ")))https://stackoverflow.com/questions/45555504
复制相似问题