CMAverse icon indicating copy to clipboard operation
CMAverse copied to clipboard

variable 'openness' was fitted with type "numeric" but type "logical" was supplied

Open minahbhabib opened this issue 1 year ago • 0 comments

Hello, I have been running into this weird issue where the error I am receiving does not seem to make sense. The mediator is class numeric-- it is not in any way a logic; however, when running the below code, I get the error that it is fitted with type numeric but type logic was supplied: ` mediation <- function(dataset, x_var, m_var, y_var, m_val, .basec, .postc) {

m_formula <-
  as.formula(paste(m_var, "~", paste(c(
    x_var, .basec, .postc
  ), collapse = "+")))

y_formula <-
  as.formula(paste(y_var, "~", paste(c(
    m_var, x_var, .basec, .postc
  ), collapse = "+")))

postc_formula_married <-
  as.formula(paste(.postc[1], "~", paste(c(
     x_var, .basec
  ), collapse = "+")))
postc_formula_ed_cont <-
  as.formula(paste(.postc[2], "~", paste(c(
    x_var, .basec
  ), collapse = "+")))

postc_formula_obese <-
  as.formula(paste(.postc[3], "~", paste(c(
    x_var, .basec#, c(.postc[1], .postc[2])
  ), collapse = "+")))

postc_formula_cesd <-
  as.formula(paste(.postc[4], "~", paste(c(
    x_var, .basec#, c(.postc[1], .postc[2])
  ), collapse = "+")))

postc_formula_smoke <-
  as.formula(paste(.postc[5], "~", paste(c(
    x_var, .basec#, c(.postc[1], .postc[2])
  ), collapse = "+")))

postc_formula_conde <-
  as.formula(paste(.postc[6], "~", paste(c(
    x_var, .basec#, c(.postc[1], .postc[2])
  ), collapse = "+")))


est <- cmest(
  data = dataset ,
  model = "gformula",
  outcome = y_var,
  exposure = x_var,
  mediator = m_var,
  basec = .basec,
  postc = .postc,
  postcreg = list(
    eval(substitute(
      glm(
        formula = postc_formula_married,
        family = binomial(link = "logit"),
        data = dataset
      )
    )),
    
    eval(substitute(
      glm(
        formula = postc_formula_ed_cont,
        family = gaussian(),
        data = dataset
      )
    )),
    
    eval(substitute(
      glm(
        formula = postc_formula_obese,
        family = binomial(link = "logit"),
        data = dataset
      ))),
    
    eval(substitute(
      glm(
        formula = postc_formula_cesd,
        family = poisson(link = "log"),
        data = dataset
      ))),
    
    eval(substitute(
      glm(
        formula = postc_formula_smoke,
        family = binomial(link = "logit"),
        data = dataset
      ))),
    
    eval(substitute(
      glm(
        formula = postc_formula_conde,
        family = poisson(link = "log"),
        data = dataset
      )))
    
  ),
  EMint = T,
  
  mreg = list(eval(substitute(
    glm(
      formula = m_formula,
      family = gaussian(link = "identity"),
      # weights = ipcw,
      data = dataset
    )
  ))),
  
  yreg = eval(substitute(
    glm(
      formula = y_formula,
      family = gaussian(link = "identity"),
      # weights = ipcw,
      data = dataset
    )
  )),
  # multimp = TRUE,
  mval = list(m_val),
  estimation = "imputation",
  # added back 1/11/24
  inference = "bootstrap",
  # boot.ci.type = "per",
  nboot = 500
)

res <- summary(est)
mediation_results <- res$summarydf %>%
  round(3) %>%
  as.data.frame() #%>% filter(
# row.names(.) %in% c("Rcde",
#                     "Rtnde",
#                     "Rtnie",
#                     "Rte",
#                     "pm",
#                     "int",
#                     "pe"))

return(mediation_results)

}

openness_temp <- mediation( dataset = temp_analysis_data, x_var = "ace_index_01", m_var = "openness", y_var = "telomere_length", m_val = 0, .basec = c("age_cont_2008", "age_squared_2008", "male", "race_eth_3cat", "moth_ed_cont" ), .postc = c( "married_2008", "ed_cont", "obese_2008", "cesd_score_2008", "smoke_2cat_2008", "conde_2008" ) ) `

minahbhabib avatar Feb 10 '24 22:02 minahbhabib