Title: | A Model Object Framework for Regression Analysis |
---|---|
Description: | A utility library to facilitate the generalization of statistical methods built on a regression framework. Package developers can use 'modelObj' methods to initiate a regression analysis without concern for the details of the regression model and the method to be used to obtain parameter estimates. The specifics of the regression step are left to the user to define when calling the function. The user of a function developed within the 'modelObj' framework creates as input a 'modelObj' that contains the model and the R methods to be used to obtain parameter estimates and to obtain predictions. In this way, a user can easily go from linear to non-linear models within the same package. |
Authors: | Shannon T. Holloway |
Maintainer: | Shannon T. Holloway <[email protected]> |
License: | GPL-2 |
Version: | 4.2 |
Built: | 2025-02-06 04:25:26 UTC |
Source: | https://github.com/cran/modelObj |
A utility function to transfer user defined models and estimation methods to an object of class modelObj.
buildModelObj( model, solver.method = NULL, solver.args = NULL, predict.method = NULL, predict.args = NULL )
buildModelObj( model, solver.method = NULL, solver.args = NULL, predict.method = NULL, predict.args = NULL )
model |
An object of class formula; the model. |
solver.method |
An object of class character specifying the name of the R function to be used to obtain parameter estimates. Or, the function to be used to obtain parameter estimates. For example, ‘lm’, ‘glm’, or ‘rpart’. The specified modeling function MUST have a corresponding predict method. |
solver.args |
An object of class list containing additional arguments to be sent to solver.method. Arguments must be provided as a list, where the name of each element matches a formal argument of solver.method. For example, if a logistic regression using glm is desired,
A solver.method can takes formal arguments 'formula' and 'data' as inputs,
such as lm and glm. Some R methods do not use formal names 'formula' and
'data'; a user can indicate if a different naming convention is used for
these two input arguments. For example, if a method expects the formula
object to be passed through input variable A solver.method can also take formal arguments 'x' and 'y' as inputs,
such as glmnet. Some R methods do not use formal names 'x' and 'y' to
indicate the covariate and response; a user can indicate if a different
naming convention is used for these two input arguments. For example, if a
method expects the covariate matrix to be passed through input variable
|
predict.method |
A character. The name of the R function or the
function to be used to obtain predictions. For example, ‘predict.lm’,
‘predict’, or ‘predict.glm’. If no function is explicitly given, the
generic |
predict.args |
A list. Additional arguments to be sent to predict.method. This must be provided as a list, where the name of each element matches a formal argument of predict.method. For example, if a logistic regression using glm was used to fit the model formula object and predictions on the scale of the response are desired,
It is assumed that the predict.method has formal arguments “object" and “newdata". If predict.method does not use these formal arguments, predict.args must explicitly indicate the variable names used for these inputs. For example, list(“newx"=“newdata") if the new data is passed to predict.method through input argument “newx". |
Unless changed by the user in solver.args and/or predict.args, default settings are assumed for the specified regression and prediction methods.
An object of class modelObjFormula
or modelObjXY
, which
inherit directly from modelObj.
#----------------------------------------------------# # Create modeling object using a formula #----------------------------------------------------# mo <- buildModelObj(model=Y ~ X1 + X2 + X3 + X4, solver.method='lm', predict.method='predict.lm', predict.args=list(type='response'))
#----------------------------------------------------# # Create modeling object using a formula #----------------------------------------------------# mo <- buildModelObj(model=Y ~ X1 + X2 + X3 + X4, solver.method='lm', predict.method='predict.lm', predict.args=list(type='response'))
Performs specified regression analysis.
fit(object, data, response, ...) ## S4 method for signature 'modelObj,data.frame' fit(object, data, response, ...)
fit(object, data, response, ...) ## S4 method for signature 'modelObj,data.frame' fit(object, data, response, ...)
object |
An object of class |
data |
An object of class data.frame containing the variables in the model. |
response |
An object of class vector containing the response variable. |
... |
ignored |
If defined by the modeling function, the following methods can be applied
to the value object returned: coef
, plot
, predict
,
print
, residuals
, show
, and summary
.
An object of class modelObjFit
, which contains the
object returned by the modeling function and the method to be used to
obtain predictions.
# generate data X <- matrix(rnorm(1000,0,1), ncol=4, dimnames=list(NULL,c("X1","X2","X3","X4"))) Y <- X %*% c(0.1, 0.2, 0.3, 0.4) + rnorm(250) X <- data.frame(X) # create modeling object using a formula mo <- buildModelObj(model=Y ~ X1 + X2 + X3 + X4, solver.method='lm') # fit model fit.obj <- fit(object=mo, data=X, response=Y) coef(fit.obj) head(residuals(fit.obj)) plot(fit.obj) head(predict(fit.obj,X)) summary(fit.obj)
# generate data X <- matrix(rnorm(1000,0,1), ncol=4, dimnames=list(NULL,c("X1","X2","X3","X4"))) Y <- X %*% c(0.1, 0.2, 0.3, 0.4) + rnorm(250) X <- data.frame(X) # create modeling object using a formula mo <- buildModelObj(model=Y ~ X1 + X2 + X3 + X4, solver.method='lm') # fit model fit.obj <- fit(object=mo, data=X, response=Y) coef(fit.obj) head(residuals(fit.obj)) plot(fit.obj) head(predict(fit.obj,X)) summary(fit.obj)
Retrieves the value object returned by the regression method used to obtain parameter estimates.
fitObject(object, ...) ## S4 method for signature 'ANY' fitObject(object, ...) ## S4 method for signature 'modelObjFit' fitObject(object, ...)
fitObject(object, ...) ## S4 method for signature 'ANY' fitObject(object, ...) ## S4 method for signature 'modelObjFit' fitObject(object, ...)
object |
An object of class modelObjFit. |
... |
ignored. |
This function is useful for accessing methods that are defined by the regression method but are not directly accessible from the modelObjFit object. For example, for many regression methods, users can retrieve the fitted values by calling fitted.values(object). This method is not directly accessible from a modelObjFit. However, fitted.values() can be applied to the object returned by fitObject().
The Value returned by the regression method specified in the governing modelObj. The exact structure of the value will depend on the regression method. For example, if nls() is the regression method, a list is returned.
# Generate data X <- matrix(rnorm(1000,0,1), ncol=4, dimnames=list(NULL,c("X1","X2","X3","X4"))) Y <- X %*% c(0.1, 0.2, 0.3, 0.4) + rnorm(250) X <- data.frame(X) # Create modeling object using a formula mo <- buildModelObj(model=Y ~ X1 + X2 + X3 + X4, solver.method='lm') # Fit model fit.obj <- fit(object=mo, data=X, response=Y) obj <- fitObject(fit.obj) fobj <- fitted.values(obj) head(fobj)
# Generate data X <- matrix(rnorm(1000,0,1), ncol=4, dimnames=list(NULL,c("X1","X2","X3","X4"))) Y <- X %*% c(0.1, 0.2, 0.3, 0.4) + rnorm(250) X <- data.frame(X) # Create modeling object using a formula mo <- buildModelObj(model=Y ~ X1 + X2 + X3 + X4, solver.method='lm') # Fit model fit.obj <- fit(object=mo, data=X, response=Y) obj <- fitObject(fit.obj) fobj <- fitted.values(obj) head(fobj)
Retrieves model from modelObj
model(object, ...) ## S4 method for signature 'ANY' model(object, ...) ## S4 method for signature 'modelObj' model(object, ...) ## S4 method for signature 'modelObjFit' model(object, ...)
model(object, ...) ## S4 method for signature 'ANY' model(object, ...) ## S4 method for signature 'modelObj' model(object, ...) ## S4 method for signature 'modelObjFit' model(object, ...)
object |
A modelObj object |
... |
ignored |
The formula for the regression
modelObj
A class for model objects.
Objects should not be created directly. The utility function buildModelObj() should be used.
model
Object of class formula
solver
Object of class methodObjSolver
method to obtain
parameter estimates.
predictor
Object of class methodObjPredict
method to obtain
predicted values.
: Executes regression step.
: Retrieve model.
: Retrieve regression method name.
: Retrieve arguments to be sent to regression method.
: Set arguments to be sent to regression method.
: Retrieve prediction method name.
: Retrieve arguments to be sent to prediction method.
: Set arguments to be sent to prediction method.
showClass("modelObj")
showClass("modelObj")
modelObjFit
A class for storing regression analysis results.
## S4 method for signature 'modelObjFit' coef(object, ...) ## S4 method for signature 'modelObjFit' plot(x, y, ...) ## S4 method for signature 'modelObjFit' print(x) ## S4 method for signature 'modelObjFit' residuals(object, ...) ## S4 method for signature 'modelObjFit' show(object) ## S4 method for signature 'modelObjFit' summary(object, ...)
## S4 method for signature 'modelObjFit' coef(object, ...) ## S4 method for signature 'modelObjFit' plot(x, y, ...) ## S4 method for signature 'modelObjFit' print(x) ## S4 method for signature 'modelObjFit' residuals(object, ...) ## S4 method for signature 'modelObjFit' show(object) ## S4 method for signature 'modelObjFit' summary(object, ...)
object |
An object of class modelObjFit |
... |
passed to underlying method defined for regression value object. |
x |
An object of class modelObjFit |
y |
ignored |
coef
: Extract Model Coefficients
plot
: X-Y plotting
print
: Print regression results
residuals
: Extract residuals
show
: Show regression results
summary
: Show summary results
fitObj
Object returned by the regression analysis
modelObj
Object of class modelObj
.
: Extracts regression step.
: Retrieve model.
: Retrieve regression method name.
: Retrieve arguments to be sent to regression method.
: Set arguments to be sent to regression method.
: Retrieve prediction method name.
: Retrieve arguments to be sent to prediction method.
: Set arguments to be sent to prediction method.
showClass("modelObjFit")
showClass("modelObjFit")
Predictions from the results of a fit object.
predict(object, ...) ## S4 method for signature 'modelObjFit' predict(object, newdata, ...)
predict(object, ...) ## S4 method for signature 'modelObjFit' predict(object, newdata, ...)
object |
An object of class |
newdata |
An object of class data.frame containing the variables in the model. |
... |
ignored |
Model predictions, the form of which depend on the regression analysis.
# generate data X <- matrix(rnorm(1000,0,1), ncol=4, dimnames=list(NULL,c("X1","X2","X3","X4"))) Y <- X %*% c(0.1, 0.2, 0.3, 0.4) + rnorm(250) X <- data.frame(X) # create modeling object using a formula mo <- buildModelObj(model=Y ~ X1 + X2 + X3 + X4, solver.method='lm') # fit model fit.obj <- fit(object=mo, data=X, response=Y) predict(fit.obj) predict(fit.obj, newdata = X[1:10,])
# generate data X <- matrix(rnorm(1000,0,1), ncol=4, dimnames=list(NULL,c("X1","X2","X3","X4"))) Y <- X %*% c(0.1, 0.2, 0.3, 0.4) + rnorm(250) X <- data.frame(X) # create modeling object using a formula mo <- buildModelObj(model=Y ~ X1 + X2 + X3 + X4, solver.method='lm') # fit model fit.obj <- fit(object=mo, data=X, response=Y) predict(fit.obj) predict(fit.obj, newdata = X[1:10,])
Retrieves method for prediction analysis
predictor(object, ...) ## S4 method for signature 'modelObj' predictor(object, ...) ## S4 method for signature 'modelObj' predictor(object, ...) ## S4 method for signature 'modelObjFit' predictor(object, ...)
predictor(object, ...) ## S4 method for signature 'modelObj' predictor(object, ...) ## S4 method for signature 'modelObj' predictor(object, ...) ## S4 method for signature 'modelObjFit' predictor(object, ...)
object |
A modelObj object |
... |
ignored |
An object of class character or function
Retrieves the arguments that are to be passed to the prediction method when called.
predictorArgs(object, ...) ## S4 method for signature 'modelObj' predictorArgs(object, ...) predictorArgs(object) <- value ## S4 replacement method for signature 'ANY,ANY' predictorArgs(object) <- value ## S4 replacement method for signature 'modelObj,list' predictorArgs(object) <- value ## S4 method for signature 'modelObjFit' predictorArgs(object, ...)
predictorArgs(object, ...) ## S4 method for signature 'modelObj' predictorArgs(object, ...) predictorArgs(object) <- value ## S4 replacement method for signature 'ANY,ANY' predictorArgs(object) <- value ## S4 replacement method for signature 'modelObj,list' predictorArgs(object) <- value ## S4 method for signature 'modelObjFit' predictorArgs(object, ...)
object |
A modelObj object |
... |
ignored |
value |
List to be stored in args |
A list
Retrieves method for regression analysis
solver(object, ...) ## S4 method for signature 'ANY' solver(object, ...) ## S4 method for signature 'modelObj' solver(object, ...) ## S4 method for signature 'modelObjFit' solver(object, ...)
solver(object, ...) ## S4 method for signature 'ANY' solver(object, ...) ## S4 method for signature 'modelObj' solver(object, ...) ## S4 method for signature 'modelObjFit' solver(object, ...)
object |
A modelObj object |
... |
ignored |
An object of class character or function
Retrieves the arguments that are to be passed to the regression method when called.
solverArgs(object, ...) ## S4 method for signature 'ANY' solverArgs(object, ...) ## S4 method for signature 'modelObj' solverArgs(object, ...) solverArgs(object) <- value ## S4 replacement method for signature 'ANY,ANY' solverArgs(object) <- value ## S4 replacement method for signature 'modelObj,list' solverArgs(object) <- value ## S4 method for signature 'modelObjFit' solverArgs(object, ...)
solverArgs(object, ...) ## S4 method for signature 'ANY' solverArgs(object, ...) ## S4 method for signature 'modelObj' solverArgs(object, ...) solverArgs(object) <- value ## S4 replacement method for signature 'ANY,ANY' solverArgs(object) <- value ## S4 replacement method for signature 'modelObj,list' solverArgs(object) <- value ## S4 method for signature 'modelObjFit' solverArgs(object, ...)
object |
A modelObj object |
... |
ignored |
value |
List to be stored in args |
A list