3636# ' \item{diagnostics}{A list containing the Hessian, gradients, eigenvalues, and
3737# ' condition number.}
3838# ' }
39+ # '
40+ # ' @examples
41+ # ' \dontrun{
42+ # ' # 1. Standard fit using default settings
43+ # ' # This uses the design object named "clogit_design" saved in "MyProject"
44+ # ' fit_result <- fishset_fit(
45+ # ' project = "MyProject",
46+ # ' model_name = "clogit_design"
47+ # ' )
48+ # '
49+ # ' # 2. Advanced fit with custom optimization settings and start values
50+ # ' # 'control' and 'start_values' are passed via the '...' argument
51+ # ' fit_custom <- fishset_fit(
52+ # ' project = "MyProject",
53+ # ' model_name = "clogit_design",
54+ # ' fit_name = "clogit_custom_fit",
55+ # '
56+ # ' # Pass control list to nlminb (e.g., increase max iterations, turn on tracing)
57+ # ' control = list(eval.max = 5000, iter.max = 5000, trace = 1),
58+ # '
59+ # ' # Pass initial start values for the parameters (e.g., for 2 predictors)
60+ # ' start_values = c(0.5, -0.2)
61+ # ' )
62+ # ' }
3963# '
4064# ' @seealso \code{\link{fishset_design}} for creating the input design object.
4165# '
@@ -62,7 +86,16 @@ fishset_fit <- function(project,
6286
6387 design <- full_design_list [[model_name ]]
6488
65- if (is_empty(fit_name )) fit_name <- paste0(model_name , " _fit" )
89+ # Load model fit list and check fit_name input
90+ full_fit_list <- unserialize_table(paste0(project , " ModelFit" ), project )
91+
92+ if (is_empty(fit_name )) {
93+ fit_name <- paste0(model_name , " _fit" )
94+ }
95+
96+ if (fit_name %in% names(full_fit_list )) {
97+ stop(paste0(" Model fit '" , fit_name , " ' already exists. Enter a new fit_name." ))
98+ }
6699
67100 # Extract "..." arguments -----------------------------------------------------------------------
68101 dots <- list (... )
@@ -298,6 +331,7 @@ fishset_fit <- function(project,
298331 class(result ) <- " fishset_fit"
299332
300333 # Save to database ------------------------------------------------------------------------------
334+ # Check if fit_name exists
301335 fishset_db <- DBI :: dbConnect(RSQLite :: SQLite(), locdatabase(project = project ))
302336 on.exit(DBI :: dbDisconnect(fishset_db ), add = TRUE )
303337
@@ -389,6 +423,6 @@ print.fishset_fit <- function(x, digits = 4, ...) {
389423 p_val_str <- format.pval(x $ LR_p_value , eps = 0.001 )
390424 cat(" LR Test: Chi2 =" , fmt(x $ LR_stat , 2 ), " , p =" , p_val_str , sig_star , " \n " )
391425 }
392-
426+
393427 invisible (x )
394428}
0 commit comments