Fit predefined functions to data via nonlinear least squares using Levenberg-Marquardt algorithm via nlsLM.

fitFunction(
  dataset = cs.in.dataset(),
  preds = cs.in.predictors(),
  resps = cs.in.responses(),
  groups = cs.in.groupvars(),
  auxs = cs.in.auxiliaries(),
  scriptvars = cs.in.scriptvars(),
  return.results = FALSE,
  ...
)

Arguments

dataset

[data.frame]
Dataset with named columns. The names correspond to predictors and responses.

preds

[character]
Character vector of predictor variables.

resps

[character]
Character vector of response variables.

groups

[character]
Character vector of group variables.

auxs

[character]
Character vector of auxiliary variables.

scriptvars

[list]
Named list of script variables set via the Cornerstone "Script Variables" menu. For details see below.

return.results

[logical(1)]
If FALSE the function returns TRUE invisibly. If TRUE, it returns a list of results. Default is FALSE.

...

[ANY]
Additional arguments to be passed to nls . Please consider possible script variables (scriptvars) to prevent duplicates.

Value

Logical [TRUE] invisibly and outputs to Cornerstone or, if return.results = TRUE, list of resulting data.frame objects:

coeff

Estimated coefficients and standard errors for each group. Convergence information is available for every group (for details see link[minpack.lm]{nls.lm}).

vcov

Variance-Covariance matrix of the main coefficients for the fitted model of each group (for details see link[stats]{vcov}).

predictions

Dataset to brush with predictions and residuals added to original values and groups, if available.

Details

The following script variables are summarized in scriptvars list:

math.fun

[character(1)]
Function selection for fitting data. It is possible to choose a predefined model, or compose a model manually by selecting User Defined.
Default is User Defined

preds.frml

[character(1)]
Required if math.fun is set to User Defined. Valid R formula for the right hand side (predictors) of the model equation.

resp.frml

[character(1)]
Required if math.fun is set to User Defined. Valid R formula for the left hand side (response) of the model equation.

limits

[character(1)]
Optional if math.fun is set to User Defined. Specifies minimum and maximum value for function math.fun as a comma separated list of min and max. It is possible to assign variables, e.g. min=a, which need start values in start.vals, as well as real numbers, e.g. min=4.5, with a period as decimal separator.

start.vals

[character(1)]
Required if math.fun is set to User Defined. Specify starting values for all terms of the right hand side as a comma separated list with a period as decimal separator.

weights

[character(1)]
Select a weighting variable from the auxiliary variables.

max.iter

Maximum number of iterations. For details see link[minpack.lm]{nls.lm.control}

max.ftol

Maximum relative error desired in the sum of squares. If 0, the default is used. For details see link[minpack.lm]{nls.lm.control}

Examples

