Running examples in ‘medflex-Ex.R’ failed
The error most likely occurred in:

> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: neImpute.default
> ### Title: Expand the dataset and impute nested counterfactual outcomes
> ### Aliases: neImpute.default
> 
> ### ** Examples
> 
> data(UPBdata)
> 
> ## example using glm imputation model with binary exposure
> fit.glm <- glm(UPB ~ factor(attbin) + negaff + gender + educ + age, 
+                family = binomial, data = UPBdata)
> impData <- neImpute(fit.glm)
> head(impData)
  id attbin0 attbin1        att attcat    negaff initiator gender educ age
1  1       1       1  1.0005617      M  0.840461    myself      F    M  41
2  1       0       1  1.0005617      M  0.840461    myself      F    M  41
3  2       0       0 -0.7085889      L -1.257465      both      M    M  42
4  2       1       0 -0.7085889      L -1.257465      both      M    M  42
5  3       0       0 -0.7085889      L -1.202256      both      F    H  43
6  3       1       0 -0.7085889      L -1.202256      both      F    H  43
        UPB
1 0.4916179
2 0.3841749
3 0.1870645
4 0.2629165
5 0.1570628
6 0.2241032
> 
> ## example using glm imputation model with continuous exposure
> fit.glm <- glm(UPB ~ att + negaff + gender + educ + age, 
+                family = binomial, data = UPBdata)
> impData <- neImpute(fit.glm, nRep = 2)
> head(impData)
  id      att0       att1 attbin attcat    negaff initiator gender educ age
1  1 -1.644093  1.0005617      1      M  0.840461    myself      F    M  41
2  1  1.644109  1.0005617      1      M  0.840461    myself      F    M  41
3  2 -1.662279 -0.7085889      0      L -1.257465      both      M    M  42
4  2  1.625923 -0.7085889      0      L -1.257465      both      M    M  42
5  3 -1.568986 -0.7085889      0      L -1.202256      both      F    H  43
6  3  1.719217 -0.7085889      0      L -1.202256      both      F    H  43
        UPB
1 0.3091871
2 0.5573637
3 0.1490191
4 0.3300578
5 0.1275219
6 0.2913869
> 
> ## example using vglm (yielding identical results as with glm)
> library(VGAM)
Loading required package: stats4
Loading required package: splines
> fit.vglm <- vglm(UPB ~ att + negaff + gender + educ + age, 
+                  family = binomialff, data = UPBdata)
> impData2 <- neImpute(fit.vglm, nRep = 2)
> head(impData2)
  id      att0       att1 attbin attcat    negaff initiator gender educ age
1  1 -1.644093  1.0005617      1      M  0.840461    myself      F    M  41
2  1  1.644109  1.0005617      1      M  0.840461    myself      F    M  41
3  2 -1.662279 -0.7085889      0      L -1.257465      both      M    M  42
4  2  1.625923 -0.7085889      0      L -1.257465      both      M    M  42
5  3 -1.568986 -0.7085889      0      L -1.202256      both      F    H  43
6  3  1.719217 -0.7085889      0      L -1.202256      both      F    H  43
        UPB
1 0.3091871
2 0.5573637
3 0.1490191
4 0.3300578
5 0.1275219
6 0.2913869
> 
> ## Not run: 
> ##D ## example using SuperLearner
> ##D library(Matrix)
> ##D library(SuperLearner)
> ##D SL.library <- c("SL.glm", "SL.glm.interaction", "SL.rpart",
> ##D                 "SL.step", "SL.stepAIC", "SL.step.interaction",
> ##D                 "SL.bayesglm", "SL.glmnet")
> ##D pred <- c("att", "negaff", "gender", "educ", "age")
> ##D fit.SL <- SuperLearner(Y = UPBdata$UPB, X = subset(UPBdata, select = pred),
> ##D                        SL.library = SL.library, family = binomial())
> ##D impSL <- neImpute(fit.SL, 
> ##D                   formula = UPB ~ att + negaff + gender + educ + age, 
> ##D                   data = UPBdata)
> ##D head(impSL)
> ## End(Not run)
> 
> ## Don't show: 
> rm(list=ls())
> library(VGAM) 
> expData <- neImpute(UPB ~ factor(attbin) + negaff + gender + educ + age, family = binomialff, data = UPBdata, FUN = vglm)
> neMod <- neModel(UPB ~ attbin0 + attbin1 + gender + educ + age, family = binomial, expData = expData, nBoot = 2)

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |======================================================================| 100%> 
> UPBdata$att2 <- ifelse(UPBdata$attbin == "H", 1, 0)
> UPBdata$att2 <- UPBdata$attbin
> impData <- neImpute(UPB ~ factor(att2) * negaff + gender + educ + age, family = binomial, data = UPBdata)
> impFit1 <- neModel(UPB ~ att20 * att21 + gender + educ + age, family = binomial, expData = impData, nBoot = 2)

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |======================================================================| 100%> impFit2 <- neModel(UPB ~ factor(att20) * factor(att21) + gender + educ + age, family = binomial, expData = impData, nBoot = 2)

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |======================================================================| 100%> summary(impFit1)
Natural effect model
with standard errors based on the non-parametric bootstrap
---
Exposure: att2 
Mediator(s): negaff 
---
Parameter estimates:
               Estimate Std. Error z value Pr(>|z|)    
