AnderWilson / bdlim

Code for fitting and interpreting Bayesian distributed lag interaction models (BDLIMs).
https://anderwilson.github.io/bdlim/
GNU General Public License v3.0
0 stars 1 forks source link

Debug bdlim1 #1

Closed hhp94 closed 3 weeks ago

hhp94 commented 3 weeks ago

Fix 2 bugs in in R/bdlim1.R:

1) if covars is a named matrix and exposure is a matrix (as suggested in the help file), then drop_na will fail because cbind will create a matrix, and drop_na doesn't have a matrix method. Also, when converted to a matrix, this will convert the factor group into a numeric variable, causing levels of group to be dropped, making names_groups <- levels(alldata$group) return NULL to names_group which would be incorrect.

REPREX:

# Convert covars to a matrix
named_Z <- as.matrix(sbd_bdlim[,c("MomPriorBMI")])
# Named matrix to skip the `if(is.null(colnames(covars)))` check
colnames(named_Z) <- "MomPriorBMI"

# X can be entered as a matrix as suggested in the docs
matrix_X <- as.matrix(sbd_bdlim[,paste0("pm25_",1:37)])

# Error because cbind(y,group,covars,exposure) returns a matrix and drop_na don't have a matrix method
fit_sex <- bdlim4(
  y = sbd_bdlim$bwgaz,
  exposure = matrix_X,
  covars = named_Z,
  group = as.factor(sbd_bdlim$ChildSex),
  df = 5,
  nits = 100,
  parallel = FALSE
)

2) Hardcoded numbers of times the exposure measured to be 37 in line 78: theta <- lm(rep(1/sqrt(37),37)~basis-1)$coef

REPREX:

# 37 is hardcoded
fit_sex <- bdlim4(
  y = sbd_bdlim$bwgaz,
  exposure = sbd_bdlim[,paste0("pm25_",1:36)],
  covars = sbd_bdlim[,c("MomPriorBMI","MomAge","race","Hispanic",
                                   "EstMonthConcept","EstYearConcept")],
  group = as.factor(sbd_bdlim$ChildSex),
  df = 5,
  nits = 100,
  parallel = FALSE
)
hhp94 commented 3 weeks ago

Dear Dr Ander Wilson,

Thank you so much for your research. I'm very excited to use the bdlm in my research :). I hope you can help me with this bug fix.

Sincerely

AnderWilson commented 3 weeks ago

Thanks