# Generate data from logistic function: fun = function(x, a, b, c, d, sigma = 1) { a+(b-a) / (1+exp(-d*(x-c))) + rnorm(length(x), sd = sigma) } library(data.table) dt = data.table( x1 = sample(seq(-10, 10, length.out = 100)) , group1 = sample(x = c("A", "B"), replace = TRUE, size = 100) ) dt[group1 == "A", y1 := fun(x1, 1, 10, 1, 0.6, 0.1)]
#> x1 group1 y1 #> 1: -1.1111111 A 2.8385145 #> 2: -5.5555556 A 1.1986749 #> 3: 5.1515152 A 9.2673385 #> 4: 2.5252525 A 7.4833385 #> 5: -0.7070707 B NA #> 6: -3.9393939 A 1.6545487 #> 7: 3.5353535 A 8.4289380 #> 8: 4.5454545 B NA #> 9: 3.7373737 B NA #> 10: -9.1919192 B NA #> 11: -5.3535354 A 1.0261820 #> 12: 5.7575758 A 9.5349187 #> 13: 5.3535354 A 9.4920224 #> 14: -9.7979798 A 1.2177368 #> 15: 2.3232323 A 7.2430345 #> 16: 0.9090909 B NA #> 17: -1.5151515 A 2.7688434 #> 18: 7.1717172 A 9.8261547 #> 19: 6.7676768 A 9.7366712 #> 20: 8.1818182 A 9.8828231 #> 21: -1.3131313 A 2.8580806 #> 22: 2.1212121 A 6.9327571 #> 23: -3.3333333 B NA #> 24: 3.9393939 A 8.6301541 #> 25: 9.3939394 B NA #> 26: -9.3939394 A 1.0367932 #> 27: 5.5555556 B NA #> 28: -3.1313131 B NA #> 29: 6.3636364 A 9.5389951 #> 30: -8.7878788 B NA #> 31: -3.7373737 B NA #> 32: 0.5050505 A 4.9213044 #> 33: -8.3838384 B NA #> 34: -5.7575758 A 1.1616012 #> 35: -8.1818182 B NA #> 36: 7.5757576 A 9.6987098 #> 37: 8.3838384 B NA #> 38: 4.7474747 B NA #> 39: -6.7676768 A 0.9898573 #> 40: 0.3030303 B NA #> 41: 1.1111111 A 5.6953786 #> 42: 3.3333333 A 8.1341347 #> 43: 8.9898990 B NA #> 44: -10.0000000 A 0.9835372 #> 45: -8.9898990 A 1.1118847 #> 46: 9.5959596 A 9.9552262 #> 47: 0.1010101 A 4.2986896 #> 48: -0.9090909 B NA #> 49: 1.3131313 A 5.8387572 #> 50: -5.1515152 B NA #> 51: 9.1919192 A 10.1221197 #> 52: -2.7272727 A 1.9454340 #> 53: -8.5858586 B NA #> 54: -3.5353535 B NA #> 55: 2.9292929 B NA #> 56: 5.9595960 A 9.6611972 #> 57: -6.3636364 B NA #> 58: -4.5454545 B NA #> 59: 4.3434343 A 9.0650655 #> 60: -7.9797980 B NA #> 61: -4.9494949 B NA #> 62: 8.5858586 B NA #> 63: 7.3737374 A 9.6957134 #> 64: 9.7979798 B NA #> 65: -9.5959596 A 1.0670351 #> 66: 4.9494949 B NA #> 67: 7.7777778 A 9.6974781 #> 68: 1.9191919 B NA #> 69: 1.7171717 A 6.6067884 #> 70: 10.0000000 B NA #> 71: 0.7070707 B NA #> 72: -2.5252525 B NA #> 73: -0.5050505 A 3.6387682 #> 74: 6.9696970 A 9.7685643 #> 75: -4.7474747 A 1.1635254 #> 76: 4.1414141 B NA #> 77: -7.7777778 A 0.9904071 #> 78: 2.7272727 B NA #> 79: 8.7878788 B NA #> 80: 3.1313131 B NA #> 81: -4.3434343 A 1.4556984 #> 82: -0.1010101 B NA #> 83: -4.1414141 B NA #> 84: 7.9797980 A 9.9332048 #> 85: -5.9595960 A 1.1400297 #> 86: 6.5656566 A 9.6561641 #> 87: 1.5151515 A 6.2682545 #> 88: 6.1616162 A 9.6913493 #> 89: -6.5656566 B NA #> 90: -2.9292929 B NA #> 91: -2.1212121 B NA #> 92: -7.5757576 B NA #> 93: -6.9696970 A 0.8847938 #> 94: -0.3030303 A 3.9189319 #> 95: -7.3737374 B NA #> 96: -1.7171717 A 2.4431945 #> 97: -7.1717172 B NA #> 98: -2.3232323 A 2.1048731 #> 99: -6.1616162 A 0.9417838 #> 100: -1.9191919 A 2.2518799 #> x1 group1 y1
dt[group1 == "B", y1 := fun(x1, 8, 2, -1, 0.3, 0.1)]
#> x1 group1 y1 #> 1: -1.1111111 A 2.8385145 #> 2: -5.5555556 A 1.1986749 #> 3: 5.1515152 A 9.2673385 #> 4: 2.5252525 A 7.4833385 #> 5: -0.7070707 B 4.7549644 #> 6: -3.9393939 A 1.6545487 #> 7: 3.5353535 A 8.4289380 #> 8: 4.5454545 B 2.9920120 #> 9: 3.7373737 B 3.1382410 #> 10: -9.1919192 B 7.5784490 #> 11: -5.3535354 A 1.0261820 #> 12: 5.7575758 A 9.5349187 #> 13: 5.3535354 A 9.4920224 #> 14: -9.7979798 A 1.2177368 #> 15: 2.3232323 A 7.2430345 #> 16: 0.9090909 B 4.1533557 #> 17: -1.5151515 A 2.7688434 #> 18: 7.1717172 A 9.8261547 #> 19: 6.7676768 A 9.7366712 #> 20: 8.1818182 A 9.8828231 #> 21: -1.3131313 A 2.8580806 #> 22: 2.1212121 A 6.9327571 #> 23: -3.3333333 B 5.9117197 #> 24: 3.9393939 A 8.6301541 #> 25: 9.3939394 B 2.3812481 #> 26: -9.3939394 A 1.0367932 #> 27: 5.5555556 B 2.8325707 #> 28: -3.1313131 B 6.0045708 #> 29: 6.3636364 A 9.5389951 #> 30: -8.7878788 B 7.5746584 #> 31: -3.7373737 B 6.1195728 #> 32: 0.5050505 A 4.9213044 #> 33: -8.3838384 B 7.2820779 #> 34: -5.7575758 A 1.1616012 #> 35: -8.1818182 B 7.3459945 #> 36: 7.5757576 A 9.6987098 #> 37: 8.3838384 B 2.5602423 #> 38: 4.7474747 B 2.8037863 #> 39: -6.7676768 A 0.9898573 #> 40: 0.3030303 B 4.3063385 #> 41: 1.1111111 A 5.6953786 #> 42: 3.3333333 A 8.1341347 #> 43: 8.9898990 B 2.1178450 #> 44: -10.0000000 A 0.9835372 #> 45: -8.9898990 A 1.1118847 #> 46: 9.5959596 A 9.9552262 #> 47: 0.1010101 A 4.2986896 #> 48: -0.9090909 B 5.1116873 #> 49: 1.3131313 A 5.8387572 #> 50: -5.1515152 B 6.7144907 #> 51: 9.1919192 A 10.1221197 #> 52: -2.7272727 A 1.9454340 #> 53: -8.5858586 B 7.6404068 #> 54: -3.5353535 B 6.0734997 #> 55: 2.9292929 B 3.6680621 #> 56: 5.9595960 A 9.6611972 #> 57: -6.3636364 B 7.1059107 #> 58: -4.5454545 B 6.5745786 #> 59: 4.3434343 A 9.0650655 #> 60: -7.9797980 B 7.4542602 #> 61: -4.9494949 B 6.5552106 #> 62: 8.5858586 B 2.2378642 #> 63: 7.3737374 A 9.6957134 #> 64: 9.7979798 B 2.1683708 #> 65: -9.5959596 A 1.0670351 #> 66: 4.9494949 B 3.0386118 #> 67: 7.7777778 A 9.6974781 #> 68: 1.9191919 B 3.7776453 #> 69: 1.7171717 A 6.6067884 #> 70: 10.0000000 B 2.2510771 #> 71: 0.7070707 B 4.3620491 #> 72: -2.5252525 B 5.7987598 #> 73: -0.5050505 A 3.6387682 #> 74: 6.9696970 A 9.7685643 #> 75: -4.7474747 A 1.1635254 #> 76: 4.1414141 B 3.1183079 #> 77: -7.7777778 A 0.9904071 #> 78: 2.7272727 B 3.4351542 #> 79: 8.7878788 B 2.4383560 #> 80: 3.1313131 B 3.3401632 #> 81: -4.3434343 A 1.4556984 #> 82: -0.1010101 B 4.5706736 #> 83: -4.1414141 B 6.0728665 #> 84: 7.9797980 A 9.9332048 #> 85: -5.9595960 A 1.1400297 #> 86: 6.5656566 A 9.6561641 #> 87: 1.5151515 A 6.2682545 #> 88: 6.1616162 A 9.6913493 #> 89: -6.5656566 B 7.0557612 #> 90: -2.9292929 B 5.7348798 #> 91: -2.1212121 B 5.4365238 #> 92: -7.5757576 B 7.0610570 #> 93: -6.9696970 A 0.8847938 #> 94: -0.3030303 A 3.9189319 #> 95: -7.3737374 B 7.4924368 #> 96: -1.7171717 A 2.4431945 #> 97: -7.1717172 B 7.0704896 #> 98: -2.3232323 A 2.1048731 #> 99: -6.1616162 A 0.9417838 #> 100: -1.9191919 A 2.2518799 #> x1 group1 y1
# Set script variables scriptvars = list(math.fun = "Logistic", resp.frml = "", preds.frml = "", limits = "" , start.vals = "", weights = "", max.iter = 50, max.ftol = 0 ) # Fit the logistic function: res = fitFunction(dt, "x1", "y1", "group1", character(0), scriptvars, TRUE) # Show estimated coefficients: res$coeff
#> group1 Coeff_a Coeff_b Coeff_c Coeff_d StdErr_a StdErr_b #> 1: A 0.9889561 9.999169 0.9671027 0.5992824 0.02905973 0.03364354 #> 2: B 8.1692693 1.991398 -1.2089269 0.2832009 0.13817812 0.08828132 #> StdErr_c StdErr_d pseudoR2 RMSE Converged Iterations Tolerance #> 1: 0.0291302 0.01006169 0.9993153 0.09908176 TRUE 5 1.490116e-08 #> 2: 0.1739934 0.01577379 0.9961984 0.12200919 TRUE 8 1.490116e-08 #> StopCode StopMessage #> 1: 1 Relative error in the sum of squares is at most `ftol'. #> 2: 1 Relative error in the sum of squares is at most `ftol'.
# Variance-Covariance matrix: res$vcov
#> group1 a b c d #> 1: A 0.0008444677 -0.0002492037 3.120112e-04 1.567246e-04 #> 2: A -0.0002492037 0.0011318880 4.890429e-04 -2.172456e-04 #> 3: A 0.0003120112 0.0004890429 8.485684e-04 -3.536382e-05 #> 4: A 0.0001567246 -0.0002172456 -3.536382e-05 1.012376e-04 #> 5: B 0.0190931916 -0.0076393540 -1.669805e-02 -1.935012e-03 #> 6: B -0.0076393540 0.0077935914 -8.320177e-04 1.153599e-03 #> 7: B -0.0166980482 -0.0008320177 3.027369e-02 1.064677e-03 #> 8: B -0.0019350119 0.0011535991 1.064677e-03 2.488126e-04
# Plot fitted vs. residuals: plot(res$predictions$Fitted, res$predictions$Residuals)