jmpsteen / medflex

Flexible mediation analysis using natural effect models in R
20 stars 6 forks source link

Cannot find expData #17

Closed bernard-liew closed 3 years ago

bernard-liew commented 4 years ago

Dear team,

many thanks for your lovely package. I hope everyone is safe too currently.

I am trying to run some analysis on my data but ran into some errors. Here is a sample of my data with the codes and errors. I would appreciate any help.

library (medflex)

samp.df <- structure(list(
  osw_late = c(-6, -6, 4, 0, 0, 4, 6, -19.12, -2, 
-18, NA, 4, 8, 20, -8, 8, NA, 8, -14, 0, 4, 2, 2, -12, -20, 4, 
-8, -20, 10, 12, -8, -4, -6, -10, -8, -2, 0, 4, -4, -2, 4, -22, 
-6, 2, -13.1111111111111, 0, 2, 10, -4, -6), grp = structure(c(2L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 
1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 
2L), .Label = c("advice", "individualisedphysio"), class = "factor"), 
pain_cope_success_early = c(NA, -10, -3, 0, -2, -1, -1, 2, 
-8, NA, 1, 0, -3, 0, 3, NA, -2, -5, -5, 5, -1, -5, -2, 1, 
NA, -5, 2, 0, -2, 2, -3, -4, -2, 3, 1, -2, -2, 5, -1, -1, 
0, 0, 0, -5, 0, -3.5, 5, 1, -2, -1), lbp_early = c(-4, -5, 
-1, 0, -6, -4.5, -4, -3, -7, -0.5, -2, 1, -3, 1, 0, -8, -2, 
1, 0, -4, -3, -3, -4, -3, -2, -3, 3, 1, -4, -3, 0, 0, -6, 
0, -3, -1, -3, -3, -1, -2, -6, 0, 3, -3, 0, -4, 1, -5, -1, 
-2)), row.names = c(148L, 63L, 119L, 208L, 121L, 215L, 286L, 
152L, 146L, 166L, 139L, 129L, 10L, 130L, 203L, 143L, 254L, 19L, 
3L, 120L, 242L, 285L, 11L, 94L, 248L, 149L, 134L, 272L, 209L, 
165L, 91L, 33L, 32L, 71L, 261L, 238L, 158L, 247L, 221L, 251L, 
187L, 112L, 232L, 161L, 122L, 227L, 1L, 105L, 125L, 13L), class = "data.frame")

########## Issue 1 ############################################

# Error in nrow(expData) : object 'expData' not found

form <- formula ("osw_late ~ grp + pain_cope_success_early +  lbp_early")
form2 <- formula("osw_late ~ grp0 + grp1 + lbp_early")

out.mod <- lm (form,  data = samp.df)
impData <- neImpute(out.mod , nMed = 1)
ne.mod <- neModel(form2 , expData = impData,  se = "robust")

########## Issue 2 ############################################

# Error in vector(length = nrow(fit2$data)) : invalid 'length' argument

out.mod <- lm (osw_late ~ grp + pain_cope_success_early +  lbp_early, data = samp.df)
impData <- neImpute(out.mod , nMed = 1)
ne.mod <- neModel(osw_late ~ grp0 + grp1 + lbp_early , expData = impData,  se = "robust")

###############
sessionInfo()

R version 3.6.2 (2019-12-12) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale: [1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] medflex_0.6-6 multcomp_1.4-11 TH.data_1.0-10 MASS_7.3-51.5 survival_3.1-8 mvtnorm_1.0-11

loaded via a namespace (and not attached): [1] zip_2.0.4 Rcpp_1.0.3 cellranger_1.1.0 compiler_3.6.2 pillar_1.4.3 forcats_0.4.0
[7] tools_3.6.2 zeallot_0.1.0 packrat_0.5.0 tibble_2.1.3 lattice_0.20-38 pkgconfig_2.0.3
[13] rlang_0.4.4 openxlsx_4.1.4 Matrix_1.2-18 rstudioapi_0.11 curl_4.3 haven_2.2.0
[19] rio_0.5.16 vctrs_0.2.1 hms_0.5.2 grid_3.6.2 data.table_1.12.8 readxl_1.3.1
[25] foreign_0.8-72 carData_3.0-3 car_3.0-6 magrittr_1.5 backports_1.1.5 codetools_0.2-16 [31] splines_3.6.2 abind_1.4-5 sandwich_2.5-1 stringi_1.4.3 crayon_1.3.4 zoo_1.8-6

Regards, Bernard

bernard-liew commented 4 years ago

Apologies,

I realised for error 2 it is because there is missing data in my dataframe. I assume one needs to remove it prior.

For error 1, i assume one cannot paste a formula string directly into the neModel call?

Regards, Bernard

jmpsteen commented 3 years ago

Thanks for your interest in medflex! Your answers are correct. The code below works, using complete cases.

samp.df <- samp.df[complete.cases(samp.df), ]
out.mod <- glm(osw_late ~ grp + pain_cope_success_early +  lbp_early, data = samp.df)
impData <- neImpute(out.mod, nMed = 1)
ne.mod <- neModel(osw_late ~ grp0 + grp1 + lbp_early , expData = impData,  se = "robust")

If you wish to run the analysis accounting for the missing data, you can use the functionality of the mice and mitools packages, as explained in the package vignette (section 9.2).

Also note that to obtain robust standard errors, one must use glm instead of lm for fitting the imputation model.

Please close this issue if you agree it has been solved.