tda_nlreg is TDA's nlreg command.
Arguments
- formula
a two-sided formula. With
expr, only the response and the predictor names it uses need to be present here – the functional form itself comes fromexpr, not from the formula's own right-hand side.- data
a data frame.
- expr
optional nonlinear expression in TDA's language, written against
formula's predictor names – e.g."a * exp(b * x)"for \(y = a e^{bx}\). Names on the right that are neither predictors nor defined elsewhere are the parameters to be estimated. Without this,nlregfits an ordinary (or orthogonal distance) linear predictor instead.- start
optional named list or vector of starting values, in the order
exprintroduces its parameters. TDA's default starting point is all1s, which will not converge for every functional form.- residuals
for
tda_nlregandtda_lsreg1, ask TDA to write its per-case table (pres=) and return it asresiduals: the case number, the response, the fitted value, the residual, then each predictor. TDA does not compute it unless asked.- control
convergence settings from
tda_control.- options
a named list of further TDA options, passed through:
opt = 1(default) is ordinary least squares,opt = 2orthogonal distance regression.- dir
working directory.
- ...
passed to
tda_run.
Non-linear fits
nlreg takes an optional right-hand-side expression and starting
values (xp=) to fit a non-linear function; without them
it falls back to a linear predictor. That right-hand-side expression
is reachable through expr/start: fitting
\(y = a \, e^{bx}\) on data generated with
\(a=2, b=0.5\) recovers both closely. options = list(opt = 2)
additionally selects orthogonal distance regression (errors in both
variables), which tda_lsreg has no equivalent for.
See also
Other regression:
TDA_FAMILIES,
TDA_QRMODELS,
tda_freg(),
tda_gdf(),
tda_l1reg(),
tda_lsreg(),
tda_mlrc_design(),
tda_mreg(),
tda_npreg(),
tda_zreg()
Examples
set.seed(25)
d <- data.frame(x = round(rnorm(60, sd = 2), 3))
d$y <- round(1.5 + 0.9 * d$x + rnorm(60, sd = 1.5), 3)
reg <- tda_nlreg(y ~ x, d, options = list(opt = 1)) # ordinary least squares
coef(reg)
#> Intercept x
#> 1.2121419 0.7990406
coef(tda_nlreg(y ~ x, d, options = list(opt = 2))) # orthogonal distance
#> Intercept x
#> 1.328604 1.066289
head(fitted(reg)) # no newdata needed
#> [1] 0.8733487 -0.4522596 -0.6312447 1.7259250 -1.1849799 0.5001967
predict(reg, newdata = data.frame(x = c(0, 1, 2)))
#> [1] 1.212142 2.011182 2.810223
# a nonlinear fit: y = a * exp(b * x), true a = 2, b = 0.5
set.seed(1)
d2 <- data.frame(x = round(runif(40, 0, 5), 2))
d2$y <- round(2 * exp(0.5 * d2$x) + rnorm(40, sd = 0.5), 3)
nl <- tda_nlreg(y ~ x, d2, expr = "a * exp(b * x)", start = c(1, 1))
coef(nl)
#> a b
#> 2.0685764 0.4912572
# predict()/fitted() for this case evaluate the fitted expression
# itself (in R, not TDA) rather than X %*% coefficients, which would
# be meaningless for a nonlinear model
head(fitted(nl))
#> [1] 3.975827 5.158259 8.430501 19.243404 3.397468 18.776489
predict(nl, newdata = data.frame(x = c(0, 1, 2, 5)))
#> [1] 2.068576 3.380819 5.525507 24.122540