joshuaschwab / ltmle

Longitudinal Targeted Maximum Likelihood Estimation package
http://joshuaschwab.github.io/ltmle/
23 stars 16 forks source link

Regimes as a list of functions sometimes causes error #11

Open lendle opened 10 years ago

lendle commented 10 years ago

I think it is when there is only one Anode, and the array created by concatenating results of the rule functions together gets a dimension dropped.

Not so minimal example:


rexpit <- function(x) rbinom(n=length(x), size=1, prob=plogis(x))
n <- 100
A1<- L1a<- L1b<- Y1<- C2 <- L2a<- L2b<- Y2<- rep(NA,n)
age <- rbinom(n, 1, 0.5)
gender <- rbinom(n, 1, 0.5)
A1 <- rexpit(age + gender)
L1a <- 2*age - 3*gender + 2*A1 + rnorm(n)
L1b <- rexpit(age + 1.5*gender - A1)
Y1 <- plogis(age - gender + L1a + 0.7*L1b + A1 + rnorm(n))
C2 <- BinaryToCensoring( is.uncensored=rbinom(n, 1,0.5)) ## added in random censorn
uncensored<- C2=='uncensored'
L2a[uncensored] <- ( 2*age - 3*gender + 2*A1 + rnorm(n))[uncensored]
L2b[uncensored] <- ( rexpit(age + 1.5*gender - A1 ) )[uncensored]
Y2[uncensored] <- (plogis(age - gender + L1a + L1b + A1  + rnorm(n)))[uncensored]
data <- data.frame(age, gender, A1, L1a, L1b, Y1, C2, L2a, L2b, Y2)

Lnodes=c("L1a", "L1b", "L2a", "L2b")
Anodes<- c("A1")
Cnodes<- c("C2")
Ynodes<- c("Y1", "Y2")

regimes<- list(function(row) 1, function(row) 0 ) 

summary.measures<- array(dim=c(2,2,2))
dimnames(summary.measures)[[2]]<- c("risk","time")
summary.measures[1, , 1]<- cbind(1, 1)
summary.measures[1, , 2]<- cbind(1, 2)
summary.measures[2, , 1]<- cbind(0, 1)
summary.measures[2, , 2]<- cbind(0, 2)

ltmleMSM(data, Anodes=Anodes, Lnodes=Lnodes, Ynodes=Ynodes, Cnodes=Cnodes, final.Ynodes = Ynodes,
        survivalOutcome=F,
        regimes=regimes, 
        summary.measures=summary.measures, 
        working.msm="Y ~ risk+ time", estimate.time=FALSE)

results in

Error in aperm.default(simplify2array(lapply(regimes, function(rule) apply(data,  : 
  'perm' is of wrong length 3 (!= 2)

Explicitly creating the regime array and using that works:

regimesarray <- array(dim=c(n, 1, 2))
regimesarray[,,1] <- 1
regimesarray[,,2] <- 0
ltmleMSM(data, Anodes=Anodes, Lnodes=Lnodes, Ynodes=Ynodes, Cnodes=Cnodes, final.Ynodes = Ynodes,
        survivalOutcome=F,
        regimes=regimesarray, 
        summary.measures=summary.measures, 
        working.msm="Y ~ risk+ time", estimate.time=FALSE)

I'll try to fix this (but not necessarily soon). We probably just need to check if there is only 1 Anode, and change how the rule functions construct the 3 dimensional array in that case.