Page 1 of 1

summedWgt() function in RPresence

PostPosted: Tue Feb 11, 2025 10:40 pm
by kdavis79
I believe I found a small bug in the summedWgt() function in RPresence. When using it on my model set, I saw that one variable of interest had a summed weight of 0.2275, despite the fact that the two models it belonged to had weights of 0.003 and 0.001 each.

After some digging, I believe this is attributable to an issue with the grepl() and deparse() components of the summedWgt function (as seen when using getAnywhere(summedWgt()) to view the underlying code). In addition to my illogical variable weights, I was also getting the following warning:
Code: Select all
In ind * aic.tab$table$wgt :
  longer object length is not a multiple of shorter object length


According to ChatGPT:
deparse(get(param, model$model)) turns the formula into a character vector, but sometimes deparse() splits it into multiple lines (which is why grepl() returns multiple values).


With the following modification (note the any() wrapped around grepl()), the problem seems to be resolved. I have stopped getting the warning and my model weights now make sense.

Code: Select all
summedWgt_KD <- function(covnames, param, aic.tab) {
    temp <- unlist(lapply(covnames, function(cvn) {
        ind <- unlist(lapply(aic.tab$models, function(model) {
            any(grepl(cvn, paste(deparse(get(param, model$model)), collapse = " ")))
        }))  # Closing bracket for second lapply
        return(sum(ind * aic.tab$table$wgt))
    }))  # Closing bracket for first lapply
    return(data.frame(covnames = covnames, sum.wgt = temp, ER = temp / (1 - temp)))
}


I hope this helps anyone else running into this issue!

Re: summedWgt() function in RPresence

PostPosted: Wed Feb 12, 2025 2:30 pm
by jhines
Thanks for diagnosing the error. I was unable to replicate the problem. Would you be able to send me (jhines@usgs.gov) the code/data which produced this?

Re: summedWgt() function in RPresence

PostPosted: Wed Feb 12, 2025 2:30 pm
by jhines
Thanks for diagnosing the error. I was unable to replicate the problem. Would you be able to send me (jhines@usgs.gov) the code/data which produced this?