LD_calc <- function(proportion,model.object,intercept,linear.parameter,quad.parameter=NULL,x.min=NULL,x.max=NULL){ post <- rethinking::sample.naive.posterior(model.object) a <- post[[which(names(post)==intercept)]] b <- post[[which(names(post)==linear.parameter)]] if(!is.null(quad.parameter)){ c <- post[[which(names(post)==quad.parameter)]] post.LD.low <- sapply(proportion,function(z)( (-b - sqrt(b^2-4*c*a+4*c*log(z/(1-z))))/(2*c))) post.LD.low.mean <- mean(post.LD.low) post.LD.low.CI <- rethinking::HPDI(post.LD.low,prob=0.95) post.LD.high <- sapply(proportion,function(z)( (-b + sqrt(b^2-4*c*a+4*c*log(z/(1-z))))/(2*c))) post.LD.high.mean <- mean(post.LD.high) post.LD.high.CI <- rethinking::HPDI(post.LD.high,prob=0.95) LD_results <- list("LD_low" = post.LD.low.mean, "LD_low_CI" = post.LD.low.CI, "LD_high" = post.LD.high.mean, "LD_high_CI" = post.LD.high.CI) if(!is.null(x.min)){ if(LD_results$LD_low < x.min){ warning("the predicted lower LD bound is outside of the range of your data") } } if(!is.null(x.max)){ if(LD_results$LD_high > x.max){ warning("the predicted higher LD bound is outside of the range of your data") } } } else if(is.null(quad.parameter)){ post.LD <- sapply(proportion,function(z)(log(z/(1-z))-post$a)/post$b) post.LD.mean <- mean(post.LD) post.LD.CI <- rethinking::HPDI(post.LD,prob=0.95) LD_results <- list("x" = post.LD.mean, "x_CI" = post.LD.CI) if(!is.null(x.max)){ if(post.LD.CI[2] > x.max){ warning("the predicted LD is outside of the range of your data") } } } return(LD_results) }