### Data Setup and Curve Fitting for Positive Response Example Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Sets up example concentration-response data and fits all available models using tcplfit2_core. Displays all fitted curves, both on linear and log concentration scales. ```R # This is taken from the example under tcplfit2_core conc_ex2 <- c(0.03, 0.1, 0.3, 1, 3, 10, 30, 100) resp_ex2 <- c(0, 0.1, 0, 0.2, 0.6, 0.9, 1.1, 1) # fit all available models in the package # show all fitted curves output_ex2 <- tcplfit2_core(conc_ex2, resp_ex2, 0.8) # arrange the ggplot2 output into a grid grid.arrange(plot_allcurves(output_ex2, conc_ex2, resp_ex2), plot_allcurves(output_ex2, conc_ex2, resp_ex2, log_conc = TRUE), ncol = 2) ``` -------------------------------- ### Example Data Setup for concRespCore Source: https://cran.r-project.org/web/packages/tcplfit2/refman/tcplfit2.html Prepares a named list 'row' with concentration and response data, along with noise estimates, required for the concRespCore function. ```R conc <- list(.03, .1, .3, 1, 3, 10, 30, 100) resp <- list(0, .2, .1, .4, .7, .9, .6, 1.2) row <- list(conc = conc, resp = resp, bmed = 0, cutoff = 1, onesd = .5, name = "some chemical", assay = "some assay") ``` -------------------------------- ### Load and Prepare Example Data Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Loads the 'mc0' dataset and sets up thread usage for data.table. This is the initial step for data processing. ```R data("mc0") data.table::setDTthreads(2) dat <- mc0 ``` -------------------------------- ### Load and Inspect Example Data Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Loads the 'mc3' example dataset, which contains processed concentration-response data. Displays the first few rows to show the structure, including chemical identifiers, concentrations, and responses. ```R # read in the data # Loading in the level 3 example data set from invitrodb stored in tcplfit2 data("mc3") # view the first 6 rows of the mc3 data # dtxsid = unique chemical identifier from EPA's DSSTox Database # casrn = unique chemical identifier from Chemical Abstracts Service # name = chemical name # spid = sample id # logc = log_10 concentration value # resp = response # assay = assay name head(mc3) ``` -------------------------------- ### Install Development Version Source: https://cran.r-project.org/web/packages/tcplfit2/readme/README.html Installs the current development version of the tcplfit2 package from GitHub. Ensure you have the devtools package installed. ```r devtools::install_github("USEPA/CompTox-ToxCast-tcplfit2") ``` -------------------------------- ### Power Model Example Source: https://cran.r-project.org/web/packages/tcplfit2/refman/tcplfit2.html Example usage of the pow function with sample parameters and concentration. ```R pow(c(1,2),1) ``` -------------------------------- ### Prepare Example Data for tcplfit2 Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Extracts and prepares sample concentration-response data from the tcplfit2 package signatures. It also calculates the fit and hitcalling information using core functions. ```R DATA_CASE <- tcplfit2::signatures[1,] conc <- strsplit(DATA_CASE[,"conc"],split = "[|]") %>% unlist() %>% as.numeric() resp <- strsplit(DATA_CASE[,"resp"],split = "[|]") %>% unlist() %>% as.numeric() OG_data <- data.frame(xval = conc,yval = resp) %>% # obtain the concentrations that are outside the cutoff band dplyr::mutate(type = ifelse(abs(resp)>=abs(DATA_CASE[,"cutoff"] Поэтому "Extreme Responses",NA)) %>% mutate(.,df = "OG_data") # obtain the fit and best fitting/hitcalling information fit <- tcplfit2::tcplfit2_core(conc = conc,resp = resp, cutoff = DATA_CASE[,"cutoff"]) hit <- tcplfit2::tcplhit2_core(params = fit, conc = conc,resp = resp, cutoff = DATA_CASE[,"cutoff"], onesd = DATA_CASE[,"onesd"]) ``` -------------------------------- ### Polynomial 2 Model Example Source: https://cran.r-project.org/web/packages/tcplfit2/refman/tcplfit2.html Example usage of the poly2 function with sample parameters and concentration. ```R poly2(c(1,2),1) ``` -------------------------------- ### Load and Prepare Example Data for BMD Analysis Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Loads sample data from the 'mc3' dataset and preprocesses it for concentration-response modeling. It back-transforms log-transformed concentrations and prepares a list object containing experimental data and metadata. ```R # load example data spid <- unique(mc3$spid)[26] ex_df <- mc3[is.element(mc3$spid,spid)] # The data file has stored concentration in log10 form, so fix that conc <- 10^ex_df$logc # back-transforming concentrations on log10 scale resp <- ex_df$resp # pull out all of the chemical identifiers and the name of the assay dtxsid <- ex_df[1,"dtxsid"] casrn <- ex_df[1,"casrn"] name <- ex_df[1,"name"] assay <- ex_df[1,"assay"] # create the row object row_up <- list(conc = conc, resp = resp, bmed = 0, cutoff = cutoff, onesd = onesd,assay=assay, dtxsid=dtxsid,casrn=casrn,name=name) # run the concentration-response modeling for a single sample res_up <- concRespCore(row_up,fitmodels = c("cnst", "hill", "gnls", "poly1", "poly2", "pow", "exp2", "exp3", "exp4", "exp5"), bidirectional=F) # plotting the results max_conc <- max(conc) concRespPlot2(res_up, log_conc = T) + # geom_vline(aes(xintercept = max(log10(conc))),lty = "dashed")+ geom_vline(aes(xintercept = log10(max_conc)),lty = "dashed")+ geom_rect(aes(xmin = log10(res_up[1, "bmdl"]), xmax = log10(res_up[1, "bmdu"]),ymin = 0,ymax = 125), alpha = 0.05,fill = "skyblue") + geom_segment(aes(x = log10(res_up[, "bmd"]), xend = log10(res_up[, "bmd"]), y = 0, yend = 125),col = "blue")+ ggtitle(label = paste(name,"-",assay),subtitle = dtxsid) ``` -------------------------------- ### Polynomial 2 Model (BMDS) Example Source: https://cran.r-project.org/web/packages/tcplfit2/refman/tcplfit2.html Example usage of the poly2bmds function with sample parameters and concentration. ```R poly2bmds(c(1,2),1) ``` -------------------------------- ### Polynomial 1 Model Example Source: https://cran.r-project.org/web/packages/tcplfit2/refman/tcplfit2.html Example usage of the poly1 function with sample parameters and concentration. ```R poly1(1,1) ``` -------------------------------- ### Calculate AUC Example Source: https://cran.r-project.org/web/packages/tcplfit2/refman/tcplfit2.html Demonstrates calculating AUC using sample concentration-response data, fitting, hit-calling, and the post_hit_AUC function. ```R conc <- c(.03, .1, .3, 1, 3, 10, 30, 100) resp <- c(0, .2, .1, .4, .7, .9, .6, 1.2) params <- tcplfit2_core(conc, resp, .8) output <- tcplhit2_core(params, conc, resp, 0.8, 0.5) post_hit_AUC(output) ``` -------------------------------- ### Run Curve Fitting with tcplfit2_core Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html This snippet shows how to load example data, extract concentration and response values, and run the tcplfit2_core function for curve fitting. It also displays the structure of the output. ```R # Load the example data set data("signatures") # using the first row of signature as an example conc <- as.numeric(str_split(signatures[1,"conc"],"\\|")[[1]]) resp <- as.numeric(str_split(signatures[1,"resp"],"\\|")[[1]]) cutoff <- signatures[1,"cutoff"] # run curve fitting output <- tcplfit2_core(conc, resp, cutoff) # show the structure of the output summary(output) ``` -------------------------------- ### Hill Function Example Source: https://cran.r-project.org/web/packages/tcplfit2/refman/tcplfit2.html Example of using the hillfn function with sample concentration and parameter values. ```R hillfn(c(1,2,3),1) ``` -------------------------------- ### Load Primary and Data Formatting Packages Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Loads the necessary R packages for tcplfit2 functionality, data manipulation, and table formatting. Ensure these packages are installed before running. ```r # Primary Packages # library(tcplfit2) library(tcpl) # Data Formatting Packages # library(data.table) library(DT) library(htmlTable) library(dplyr) library(stringr) # Plotting Packages # library(ggplot2) library(gridExtra) ``` -------------------------------- ### Setup and fit negative response curves Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Prepares data for negative response curve fitting, fits multiple models using tcplfit2_core, and generates plots of all fitted curves. This is used to visualize monotonic decreasing responses. ```R # use row 5 in the data conc <- as.numeric(str_split(signatures[5,"conc"],"\|")[[1]]) resp <- as.numeric(str_split(signatures[5,"resp"],"\|")[[1]]) cutoff <- signatures[5,"cutoff"] # plot all models, this is an example of negative curves output_negative <- tcplfit2_core(conc, resp, cutoff) grid.arrange(plot_allcurves(output_negative, conc, resp), plot_allcurves(output_negative, conc, resp, log_conc = TRUE), ncol = 2) ``` -------------------------------- ### Add Binary Hitcall Column to Data Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html This example demonstrates adding a binary hitcall column to a data frame. The output format is similar to the result_table in tcplfit2_core and tcplhit2_core, suitable for plotting with concRespPlot2. ```r TP0001366A07 | 0 | 49.2830638452227 | gnls | 0.25480547525408 | 8.86452058842676 | | | -12.5575945136781 | 7.98842631852464 | 7.9999986953221 | 0.0167893679648239 | 3.91623536169883 | 1.89813253015055 | 33.9132650800971 | | | 0.00148372909623819 | -85.8405179284853 | 0 | 0.0167893674048174 | 3.91623536237081 | -12.5575945050592 | | | | | | | 33.3|33.3|11.1|11.1|3.7|3.7|1.2|1.2|0.412|0.412|0.138|0.138|33.3|33.3|11.1|11.1|3.7|3.7|1.2|1.2|0.412|0.412|0.138|0.138 | 4.11243543988432|20.7326387429006|14.7529451483177|7.10911496582029|-1.76036826257897|-4.59509043954118|-6.99430833999997|-16.3638605847648|-3.6587758270851|-24.2421016154744|-11.1072892792031|-16.4019756012561|-23.1836231299777|1.20158114007354|11.3527779597309|7.25486119401818|-15.6678034542428|-9.75250675357766|-5.5003115908377|-13.0995926623172|-14.8552472512966|-13.4613706078389|-14.086358968162|-11.5213652234997 | dt4 | 0 TP0001366A08 | 0 | 49.2830638452227 | gnls | 0.440022924941614 | 7.45675844926627 | | | -29.8245869314839 | 7.98596120556591 | 0.353240646376545 | 0.083649130537124 | 2.64521777979504 | 1.86978547363224 | 33.9132650800971 | | | 2.45711096575871e-07 | -80.0457583161288 | 0 | 0.0824132481469447 | 12.9099530041626 | -21.6856779032592 | | | | | | | 33.3|33.3|11.1|11.1|3.7|3.7|1.2|0.412|0.412|0.138|0.138|33.3|33.3|11.1|11.1|3.7|3.7|1.2|1.2|0.412|0.412|0.138|0.138 | -9.97203526140537|-9.26141749522581|-8.93154426626026|-17.7216363630617|-0.544184525361571|-19.4566738752308|-7.72856440998552|-19.1592648493029|-20.1683266913997|-14.6279698557126|-24.5517214338418|-6.07777157463346|-16.2381999212206|-4.93900677916975|-19.3878430509545|-8.9921646264341|-8.68643323910698|-2.75155451162059|-19.2249765364499|-33.1882205045543|-29.3179448006381|-16.9564267035365|-33.6131504865745 | dt4 | 0 ``` -------------------------------- ### Prepare Concentration Data for Model Demonstrations Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Sets up two concentration ranges that will be used in subsequent visualizations to demonstrate the effect of parameter changes on model curves. ```R # prepare concentration data for demonstration ex_conc <- seq(0, 100, length.out = 500) ex2_conc <- seq(0, 3, length.out = 100) ``` -------------------------------- ### Setting up Input Data for concRespCore Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Prepare the necessary input data, including concentrations, responses, and baseline parameters, before calling the concRespCore function. ```R # tested concentrations conc <- list(.03,.1,.3,1,3,10,30,100) # observed responses at respective concentrations resp <- list(0,.2,.1,.4,.7,.9,.6, 1.2) # row object with relevant parameters row = list(conc = conc,resp = resp,bmed = 0,cutoff = 1,onesd = 0.5,name="some chemical") ``` -------------------------------- ### Setting up Data for Lower BMD Bound Demonstration Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html This R code prepares data for demonstrating how to impose a lower bound on BMD estimates. It loads the 'mc3' dataset, calculates background variation, and filters the data to simulate a lack of low-dose experimental points. ```r # We'll use data from mc3 in this section data("mc3") # determine the background variation # background is defined per the assay. In this case we use logc <= -2 # However, background should be defined in a way that makes sense for your application temp <- mc3[mc3$logc<= -2,"resp"] bmad <- mad(temp) onesd <- sd(temp) cutoff <- 3*bmad # load example data spid <- unique(mc3$spid)[94] ex_df <- mc3[is.element(mc3$spid,spid),] # The data file has stored concentration in log10 form, fix it conc <- 10^ex_df$logc # back-transforming concentrations on log10 scale resp <- ex_df$resp # modify the data for demonstration purposes conc2 <- conc[conc>0.41] resp2 <- resp[which(conc>0.41)] # pull out all of the chemical identifiers and the name of the assay dtxsid <- ex_df[1,"dtxsid"] casrn <- ex_df[1,"casrn"] name <- ex_df[1,"name"] assay <- ex_df[1,"assay"] # create the row object row_low <- list(conc = conc2, resp = resp2, bmed = 0, cutoff = cutoff, onesd = onesd, assay=assay, dtxsid=dtxsid,casrn=casrn,name=name) ``` -------------------------------- ### Prepare Data and Run Concentration-Response Modeling with concRespCore Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html This snippet illustrates how to prepare a 'row' object with necessary data and run the concRespCore function for concentration-response modeling. It's a precursor to plotting the winning model. ```R # prepare the 'row' object for concRespCore row <- list(conc=conc, resp=resp, bmed=0, cutoff=cutoff, onesd=signatures[1,"onesd"], name=signatures[1,"name"], assay=signatures[1,"signature"]) # run concentration-response modeling out <- concRespCore(row,conthits=F) ``` -------------------------------- ### Simulate and Plot Biphasic Poly2 Curve Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Simulates a bi-phasic concentration-response curve using the poly2 model and plots the result. This code is used to generate example data for AUC estimation. ```R # simulate a poly2 curve conc_sim <- seq(0,3, length.out = 100) ## biphasic poly2 parameters b1 <- -1.3 b2 <- 0.7 ## converted to tcplfit2's poly2 parameters a <- b1^2/b2 b <- b1/b2 c(a,b) #> [1] 2.414286 -1.857143 ## plot the curve resp_sim <- poly2(c(a, b, 0.1), conc_sim) plot(conc_sim, resp_sim, type = "l", xlab = "Concentration",ylab = "Response",main = "Biphasic Response") abline(h = 0) ``` -------------------------------- ### Calculate Top Likelihood Probabilities Source: https://cran.r-project.org/web/packages/tcplfit2/refman/tcplfit2.html Demonstrates how to use the toplikelihood function with different cutoff values. Ensure all required parameters are correctly defined before calling. ```R fname = "hillfn" conc = c(.03,.1,.3,1,3,10,30,100) resp = c(0,.1,0,.2,.6,.9,1.1,1) ps = c(1.033239, 2.453014, 1.592714, er = -3.295307) top = 1.023239 mll = 12.71495 toplikelihood(fname, cutoff = .8, conc, resp, ps, top, mll) toplikelihood(fname, cutoff = 1, conc, resp, ps, top, mll) toplikelihood(fname, cutoff = 1.2, conc, resp, ps, top, mll) ``` -------------------------------- ### Get absolute AUC for negative response Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Demonstrates how to obtain the absolute value of the AUC for a negative response curve by setting the 'return.abs' argument to TRUE in the get_AUC function. This converts negative AUC values to positive. ```R get_AUC(fit_method, min(conc), max(conc), modpars, return.abs = TRUE) ``` -------------------------------- ### Prepare Results for Comparison Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Adds calculated minimum concentration and a descriptive name to the results object. It then rounds relevant BMD columns and combines these results with previous output for comparison. ```R # print out the new results # add to previous results for comparison res_low3['Min. Conc.'] <- min(conc2) res_low3['Name'] <- paste("Chlorothalonil", "after `bounding` (two fifths)", sep = "-") res_low3[1, c("Min. Conc.", "bmd", "bmdl", "bmdu")] <- round(res_low3[1, c("Min. Conc.", "bmd", "bmdl", "bmdu")], 3) output_low <- rbind(output_low[-3, ], res_low3[1, c('Name', "Min. Conc.", "bmd", "bmdl", "bmdu")]) ``` -------------------------------- ### Concise Response Core Output with AUC Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Demonstrates the output of concRespCore when conthits and AUC are set to TRUE, showing various fit metrics and AUC. ```R concRespCore(row, conthits = TRUE, AUC = TRUE) ``` -------------------------------- ### Perform Concentration-Response Modeling and Hitcalling Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html This snippet iterates through a list of samples, performs concentration-response modeling using tcplfit2_core, generates plots of the fits, and then uses tcplhit2_core to identify hits. It demonstrates data preparation, model fitting, and result aggregation. ```R temp <- mc3[mc3$logc<= -2,"resp"] # obtain response in the two lowest concentrations bmad <- mad(temp) # obtain the baseline median absolute deviation onesd <- sd(temp) # obtain the baseline standard deviation cutoff <- 3*bmad # estimate the cutoff, use the typical cutoff=3*BMAD spid.list <- unique(mc3$spid) spid.list <- spid.list[1:6] model_fits <- NULL result_table <- NULL plt_lst <- NULL for(spid in spid.list) { # select the data for just this sample temp <- mc3[is.element(mc3$spid,spid),] # The data file stores concentrations in log10 units, so back-transform to "raw scale" conc <- 10^temp$logc # Save the response values resp <- temp$resp # pull out all of the chemical identifiers and the assay name dtxsid <- temp[1,"dtxsid"] casrn <- temp[1,"casrn"] name <- temp[1,"name"] assay <- temp[1,"assay"] # Execute curve fitting # Input concentrations, responses, cutoff, a list of models to fit, and other model fitting requirements # force.fit is set to true so that all models will be fit regardless of cutoff # bidirectional = FALSE indicates only fit models in the positive direction. # if using bidirectional = TRUE the coff only needs to be specified in the positive direction. model_fits[[spid]] <- tcplfit2_core(conc, resp, cutoff, force.fit = TRUE, fitmodels = c("cnst", "hill", "gnls", "poly1", "poly2", "pow", "exp2","exp3", "exp4", "exp5"), bidirectional = FALSE) # Get a plot of all curve fits plt_lst[[spid]] <- plot_allcurves(model_fits[[spid]], conc = conc, resp = resp, log_conc = TRUE) # Pass the output from 'tcplfit2_core' to 'tcplhit2_core' along with # cutoff, onesd, and any identifiers out <- tcplhit2_core(model_fits[[spid]], conc, resp, bmed = 0, cutoff = cutoff, onesd = onesd, identifiers = c(dtxsid = dtxsid, casrn = casrn, name = name, assay = assay)) # store all results in one table result_table <- rbind(result_table,out) } ``` -------------------------------- ### Prepare Data for Exp4 Parameter Visualization in R Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Creates a data frame with different response values for the Exponential 4 model by varying the 'tp' and 'ga' parameters. This data is intended for subsequent plotting. ```r fits_exp4 <- data.frame( # change tp y1 = exp4(ps = c(tp = 895,ga = 15),x = ex_conc), y2 = exp4(ps = c(tp = 200,ga = 15),x = ex_conc), y3 = exp4(ps = c(tp = -500,ga = 15),x = ex_conc), # change ga y4 = exp4(ps = c(tp = 500,ga = 0.4),x = ex_conc), y5 = exp4(ps = c(tp = 500,ga = 10),x = ex_conc), y6 = exp4(ps = c(tp = 500,ga = 20),x = ex_conc) ) ``` -------------------------------- ### Displaying BMD Results Before and After Bounding in R Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html This code prepares data for comparison by adding results before and after applying the lower bound BMD bounding. It includes the minimum concentration, BMD, BMDL, and BMDU for both scenarios. ```R # print out the new results # include previous results side by side for comparison res_low2['Min. Conc.'] <- min(conc2) res_low2['Name'] <- paste(name, "after `bounding`", sep = "-") res_low['Name'] <- paste(name, "before `bounding`", sep = "-") res_low2[1, c("Min. Conc.", "bmd", "bmdl", "bmdu")] <- round(res_low2[1, c("Min. Conc.", "bmd", "bmdl", "bmdu")], 3) output_low <- rbind(res_low[1, c('Name', "Min. Conc.", "bmd", "bmdl", "bmdu")], res_low2[1, c('Name', "Min. Conc.", "bmd", "bmdl", "bmdu")]) ``` -------------------------------- ### Running Concentration-Response Modeling with Lower BMD Bound Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html This snippet executes the concentration-response modeling using concRespCore with specified models and bidirectional=FALSE. It then prepares for plotting the results, highlighting the minimum concentration, BMD estimate, and its confidence interval. ```r # run the concentration-response modeling for a single sample res_low <- concRespCore(row_low,fitmodels = c("cnst", "hill", "gnls", "poly1", "poly2", "pow", "exp2", "exp3", "exp4", "exp5"), bidirectional=F) # plotting the results min_conc <- min(conc2) concRespPlot2(res_low, log_conc = T) + geom_vline(aes(xintercept = log10(min_conc)),lty = "dashed")+ geom_rect(aes(xmin = log10(res_low[1, "bmdl"]), xmax = log10(res_low[1, "bmdu"]),ymin = 0,ymax = 30), alpha = 0.05,fill = "skyblue") + geom_segment(aes(x = log10(res_low[, "bmd"]), xend = log10(res_low[, "bmd"]), y = 0, yend = 30),col = "blue")+ ggtitle(label = paste(name,"-",assay),subtitle = dtxsid) ``` -------------------------------- ### Plot All Model Fits with plot_allcurves Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Demonstrates using the plot_allcurves function with the output from tcplfit2_core to visualize all included model fits. It shows how to generate plots in both original and log-10 concentration scales and arrange them in a grid. ```R # get plots in the original and in log-10 concentration scale basic <- plot_allcurves(output, conc, resp) basic_log <- plot_allcurves(output, conc, resp, log_conc = T) # arrange the ggplot2 output into a grid grid.arrange(basic, basic_log) ``` -------------------------------- ### Tune Benchmark Dose (BMD) Bounds Source: https://cran.r-project.org/web/packages/tcplfit2/refman/tcplfit2.html Tunes the upper and lower bounds for Benchmark Dose (BMD) using a maximum likelihood method. Requires fit method, benchmark risk (bmr), model parameters, concentration, response, and optionally a pre-calculated BMD value. ```R bmdbounds( fit_method, bmr, pars, conc, resp, onesidedp = 0.05, bmd = NULL, which.bound = "lower", poly2.biphasic = TRUE, x_v ) ``` -------------------------------- ### Visualize Concentration-Response Modeling Results Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html This snippet demonstrates how to visualize the results of concentration-response modeling for tcpl-like data. It assumes the data is in a format compatible with the concRespPlot2 function. ```r # The following code chunk demonstrates how to visualize the Concentration-Response Modeling for `tcpl`-like data without a database connection fit results. ``` -------------------------------- ### Compare BMD Results Before and After Upper Bounding Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Combines and rounds the results from before and after applying the upper BMD bound (`bmd_up_bnd`) for direct comparison. This highlights the effect of the bounding on BMD and its confidence interval. ```R # print out the new results # include previous results side by side for comparison res_up2['Max Conc.'] <- max(conc) res_up2['Name'] <- paste(name, "after `bounding`", sep = "-") res_up['Name'] <- paste(name, "before `bounding`", sep = "-") res_up2[1, c("Max Conc.", "bmd", "bmdl", "bmdu")] <- round(res_up2[1, c("Max Conc.", "bmd", "bmdl", "bmdu")], 3) output_up <- rbind(res_up[1, c('Name', "Max Conc.", "bmd", "bmdl", "bmdu")], res_up2[1, c('Name', "Max Conc.", "bmd", "bmdl", "bmdu")]) ``` -------------------------------- ### Apply Upper BMD Bound with bmd_up_bnd Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Re-runs the concentration-response modeling using the `bmd_up_bnd` argument to set an upper threshold for the benchmark dose (BMD). This is useful when the estimated BMD exceeds the maximum tested concentration. ```R # using bmd_up_bnd = 2 res_up2 <- concRespCore(row_up,fitmodels = c("cnst", "hill", "gnls", "poly1", "poly2", "pow", "exp2", "exp3", "exp4", "exp5"), bidirectional=F, bmd_up_bnd = 2) #> Warning in fitpoly2(conc = c(0.0199999999999999, 0.3, 0.0599999999999995, : The #> `bidirectional` argument is ignored when `biphasic = TRUE`. ``` -------------------------------- ### Empirically Calculate Top Concentration Source: https://cran.r-project.org/web/packages/tcplfit2/refman/tcplfit2.html Estimates the maximum response (top) and the concentration at which it occurs by sampling points within the experimental range. Requires concentration, parameters, and model function name. ```R conc = c(.03,.1,.3,1,3,10,30,100) ps = c(1,2,1,2,2) fname = "gnls" calcempirical_top(conc, ps, fname) ``` -------------------------------- ### Plotting Dose-Response Curves and BMD Estimates Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Generates a dose-response plot, marking maximum concentration, boundary dose, and estimated BMD with confidence intervals before and after bounding. This is useful for visualizing fitting results and the impact of bounding on BMD estimation. ```R # generate some concentration for the fitting curve logc_plot <- seq(from=-3,to=2,by=0.05) conc_plot <- 10^logc_plot # initiate plot plot(conc,resp,xlab="conc (uM)",ylab="Response",xlim=c(0.001,500),ylim=c(-5,150), log="x",main=paste(name,"\n",assay),cex.main=0.9) # add vertical lines to mark the maximum concentration in the data and the upper boundary set by bmd_up_bnd abline(v=max(conc), lty = 1, col = "brown", lwd=2) abline(v=160, lty = 2, col = "darkviolet", lwd=2) # add marker for BMD and its boundaries before `bounding` lines(c(res_up$bmd,res_up$bmd),c(0,125),col="green",lwd=2) rect(xleft=res_up$bmdl,ybottom=0,xright=res_up$bmdu,ytop=125,col=rgb(0,1,0, alpha = .5), border = NA) points(res_up$bmd, -0.5, pch = "x", col = "green") # add marker for BMD and its boundaries after `bounding` lines(c(res_up2$bmd,res_up2$bmd),c(0,125),col="blue",lwd=2) rect(xleft=res_up2$bmdl,ybottom=0,xright=res_up2$bmdu,ytop=125,col=rgb(0,0,1, alpha = .5), border = NA) points(res_up2$bmd, -0.5, pch = "x", col = "blue") # add the fitting curve lines(conc_plot, poly1(ps = c(res_up$a), conc_plot)) legend(1e-3, 150, legend=c("Maximum Dose Tested", "Boundary", "BMD-before", "BMD-after"), col=c("brown", "darkviolet", "green", "blue"), lty=c(1,2,1,1)) ``` -------------------------------- ### Gain-Loss Plot for 'q' Parameter Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Visualizes the effect of changing the 'q' (loss power) parameter on the Gain-Loss model curve. Uses pre-computed 'fits_gnls' data. ```R gnls_plot2 <- ggplot(fits_gnls, aes(log10(ex_conc))) + geom_line(aes(y = y4, color = "q=0.3")) + geom_line(aes(y = y5, color = "q=1.2")) + geom_line(aes(y = y6, color = "q=8")) + labs(x = "Concentration in Log-10 Scale", y = "Response") + theme_bw()+ theme(legend.position = c(0.15,0.8)) + scale_color_manual(name='q values', breaks=c('q=0.3', 'q=1.2', 'q=8'), values=c('q=0.3'='black', 'q=1.2'='red', 'q=8'='blue')) ``` -------------------------------- ### Perform Hitcalling and Bounding with tcplhit2_core Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Performs hitcalling and applies BMD bounding using the `tcplhit2_core` function. This snippet demonstrates how to use the `bmd_low_bnd` argument to replicate bounding results achieved with `concRespCore`. ```R hit_res <- tcplhit2_core(param, conc2, resp2, cutoff = cutoff, onesd = onesd, bmd_low_bnd = 0.8) ``` -------------------------------- ### Augmenting Output with tcplhit2_core Results Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Adds the results from `tcplhit2_core` to an output table for comparison. This involves naming the results and rounding relevant numerical columns before binding. ```R # adding the result from tcplhit2_core to the output table for comparison hit_res["Name"]<- paste("Chlorothalonil", "tcplhit2_core", sep = "-") hit_res['Min. Conc.'] <- min(conc2) hit_res[1, c("Min. Conc.", "bmd", "bmdl", "bmdu")] <- round(hit_res[1, c("Min. Conc.", "bmd", "bmdl", "bmdu")], 3) output_low <- rbind(output_low, hit_res[1, c('Name', "Min. Conc.", "bmd", "bmdl", "bmdu")]) ``` -------------------------------- ### Gain-Loss Model Source: https://cran.r-project.org/web/packages/tcplfit2/refman/tcplfit2.html Implements the Gain-Loss dose-response model, defined by the equation f(x)=tp/[(1 + (ga/x)^p )(1 + (x/la)^q )]. It takes model parameters and concentration values as input. ```APIDOC ## Gain-Loss Model ### Description `f(x)=tp/[(1 + (ga/x)^p )(1 + (x/la)^q )]` ### Usage ``` gnls(ps, x) ``` ### Arguments `ps` | Vector of parameters: tp,ga,p,la,q,er `x` | Vector of concentrations (regular units) ### Value Vector of model responses ### Examples ``` gnls(c(1,2,1,2,2),1) ``` ``` -------------------------------- ### exp5 Source: https://cran.r-project.org/web/packages/tcplfit2/refman/tcplfit2.html Calculates the response for the Exponential 5 model given parameters and concentrations. ```APIDOC ## Exponential 5 Model ### Description `f(x)=tp∗(1−2(−(x/ga)p))f(x) = tp*(1-2^{(-(x/ga)^p)})f(x)=tp∗(1−2(−(x/ga)p))` ### Usage ``` exp5(ps, x) ``` ### Arguments `ps` | Vector of parameters: tp,ga,p,er `x` | Vector of concentrations (regular units) ### Value Vector of model responses ### Examples ``` exp5(c(1,2,3),1) ``` ``` -------------------------------- ### Run Core Hitcalling Function Source: https://cran.r-project.org/web/packages/tcplfit2/refman/tcplfit2.html This function selects the winning model, extracts key parameters like top and ac50, computes hitcalls, and calculates BMD statistics. It uses nested model selection and requires tcplfit2_core to be run with force.fit = TRUE for continuous hitcalls. ```R tcplhit2_core( params, conc, resp, cutoff, onesd, bmr_scale = 1.349, bmed = 0, conthits = TRUE, aicc = FALSE, identifiers = NULL, bmd_low_bnd = NULL, bmd_up_bnd = NULL, poly2.biphasic = TRUE, verbose = FALSE ) ``` -------------------------------- ### Create Reference Lines Dataframe Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Creates a dataframe containing reference lines for plotting, such as cutoff, BMR, and top response. ```R ref_df <- data.frame( xval = rep(0,6), yval = c(hit$cutoff*c(-1,1), hit$bmr*c(-1,1), fit$exp4$top, hit$cutoff), type = c(rep("Cutoff",2),rep("BMR",2),"Top","Top at Cutoff") ) %>% mutate(.,df = "ref_df") ``` -------------------------------- ### Basic Concentration-Response Plotting Source: https://cran.r-project.org/web/packages/tcplfit2/refman/tcplfit2.html Generates a concentration-response plot using provided data and a fitted model. Requires pre-defined 'row' data structure. ```R conc <- list(.03, .1, .3, 1, 3, 10, 30, 100) resp <- list(0, .2, .1, .4, .7, .9, .6, 1.2) row <- list(conc = conc, resp = resp, bmed = 0, cutoff = 0.25, onesd = 0.125, name = "some chemical", assay = "some assay") res <- concRespCore(row, conthits = TRUE) concRespPlot(res,ymin=-2.5,ymax=2.,5) ``` -------------------------------- ### Plotting All Models with concRespCore Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Fits concentration-response models for each observation in the `signatures` dataset and generates plots of all curve fits using the `do.plot=TRUE` argument in `concRespCore`. Requires the `signatures` dataset to be loaded. ```R # read in the file data("signatures") # set up a 3 x 2 grid for the plots oldpar <- par(no.readonly = TRUE) on.exit(par(oldpar)) par(mfrow=c(3,2),mar=c(4,4,5,2)) # fit 6 observations in signatures for(i in 1:nrow(signatures)){ # set up input data row = list(conc=as.numeric(str_split(signatures[i,"conc"],"\\|")[[1]]), resp=as.numeric(str_split(signatures[i,"resp"],"\\|")[[1]]), bmed=0, cutoff=signatures[i,"cutoff"], onesd=signatures[i,"onesd"], name=signatures[i,"name"], assay=signatures[i,"signature"]) # run concentration-response modeling (1st plotting option) out = concRespCore(row,conthits=F,do.plot=T) if(i==1){ res <- out }else{ res <- rbind.data.frame(res,out) } } ``` -------------------------------- ### Visualize Exp2 Parameter Changes in R Source: https://cran.r-project.org/web/packages/tcplfit2/vignettes/tcplfit2-vignette.html Generates plots to illustrate the effect of changing parameters 'a' and 'b' on the Exponential 2 dose-response curve. Requires the ggplot2 and gridExtra packages. ```r fits_exp2 <- data.frame( # change a y1 = exp2(ps = c(a = 20,b = 12), x = ex2_conc), y2 = exp2(ps = c(a = 9,b = 12), x = ex2_conc), y3 = exp2(ps = c(a = 0.1,b = 12), x = ex2_conc), y4 = exp2(ps = c(a = -3,b = 12), x = ex2_conc), # change b y5 = exp2(ps = c(a = 0.45,b = 4), x = ex2_conc), y6 = exp2(ps = c(a = 0.45,b = 9), x = ex2_conc), y7 = exp2(ps = c(a = 0.45,b = 20), x = ex2_conc) ) # shows how changes in parameter 'a' affect the shape of the curve exp2_plot1 <- ggplot(fits_exp2, aes(ex2_conc)) + geom_line(aes(y = y1, color = "a=20")) + geom_line(aes(y = y2, color = "a=9")) + geom_line(aes(y = y3, color = "a=0.1")) + geom_line(aes(y = y4, color = "a=(-3)")) + labs(x = "Concentration", y = "Response") + theme_bw()+ theme(legend.position = c(0.15,0.8)) + scale_color_manual(name='a values', breaks=c('a=(-3)', 'a=0.1', 'a=9', 'a=20'), values=c('a=(-3)'='black', 'a=0.1'='red', 'a=9'='blue', 'a=20'='darkviolet')) # shows how changes in parameter 'b' affect the shape of the curve exp2_plot2 <- ggplot(fits_exp2, aes(ex2_conc)) + geom_line(aes(y = y5, color = "b=4")) + geom_line(aes(y = y6, color = "b=9")) + geom_line(aes(y = y7, color = "b=20")) + labs(x = "Concentration", y = "Response") + theme_bw()+ theme(legend.position = c(0.15,0.8)) + scale_color_manual(name='b values', breaks=c('b=4', 'b=9', 'b=20'), values=c('b=4'='black', 'b=9'='red', 'b=20'='blue')) grid.arrange(exp2_plot1, exp2_plot2, ncol = 2) ```