I'm trying to do a stepwise Cox regression imputing missing values.
As a first step I tried a simple stepwise procedure using the StepReg package but I ran into the following issue:
set.seed(123) library(tidyverse) lung_na <- survival::lung %>% mutate(sex = factor(sex, levels = c(1, 2))) %>% # make sex as factor na.omit() res6 <- StepReg::stepwise( formula = survival::Surv(time, status) ~ age + sex + ph.ecog + ph.karno + pat.karno + meal.cal + wt.loss, data = lung_na, type = "cox", strategy = "forward", metric = "AICc" ) #> Error in data.frame(Parameter = c("initial formula", "regression type", : arguments imply differing number of rows: 11, 12 res6 #> Error: object 'res6' not found Created on 2025-01-03 with reprex v2.1.1
I can't seem to understand why an error occurs using a complete formula and not using survival::Surv(time, status) ~ . as the formula inside the example.
I'm using the complete formula because the following step would be, using the mice package, this:
model_lung_imp <- with( data = lung_imp, StepReg::stepwise( formula = survival::Surv(time, status) ~ age + sex + ph.ecog + ph.karno + pat.karno + meal.cal + wt.loss, data = ???, type = "cox", strategy = "forward", metric = "AICc" ) ) which doesn't work with dot formulas. In this case it's also not particularly clear how to pass the data argument to the with() formula.
This is an example with a normal Cox regression:
set.seed(123) library(survival) library(mice) #> #> Attaching package: 'mice' #> The following object is masked from 'package:stats': #> #> filter #> The following objects are masked from 'package:base': #> #> cbind, rbind lung_imp <- survival::lung |> mice::mice(method = "cart", print = F) model_lung_imp <- with( data = lung_imp, survival::coxph(survival::Surv(time, status) ~ age + sex + ph.ecog + ph.karno + pat.karno + meal.cal + wt.loss) ) summary(mice::pool(model_lung_imp)) #> term estimate std.error statistic df p.value #> 1 age 1.166524e-02 0.0095162812 1.22581919 154.84482 0.2221277796 #> 2 sex -5.918487e-01 0.1717995858 -3.44499507 155.11338 0.0007347178 #> 3 ph.ecog 5.728645e-01 0.1870627320 3.06241937 145.77603 0.0026153061 #> 4 ph.karno 1.341023e-02 0.0094293986 1.42217219 154.85166 0.1569874005 #> 5 pat.karno -1.269711e-02 0.0070854108 -1.79200694 137.03091 0.0753384836 #> 6 meal.cal -1.991951e-05 0.0002478896 -0.08035637 66.20331 0.9361962526 #> 7 wt.loss -1.003045e-02 0.0066743084 -1.50284529 127.96469 0.1353428021 Created on 2025-01-03 with reprex v2.1.1
Any tips on how to accomplish this?
Thank you.
coxphdoes not commit that sin.survivalpackage. I've never heard of this "StepReg" package" and so I'm guessing it fairly new. The error message seems to be related to a data structure that is constructed in service of the stepwise procedures, so I'm thinking this may be better directed to the maintainer of that package. Easy to retrieve with themaintainerfunction. (Stepwise procedures are viewed with suspicion in the R statstical community.)#> Error in data.frame(Parameter = c("initial formula", "regression type", : arguments imply differing number of rows: 11, 12, I have fixed the bug, You can now reinstall the development version using the following command:install_github("JunhuiLi1017/StepReg"). Currently, StepReg does not support thewith()function, but this seems like a useful feature to consider for future updates.