tagteam / pec

Prediction error curves for censored event history analysis
0 stars 0 forks source link

selectFGR error on categorical predictor #1

Closed sahirbhatnagar closed 5 years ago

sahirbhatnagar commented 5 years ago

When one of the variables is a factor, selectFGR returns an error. Here is a reproducible example:

# example shown in selectFGR help
library(riskRegression)
#> Loading required package: data.table
#> Loading required package: ggplot2
#> Loading required package: prodlim
#> riskRegression version 2019.01.29
library(prodlim)
library(lava)
#> 
#> Attaching package: 'lava'
#> The following object is masked from 'package:ggplot2':
#> 
#>     vars
library(cmprsk)
#> Loading required package: survival
library(pec)
#> 
#> Attaching package: 'pec'
#> The following objects are masked from 'package:riskRegression':
#> 
#>     ipcw, selectCox
m <- crModel()
m <- addvar(m,c('X1','X2','X3','X4','X5','X6','X7','X8','X9','X10'))
distribution(m,c("X2","X7","X9")) <- binomial.lvm()
regression(m,eventtime1~X1+X2+X5+X9) <- c(-1,1,0.5,0.8)
set.seed(100)
d <- sim(m,100)

# make X2 a factor
d$X2 <- factor(d$X2)

## full formula
ff <- Hist(time, event) ~ X1 + X2 + X3 + X4 +X5 + X6 + X7+ X8 + X9 + X10

# Fit full model with FGR
fg <- FGR(ff,cause=1,data=d)

# Backward selection based on the AIC
sfgAIC <- selectFGR(ff, data=d, rule="AIC", direction="backward") 
#> Loading required namespace: crrstep
#> Error in eval(inp, data, env): object 'X21' not found

Created on 2019-04-16 by the reprex package (v0.2.1)

sahirbhatnagar commented 5 years ago

I think the error comes from:

newform <- as.formula(update(formula, paste(".~", paste(rownames(crrstep.fit$coefficients), 
            collapse = "+"))), env = NULL)

because rownames(crrstep.fit$coefficients) returns coefficients for each level of the categorical predictor which wont be found in the data. So maybe I should be sending model.matrix to the data argument ?

tagteam commented 5 years ago

yes, the best solution is that you prepare the data using model.matrix. I am not very motivated to fix this function myself but will write to the author of the function ...