(Intercept)   -0.882155   0.549903  -1.604 0.108670    
att201         0.379757   0.113043   3.359 0.000781 ***
att211         0.305384   0.007506  40.684  < 2e-16 ***
genderM        0.292674   0.119840   2.442 0.014598 *  
educM          0.313630   0.838459   0.374 0.708363    
educH          0.482386   0.952346   0.507 0.612489    
age           -0.012471   0.004123  -3.025 0.002490 ** 
att201:att211  0.057225   0.131515   0.435 0.663474    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> summary(impFit2)
Natural effect model
with standard errors based on the non-parametric bootstrap
---
Exposure: att2 
Mediator(s): negaff 
---
Parameter estimates:
                               Estimate Std. Error z value Pr(>|z|)    
(Intercept)                   -0.882155   0.874287  -1.009  0.31297    
factor(att20)1                 0.379757   0.058807   6.458 1.06e-10 ***
factor(att21)1                 0.305384   0.060398   5.056 4.28e-07 ***
genderM                        0.292674   0.107263   2.729  0.00636 ** 
educM                          0.313630   0.472063   0.664  0.50645    
educH                          0.482386   0.679999   0.709  0.47808    
age                           -0.012471   0.006159  -2.025  0.04286 *  
factor(att20)1:factor(att21)1  0.057225   0.067275   0.851  0.39498    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> 
> head(neImpute(UPB ~ att + negaff + gender + educ + age, data = UPBdata, weights = rep(1, nrow(UPBdata))))
  id          att0       att1 attbin attcat    negaff initiator gender educ age
1  1 -1.644093e+00  1.0005617      1      M  0.840461    myself      F    M  41
2  1 -5.974787e-01  1.0005617      1      M  0.840461    myself      F    M  41
3  1  8.023932e-06  1.0005617      1      M  0.840461    myself      F    M  41
4  1  5.974948e-01  1.0005617      1      M  0.840461    myself      F    M  41
5  1  1.644109e+00  1.0005617      1      M  0.840461    myself      F    M  41
6  2 -1.662279e+00 -0.7085889      0      L -1.257465      both      M    M  42
        UPB att2
1 0.3327717    1
2 0.4011284    1
3 0.4401516    1
4 0.4791748    1
5 0.5475316    1
6 0.1231013    0
> fit1 <- vglm(UPB ~ att + negaff + gender + educ + age, family = binomialff, data = UPBdata)
> head(neImpute(fit1))
  id          att0       att1 attbin attcat    negaff initiator gender educ age
1  1 -1.644093e+00  1.0005617      1      M  0.840461    myself      F    M  41
2  1 -5.974787e-01  1.0005617      1      M  0.840461    myself      F    M  41
3  1  8.023932e-06  1.0005617      1      M  0.840461    myself      F    M  41
4  1  5.974948e-01  1.0005617      1      M  0.840461    myself      F    M  41
5  1  1.644109e+00  1.0005617      1      M  0.840461    myself      F    M  41
6  2 -1.662279e+00 -0.7085889      0      L -1.257465      both      M    M  42
        UPB att2
1 0.3091871    1
2 0.3835087    1
3 0.4288055    1
4 0.4753273    1
5 0.5573637    1
6 0.1490191    0
> head(neImpute(UPB ~ att + negaff + gender + educ + age, family = binomialff, data = UPBdata, FUN = vglm, weights = rep(1, nrow(UPBdata))))
  id          att0       att1 attbin attcat    negaff initiator gender educ age
1  1 -1.644093e+00  1.0005617      1      M  0.840461    myself      F    M  41
2  1 -5.974787e-01  1.0005617      1      M  0.840461    myself      F    M  41
3  1  8.023932e-06  1.0005617      1      M  0.840461    myself      F    M  41
4  1  5.974948e-01  1.0005617      1      M  0.840461    myself      F    M  41
5  1  1.644109e+00  1.0005617      1      M  0.840461    myself      F    M  41
6  2 -1.662279e+00 -0.7085889      0      L -1.257465      both      M    M  42
        UPB att2
1 0.3091871    1
2 0.3835087    1
3 0.4288055    1
4 0.4753273    1
5 0.5573637    1
6 0.1490191    0
> head(neImpute(UPB ~ att + negaff + gender + educ + age, family = binomial, data = UPBdata, weights = rep(1, nrow(UPBdata))))
  id          att0       att1 attbin attcat    negaff initiator gender educ age
