Title: | Constrained Optimization on Linear Function |
---|---|
Description: | Performs least squares constrained optimization on a linear objective function. It contains a number of algorithms to choose from and offers a formula syntax similar to lm(). |
Authors: | Theo Boutaris [aut, cre, cph] |
Maintainer: | Theo Boutaris <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.3 |
Built: | 2024-11-03 03:19:34 UTC |
Source: | https://github.com/lyzander/colf |
Coefficients for colf_nlxb
## S3 method for class 'colf_nlxb' coef(object, ...)
## S3 method for class 'colf_nlxb' coef(object, ...)
object |
A colf_nlxb object i.e. the result of running |
... |
Currently not used |
A vector with the coefficients
mymod <- colf_nlxb(mpg ~ hp + cyl, mtcars) #coefficients coef(mymod)
mymod <- colf_nlxb(mpg ~ hp + cyl, mtcars) #coefficients coef(mymod)
Non linear least squares optimization using the port algorithm on a linear objective function.
colf_nls(formula, data, start = NULL, trace = FALSE, control = NULL, na.action = c("na.omit", "na.fail", "na.exclude"), lower = -Inf, upper = Inf, ...)
colf_nls(formula, data, start = NULL, trace = FALSE, control = NULL, na.action = c("na.omit", "na.fail", "na.exclude"), lower = -Inf, upper = Inf, ...)
formula |
The formula. This has the same syntax and supports the same features as the
formula in |
data |
A data frame containing the data of the variables in the formula. |
start |
An atomic vector of same length as the number of parameters. If not provided a cheap guess will be made. If categorical variables are included these need to be takent into consideration as number of categories minus one. See examples and details. |
trace |
Logical. Defaults to FALSE. Set to TRUE if you want the intermediate progress to be reported |
control |
an optional list of control settings. See nls.control for the names of the settable control values and their effect. |
na.action |
A function which indicates what should happen if NAs are present in the data set. Defaults to options('na.action'). na.fail, or na.exclude can be used. |
lower |
Lower bounds of the parameters (atomic vector). If a single number, this will be applied to all parameters. Defaults to -Inf (unconstrained). |
upper |
Upper bounds of the parameters (atomic vector). If a single number, this will be applied to all parameters. Defaults to Inf (unconstrained). |
... |
Other arguments passed on to optimiser |
colf_nls
uses nls, in an attempt to find the minimum of the residual sum of squares.
The algorithm is applied on a linear objective function.
The function provides an easy way to apply the optimizer on a linear objective function in a
similar way to lm
.
start, lower and upper, if provided, can be either an atomic vector which has the same length as
the number of parameters or a single number which will be replicated to match the length of the
parameters. If categorical variables exist in the function these will be dummified. Out of one
categorical variable, n - 1 will be created where n is the total number of categories in the
variable. This needs to be taken into account when providing an atomic vector for start, lower or
upper. Also, as with lm
an intercept will be added which also needs to be taken into
account.
Same as nls
#no constraints colf_nls(mpg ~ cyl + disp, mtcars) #no intercept colf_nls(mpg ~ 0 + cyl + disp, mtcars) #including categorical variables. These will be dummified. colf_nls(Sepal.Length ~ Sepal.Width + Species, iris) #lower boundary will be replicated for all parameters colf_nls(Sepal.Length ~ Sepal.Width + Species, iris, lower = 0.5) #species is categorical and contains 3 categories, thus we need to specify 4 lower bounds: #the first one for the intercept. #the second one for Sepal.Width #the two next for the dummy variables constructed from Species. colf_nls(Sepal.Length ~ Sepal.Width + Species, iris, lower = rep(0.5, 4))
#no constraints colf_nls(mpg ~ cyl + disp, mtcars) #no intercept colf_nls(mpg ~ 0 + cyl + disp, mtcars) #including categorical variables. These will be dummified. colf_nls(Sepal.Length ~ Sepal.Width + Species, iris) #lower boundary will be replicated for all parameters colf_nls(Sepal.Length ~ Sepal.Width + Species, iris, lower = 0.5) #species is categorical and contains 3 categories, thus we need to specify 4 lower bounds: #the first one for the intercept. #the second one for Sepal.Width #the two next for the dummy variables constructed from Species. colf_nls(Sepal.Length ~ Sepal.Width + Species, iris, lower = rep(0.5, 4))
Non linear least squares solution via qr linear solver on a linear objective function.
colf_nlxb(formula, data, start = NULL, trace = FALSE, lower = -Inf, upper = Inf, na.action = c("na.omit", "na.fail", "na.exclude"), masked = NULL, control = NULL, ...)
colf_nlxb(formula, data, start = NULL, trace = FALSE, lower = -Inf, upper = Inf, na.action = c("na.omit", "na.fail", "na.exclude"), masked = NULL, control = NULL, ...)
formula |
The formula. This has the same syntax and supports the same features as the
formula in |
data |
A data frame containing the data of the variables in the formula. |
start |
An atomic vector of same length as the number of parameters. If not provided a cheap guess will be made. If categorical variables are included these need to be takent into consideration as number of categories minus one. See examples and details. |
trace |
Logical. Defaults to FALSE. Set to TRUE if you want the intermediate progress to be reported |
lower |
Lower bounds of the parameters (atomic vector). If a single number, this will be applied to all parameters. Defaults to -Inf (unconstrained). |
upper |
Upper bounds of the parameters (atomic vector). If a single number, this will be applied to all parameters. Defaults to Inf (unconstrained). |
na.action |
A function which indicates what should happen if NAs are present in the data set. Defaults to options('na.action'). na.fail, or na.exclude can be used. |
masked |
Character vector of parameter names. These parameters will not be altered by the algorithm. |
control |
A list of controls for the algorithm. These are:
|
... |
Other arguments passed on to optimiser |
colf_nlxb
uses Nash's (Nash, 1979) variant of the Marquardt algorithm, in an attempt to
find the minimum of the residual sum of squares. The algorithm is applied on a linear objective
function.
The function provides an easy way to apply the optimizer on a linear objective function in a
similar way to lm
.
start, lower and upper, if provided, can be either an atomic vector which has the same length as
the number of parameters or a single number which will be replicated to match the length of the
parameters. If categorical variables exist in the function these will be dummified. Out of one
categorical variable, n - 1 will be created where n is the total number of categories in the
variable. This needs to be taken into account when providing an atomic vector for start, lower or
upper. Also, as with lm
an intercept will be added which also needs to be taken into
account.
Same as nlxb
#no constraints colf_nlxb(mpg ~ cyl + disp, mtcars) #no intercept colf_nlxb(mpg ~ 0 + cyl + disp, mtcars) #including categorical variables. These will be dummified. colf_nlxb(Sepal.Length ~ Sepal.Width + Species, iris) #lower boundary will be replicated for all parameters colf_nlxb(Sepal.Length ~ Sepal.Width + Species, iris, lower = 0.5) #species is categorical and contains 3 categories, thus we need to specify 4 lower bounds: #the first one for the intercept. #the second one for Sepal.Width #the two next for the dummy variables constructed from Species. colf_nlxb(Sepal.Length ~ Sepal.Width + Species, iris, lower = rep(0.5, 4))
#no constraints colf_nlxb(mpg ~ cyl + disp, mtcars) #no intercept colf_nlxb(mpg ~ 0 + cyl + disp, mtcars) #including categorical variables. These will be dummified. colf_nlxb(Sepal.Length ~ Sepal.Width + Species, iris) #lower boundary will be replicated for all parameters colf_nlxb(Sepal.Length ~ Sepal.Width + Species, iris, lower = 0.5) #species is categorical and contains 3 categories, thus we need to specify 4 lower bounds: #the first one for the intercept. #the second one for Sepal.Width #the two next for the dummy variables constructed from Species. colf_nlxb(Sepal.Length ~ Sepal.Width + Species, iris, lower = rep(0.5, 4))
Construct an nls-compatible formula from an lm style formula
construct_formula(formula, data)
construct_formula(formula, data)
formula |
The formula. This has the same syntax and supports the same features as the
formula in |
data |
A data frame containing the data of the variables in the formula. |
construct_formula
creates the parameters needed for the formula to be compatible with nls
style functions. It also creates and returns the modelling set.
construct_formula
will make syntactically valid names (if applicable) otherwise the
optimizers will fail. To make these names make.names
is used. Check examples.
A list of three elements:
model_formula - An nls compatible formula
model_data - The modelling set created (inlcuding dummy variables, if any)
x_param_names - The names of the parameters
#simple syntax construct_formula(mpg ~ hp + cyl, mtcars) #example of make.names to create syntactically valid names make.names('(foo/^@bar)') #function will create syntactically valid names (if applicable) #otherwise the optimizers will fail construct_formula(mpg ~ I(hp + cyl), mtcars) construct_formula(mpg ~ (hp + cyl + disp)^3, mtcars)
#simple syntax construct_formula(mpg ~ hp + cyl, mtcars) #example of make.names to create syntactically valid names make.names('(foo/^@bar)') #function will create syntactically valid names (if applicable) #otherwise the optimizers will fail construct_formula(mpg ~ I(hp + cyl), mtcars) construct_formula(mpg ~ (hp + cyl + disp)^3, mtcars)
Fitted values for colf_nlxb
## S3 method for class 'colf_nlxb' fitted(object, ...)
## S3 method for class 'colf_nlxb' fitted(object, ...)
object |
A colf_nlxb object i.e. the result of running |
... |
Currently not used |
A vector with the fitted values
mymod <- colf_nlxb(mpg ~ hp + cyl, mtcars) #fitted values fitted(mymod)
mymod <- colf_nlxb(mpg ~ hp + cyl, mtcars) #fitted values fitted(mymod)
Predict method for colf_nls
## S3 method for class 'colf_nls' predict(object, newdata, ...)
## S3 method for class 'colf_nls' predict(object, newdata, ...)
object |
A colf_nls object |
newdata |
A new data.frame which contains the same column names and classes as the original data.frame |
... |
Currently not used |
predict.colf_nls
will use the fit model to predict on a new data set.
When using predict.colf_nls make sure the column names and classes of the new data set are the same as the data the model was trained on.
A vector with the predictions
mymod <- colf_nls(mpg ~ hp + cyl, mtcars) #prediction predict(mymod, mtcars)
mymod <- colf_nls(mpg ~ hp + cyl, mtcars) #prediction predict(mymod, mtcars)
Predict method for colf_nlxb
## S3 method for class 'colf_nlxb' predict(object, newdata, ...)
## S3 method for class 'colf_nlxb' predict(object, newdata, ...)
object |
A colf_nls object |
newdata |
A new data.frame which contains the same column names and classes as the original data.frame |
... |
Currently not used |
predict.colf_nlxb
will use the fit model to predict on a new data set.
When using predict.colf_nlxb make sure the column names and classes of the new data set are the same as the data the model was trained on.
A vector with the predictions
mymod <- colf_nlxb(mpg ~ hp + cyl, mtcars) #prediction predict(mymod, mtcars)
mymod <- colf_nlxb(mpg ~ hp + cyl, mtcars) #prediction predict(mymod, mtcars)
colf_nlxb Print method
## S3 method for class 'colf_nlxb' print(x, ...)
## S3 method for class 'colf_nlxb' print(x, ...)
x |
A colf_nlxb object i.e. the result of running |
... |
Currently not used |
Printing the colf_nlxb object
mymod <- colf_nlxb(mpg ~ hp + cyl, mtcars) #print print(mymod)
mymod <- colf_nlxb(mpg ~ hp + cyl, mtcars) #print print(mymod)
Residuals for colf_nlxb
## S3 method for class 'colf_nlxb' residuals(object, ...)
## S3 method for class 'colf_nlxb' residuals(object, ...)
object |
A colf_nlxb object i.e. the result of running |
... |
Currently not used |
A vector with the residuals
mymod <- colf_nlxb(mpg ~ hp + cyl, mtcars) #residuals residuals(mymod) resid(mymod)
mymod <- colf_nlxb(mpg ~ hp + cyl, mtcars) #residuals residuals(mymod) resid(mymod)
colf_nlxb Summary
## S3 method for class 'colf_nlxb' summary(object, ...)
## S3 method for class 'colf_nlxb' summary(object, ...)
object |
A colf_nlxb object i.e. the result of running |
... |
Currently not used |
The summary of the model
mymod <- colf_nlxb(mpg ~ hp + cyl, mtcars) #summary summary(mymod)
mymod <- colf_nlxb(mpg ~ hp + cyl, mtcars) #summary summary(mymod)