daniel258 / GEEmediate

R package, development version. Mediation analysis in generalized linear nodels using the difference method
0 stars 1 forks source link

Currently does not work with binary exposures #5

Closed OddMusLeb closed 4 months ago

OddMusLeb commented 4 months ago

Hello,

I've been attempting to figure out how to perform mediation analysis using GEE models and stumbled upon your package. I'm currently working with a set of binary exposures and 1 continuous mediator variable that I am attempting to assess mediation between. The CRAN documentation states that this package is for both continuous AND binary exposures: https://cran.r-project.org/web/packages/GEEmediate/GEEmediate.pdf

However, when attempting to use a binary exposure I get the below error:

Error in if (pm < 0 | pm > 1) { : argument is on length zero

It took some digging in the source code but I think I've found the reason for this particular error. the relevant code chunk is shown below: nde <- fit$coefficients[names(fit$coefficients) == exposure] nie <- fit$coefficients[names(fit$coefficients) == paste0(exposure, ".star")] - nde te <- nde + nie pm <- nie/te cov.gee <- fit$robust.variance v <- cov.gee[dimnames(fit$robust.variance)[[1]] == exposure, dimnames(fit$robust.variance)[[2]] == exposure] v.star <- cov.gee[dimnames(fit$robust.variance)[[1]] == paste0(exposure, ".star"), dimnames(fit$robust.variance)[[2]] == paste0(exposure, ".star")] covar <- cov.gee[dimnames(fit$robust.variance)[[1]] == exposure, dimnames(fit$robust.variance)[[2]] == paste0(exposure, ".star")]

This works perfectly with continuous exposures, the exposure object is set by the user in the arguments of the function and will be equivilent to what is found in names(fit$coefficients). However, when the exposure is binary, it will not be equal to the variable name found in the gee output created from the fit object. because the exposure is binary, the resulting name of the variable found in the gee output will include the non-reference category. (i.e. exposure --> exposure1). This however is not the same as the name of the variable set in the exposure argument of the function. the same issue arises for the .star variable. the resulting output involving a binary variable will show that the name is exposure.star1 not exposure.star. I'm not sure if whether using the exact name of what the gee output will provide, in the exposure argument of the function, suffices either, as then it will be exposure1.star instead of exposure.star1.

The reason why this naming selection is an issue is that all these objects will turn out as empty/null objects. as there was never a variable with the name set in exposure to begin with. as a result when we get to the point of calculating p values and CIs in the if statement at the end of the source code of the function, we will get the error i mentioned above as there is nothing to actually work with when attempting to calculate those.

Am I missing something here?

daniel258 commented 4 months ago

Hi, thanks for reaching out! I was a bit surprised because both myself and others did use binary exposures in the past. Is it possible the problem is that the exposure is kept as a string or factor? I wonder if the following simple fix will work: change the exposure variable coding to 0/1. If that indeed solves the problem I'll add a comment to the documentation. Daniel

OddMusLeb commented 4 months ago

Hello Daniel,

My apologies. I was setting the exposure to be a factor prior to using your package. Setting it back definitely resolves the issue. My apologies for assuming my mistake was some bug in your code. There unfortunately isnt a lot of info regarding this specific package out there so I am not sure what the appropriate protocol is for its use. Thanks for the quick response and assistance!

Burjak

daniel258 commented 4 months ago

No worries Burjak, thanks for reaching out!