1  1 -1.644093e+00  1.0005617      1      M  0.840461    myself      F    M  41
2  1 -5.974787e-01  1.0005617      1      M  0.840461    myself      F    M  41
3  1  8.023932e-06  1.0005617      1      M  0.840461    myself      F    M  41
4  1  5.974948e-01  1.0005617      1      M  0.840461    myself      F    M  41
5  1  1.644109e+00  1.0005617      1      M  0.840461    myself      F    M  41
6  2 -1.662279e+00 -0.7085889      0      L -1.257465      both      M    M  42
        UPB att2
1 0.3091871    1
2 0.3835087    1
3 0.4288055    1
4 0.4753273    1
5 0.5573637    1
6 0.1490191    0
> UPBdata$att <- factor(cut(UPBdata$att, 2), labels = c("low", "high"))
> fit2 <- glm(UPB ~ att * negaff * gender * educ * age, family = binomial, data = UPBdata)
> # head(neImpute(fit2, nMed = 3, joint = FALSE))
> # head(neImpute(UPB ~ att * negaff * gender * educ * age, data = UPBdata, family = binomial, nMed = 3, joint = FALSE))
> head(neImpute(UPB ~ att + negaff + gender + educ + age, data = UPBdata, family = binomial))
  id att0 att1 attbin attcat    negaff initiator gender educ age       UPB att2
1  1 high high      1      M  0.840461    myself      F    M  41 0.5106796    1
2  1  low high      1      M  0.840461    myself      F    M  41 0.4059912    1
3  2  low  low      0      L -1.257465      both      M    M  42 0.1999050    0
4  2 high  low      0      L -1.257465      both      M    M  42 0.2761580    0
5  3  low  low      0      L -1.202256      both      F    H  43 0.1668007    0
6  3 high  low      0      L -1.202256      both      F    H  43 0.2341212    0
> head(neImpute(UPB ~ att + negaff + gender + educ + age, data = UPBdata))
  id att0 att1 attbin attcat    negaff initiator gender educ age       UPB att2
1  1 high high      1      M  0.840461    myself      F    M  41 0.5104756    1
2  1  low high      1      M  0.840461    myself      F    M  41 0.4175505    1
3  2  low  low      0      L -1.257465      both      M    M  42 0.1928804    0
4  2 high  low      0      L -1.257465      both      M    M  42 0.2858055    0
5  3  low  low      0      L -1.202256      both      F    H  43 0.1528799    0
6  3 high  low      0      L -1.202256      both      F    H  43 0.2458050    0
> 
> # # test with vglm!
> library(VGAM)
> impFit <- vglm(UPB ~ att + negaff + gender + educ + age, family = binomialff, data = UPBdata)
> terms(impFit)
UPB ~ att + negaff + gender + educ + age
attr(,"variables")
list(UPB, att, negaff, gender, educ, age)
attr(,"factors")
       att negaff gender educ age
UPB      0      0      0    0   0
att      1      0      0    0   0
negaff   0      1      0    0   0
gender   0      0      1    0   0
educ     0      0      0    1   0
age      0      0      0    0   1
attr(,"term.labels")
[1] "att"    "negaff" "gender" "educ"   "age"   
attr(,"order")
[1] 1 1 1 1 1
attr(,"intercept")
[1] 1
attr(,"response")
[1] 1
attr(,".Environment")
<environment: R_GlobalEnv>
attr(,"predvars")
list(UPB, att, negaff, gender, educ, age)
attr(,"dataClasses")
      UPB       att    negaff    gender      educ       age 
"numeric"  "factor" "numeric"  "factor"  "factor" "numeric" 
> # debug(neImpute)
> impData <- neImpute(impFit)
> impFit2 <- glm(UPB ~ att + negaff + gender + educ + age, family = binomial, data = UPBdata)
> impData2 <- neImpute(impFit2)
> head(impData); head(impData2)
  id att0 att1 attbin attcat    negaff initiator gender educ age       UPB att2
1  1 high high      1      M  0.840461    myself      F    M  41 0.5106796    1
2  1  low high      1      M  0.840461    myself      F    M  41 0.4059912    1
3  2  low  low      0      L -1.257465      both      M    M  42 0.1999050    0
4  2 high  low      0      L -1.257465      both      M    M  42 0.2761580    0
5  3  low  low      0      L -1.202256      both      F    H  43 0.1668007    0
6  3 high  low      0      L -1.202256      both      F    H  43 0.2341212    0
  id att0 att1 attbin attcat    negaff initiator gender educ age       UPB att2
1  1 high high      1      M  0.840461    myself      F    M  41 0.5106796    1
2  1  low high      1      M  0.840461    myself      F    M  41 0.4059912    1
3  2  low  low      0      L -1.257465      both      M    M  42 0.1999050    0
4  2 high  low      0      L -1.257465      both      M    M  42 0.2761580    0
5  3  low  low      0      L -1.202256      both      F    H  43 0.1668007    0
6  3 high  low      0      L -1.202256      both      F    H  43 0.2341212    0
> # check!!
> 
> # test with gam?
> library(gam)
Error in library(gam) : there is no package called ‘gam’
Execution halted