nicholasjclark / MRFcov

Markov random fields with covariates
23 stars 5 forks source link

Indirect coefficients omitted when predicting networks with bootstrapped MRF models #33

Open lawwt opened 2 years ago

lawwt commented 2 years ago

predict_MRFnetworks.R has inconsistencies in handling indirect coefficients with bootstrapped MRF models, resulting in indirect coefficients omitted from the predicted networks when a bootstrapped model is used.

For example, within code chunk 119-133, line 126 checks if the length of MRF_mod$indirect_coefs is greater than 0

# Create an MRF_mod object to feed to the predict function
MRF_mod_booted <- list()
MRF_mod_booted$graph <- MRF_mod$direct_coef_means[ , 2:(n_nodes + 1)]
MRF_mod_booted$intercepts <- as.vector(MRF_mod$direct_coef_means[ , 1])
MRF_mod_booted$direct_coefs <- MRF_mod$direct_coef_means
MRF_mod_booted$mod_family <- MRF_mod$mod_family
MRF_mod_booted$mod_type <- 'MRFcov'
if(length(MRF_mod$indirect_coefs) > 0){
  for(i in seq_along(MRF_mod$indirect_coef_mean)){
    MRF_mod_booted$indirect_coefs[[i]] <- list(MRF_mod$indirect_coef_mean[[i]],"")[1]
    }
  names(MRF_mod_booted$indirect_coefs) <- names(MRF_mod$indirect_coef_mean)
} else {
  MRF_mod_booted$indirect_coefs <- NULL
}

However, MRF_mod$indirect_coefs does not exist for booted models, so length will always = 0. It should be MRF_mod$indirect_coef_mean. This means that MRF_mod_booted$indirect_coefs is never created, causing downstream problems (i.e. predicted networks will not change with the environment)

The same issue occurs again on line 189

 # If covariates exist, incorporate their modifications to species' interactions 
 if(length(MRF_mod$indirect_coefs) > 0){

Checking MRF_mod$indirect_coefs for booted models will always result in FALSE.