Title: | Feasible Solution Algorithm for Finding Best Subsets and Interactions |
---|---|
Description: | Assists in statistical model building to find optimal and semi-optimal higher order interactions and best subsets. Uses the lm(), glm(), and other R functions to fit models generated from a feasible solution algorithm. Discussed in Subset Selection in Regression, A Miller (2002). Applied and explained for least median of squares in Hawkins (1993) <doi:10.1016/0167-9473(93)90246-P>. The feasible solution algorithm comes up with model forms of a specific type that can have fixed variables, higher order interactions and their lower order terms. |
Authors: | Joshua Lambert [aut, cre], Liyu Gong [aut], Corrine Elliott [aut], Sarah Janse [ctb] |
Maintainer: | Joshua Lambert <[email protected]> |
License: | GPL-2 |
Version: | 0.9.6 |
Built: | 2024-11-05 03:41:19 UTC |
Source: | https://github.com/joshuawlambert/rfsa |
rFSA Criterion Function to compute Adjusted R-Squared.
adj.r.squared(model, name = "Adj R Squared")
adj.r.squared(model, name = "Adj R Squared")
model |
lm or glm fit to be passed. |
name |
passed to print.FSA |
rFSA Criterion Function to Allen's Press Statistic.
apress(model, name = "PRESS")
apress(model, name = "PRESS")
model |
lm or glm fit to be passed. |
name |
passed to print.FSA |
rFSA Criterion Function to compute the Bhattacharyya distance.
bdist(model, name = "B Distance")
bdist(model, name = "B Distance")
model |
lm or glm fit to be passed. |
name |
passed to print.FSA |
#To use Bhattacharyya Distance and FSA the response must be binary, and you must #be considering searching for two way continuous interactions. data(mtcars) fit<-FSA(formula = "am~gear*hp",data = mtcars, fitfunc = glm,family="binomial",m = 2,cores=1, interactions = TRUE,criterion = bdist,minmax = "max")
#To use Bhattacharyya Distance and FSA the response must be binary, and you must #be considering searching for two way continuous interactions. data(mtcars) fit<-FSA(formula = "am~gear*hp",data = mtcars, fitfunc = glm,family="binomial",m = 2,cores=1, interactions = TRUE,criterion = bdist,minmax = "max")
Model fitting function for FSA solutions
fitmodels(object, ...)
fitmodels(object, ...)
object |
FSA object to construct models on. |
... |
other parameters passed to lm or glm. See help(lm) or help(glm) for other potential arguements. |
list of FSA models that have been fitted.
#use mtcars package see help(mtcars) data(mtcars) colnames(mtcars) fit<-lmFSA(formula="mpg~hp*wt",data=mtcars,fixvar="hp", quad=FALSE,m=2,numrs=10,save_solutions=FALSE,cores=1) fitmodels(fit)
#use mtcars package see help(mtcars) data(mtcars) colnames(mtcars) fit<-lmFSA(formula="mpg~hp*wt",data=mtcars,fixvar="hp", quad=FALSE,m=2,numrs=10,save_solutions=FALSE,cores=1) fitmodels(fit)
Fitted Values for FSA solutions
## S3 method for class 'FSA' fitted(object, ...)
## S3 method for class 'FSA' fitted(object, ...)
object |
FSA object to get fitted values from. |
... |
other parameters passed to fitmodels or fitted function. See help(fitmodels) or help(fitted) for assistance. |
list of fitted values from each FSA model.
#use mtcars package see help(mtcars) data(mtcars) colnames(mtcars) fit<-lmFSA(formula="mpg~hp*wt",data=mtcars,fixvar="hp", quad=FALSE,m=2,numrs=10,save_solutions=FALSE,cores=1) fitted(fit)
#use mtcars package see help(mtcars) data(mtcars) colnames(mtcars) fit<-lmFSA(formula="mpg~hp*wt",data=mtcars,fixvar="hp", quad=FALSE,m=2,numrs=10,save_solutions=FALSE,cores=1) fitted(fit)
A function using a Feasible Solution Algorithm to find a set of feasible solutions for a statistical model of a specific form that could include mth-order interactions (Note that these solutions are optimal in the sense that no one swap to any of the variables will increase the criterion function.)
FSA( formula, data, fitfunc = lm, fixvar = NULL, quad = FALSE, m = 2, numrs = 1, cores = 1, interactions = T, criterion = AIC, minmax = "min", checkfeas = NULL, var4int = NULL, min.nonmissing = 1, return.models = FALSE, fix.formula = NULL, ... ) lmFSA(...) glmFSA(...)
FSA( formula, data, fitfunc = lm, fixvar = NULL, quad = FALSE, m = 2, numrs = 1, cores = 1, interactions = T, criterion = AIC, minmax = "min", checkfeas = NULL, var4int = NULL, min.nonmissing = 1, return.models = FALSE, fix.formula = NULL, ... ) lmFSA(...) glmFSA(...)
formula |
an object of class "formula" (or one that can be coerced to that class): a symbolic description of the model to be fitted. |
data |
a data frame, list or environment (or object coercible by as.data.frame to a data frame) containing the variables in the model. |
fitfunc |
the method that should be used to fit the model. For Example: lm, glm, or other methods that rely on formula, data, and other inputs. |
fixvar |
variable(s) to fix in the model. Usually a covariate that should always be included (Example: Age, Sex). Will still consider it with interactions. Default is NULL. |
quad |
Include quadratic terms or not. Logical. |
m |
order of terms to include. If interactions is set to TRUE then m is the order of interactions to be considered. For Subset selection (interaction=F), m is the size of the subset to examine. Defaults to 2. |
numrs |
number of random starts to perform. |
cores |
number of cores to use while running. Note: Windows can only use 1 core. See mclapply for details. If function detects a Windows user it will automatically set cores=1. |
interactions |
whether to include interactions in model. Defaults to TRUE. |
criterion |
which criterion function to either maximize or minimize. For linear models one can use: r.squared, adj.r.squared, cv5.lmFSA (5 Fold Cross Validation error), cv10.lmFSA (10 Fold Cross Validation error), apress (Allen's Press Statistic), int.p.val (Interaction P-value), AIC, BIC. |
minmax |
whether to minimize or maximize the criterion function |
checkfeas |
vector of variables that could be a feasible solution. These variables will be used as the last random start. |
var4int |
specification of which variables to check for marginal feasiblilty. Default is NULL |
min.nonmissing |
the combination of predictors will be ignored unless this many of observations are not missing |
return.models |
bool value to specify whether return all the fitted models which have been checked |
fix.formula |
... |
... |
other arguments passed to fitfunc. |
matrix of results
FSA
: find best set of variables for statistical models
lmFSA
: alias for FSA(fitfunc=lm,...)
glmFSA
: alias for FSA(fitfunc=glm,...)
N <- 10 #number of obs P <- 100 #number of variables data <- data.frame(matrix(rnorm(N*(P+1)), nrow = N, ncol = P+1)) sln <- FSA(formula = "X101~1", data = data, cores = 1, m = 2, interactions = FALSE, criterion = AIC, minmax = "min", numrs = 10) sln
N <- 10 #number of obs P <- 100 #number of variables data <- data.frame(matrix(rnorm(N*(P+1)), nrow = N, ncol = P+1)) sln <- FSA(formula = "X101~1", data = data, cores = 1, m = 2, interactions = FALSE, criterion = AIC, minmax = "min", numrs = 10) sln
rFSA Criterion Function to compute Liklihood Ratio Test Statistics p-value for the largest order interation term.
int.p.val(model, name = "Interaction P-Value")
int.p.val(model, name = "Interaction P-Value")
model |
lm or glm fit to be passed. |
name |
passed to print.FSA |
List all included Criteria function for lmFSA and glmFSA.
list.criterion()
list.criterion()
list of functions and whether lmFSA or glmFSA work with those functions.
list.criterion()
list.criterion()
Return maximum absolute residual from a model
max_abs_resid(model)
max_abs_resid(model)
model |
model obj |
Finds the swaps available given a current position given previous picks.
nextswap(curpos, n, prevpos, quad)
nextswap(curpos, n, prevpos, quad)
curpos |
A vector of length greater than 2 of what current explantory varialbes are being used in the model. |
n |
The number of explanatory variables in available to swap. |
prevpos |
A vector of previous best spots |
quad |
Whether to include quadratic terms. ie (x1*x1) as potential swaps. |
a matrix with the possible forms by column.
A function using a Feasible Solution Algorithm to estimate a set of models which are on the Pareto frontiers for chosen criteria
pFSA( numFronts = 2, pselExpr = NULL, plot.it = TRUE, formula, data, fitfunc = lm, fixvar = NULL, quad = FALSE, m = 2, numrs = 1, cores = 1, interactions = T, criterion = AIC, minmax = "min", checkfeas = NULL, var4int = NULL, min.nonmissing = 1, return.models = FALSE, fix.formula = NULL, ... )
pFSA( numFronts = 2, pselExpr = NULL, plot.it = TRUE, formula, data, fitfunc = lm, fixvar = NULL, quad = FALSE, m = 2, numrs = 1, cores = 1, interactions = T, criterion = AIC, minmax = "min", checkfeas = NULL, var4int = NULL, min.nonmissing = 1, return.models = FALSE, fix.formula = NULL, ... )
numFronts |
integer number of estimated frontiers to return |
pselExpr |
expression used by function psel to estimate pareto frontiers. help(psel). |
plot.it |
TRUE/FALSE for whether to plot the pareto frontiers |
formula |
an object of class "formula" (or one that can be coerced to that class): a symbolic description of the model to be fitted. |
data |
a data frame, list or environment (or object coercible by as.data.frame to a data frame) containing the variables in the model. |
fitfunc |
the method that should be used to fit the model. For Example: lm, glm, or other methods that rely on formula, data, and other inputs. |
fixvar |
variable(s) to fix in the model. Usually a covariate that should always be included (Example: Age, Sex). Will still consider it with interactions. Default is NULL. |
quad |
Include quadratic terms or not. Logical. |
m |
order of terms to include. If interactions is set to TRUE then m is the order of interactions to be considered. For Subset selection (interaction=F), m is the size of the subset to examine. Defaults to 2. |
numrs |
number of random starts to perform. |
cores |
number of cores to use while running. Note: Windows can only use 1 core. See mclapply for details. If function detects a Windows user it will automatically set cores=1. |
interactions |
whether to include interactions in model. Defaults to TRUE. |
criterion |
which criterion function to either maximize or minimize. For linear models one can use: r.squared, adj.r.squared, cv5.lmFSA (5 Fold Cross Validation error), cv10.lmFSA (10 Fold Cross Validation error), apress (Allen's Press Statistic), int.p.val (Interaction P-value), AIC, BIC. |
minmax |
whether to minimize or maximize the criterion function |
checkfeas |
vector of variables that could be a feasible solution. These variables will be used as the last random start. |
var4int |
specification of which variables to check for marginal feasiblilty. Default is NULL |
min.nonmissing |
the combination of predictors will be ignored unless this many of observations are not missing |
return.models |
bool value to specify whether return all the fitted models which have been checked |
fix.formula |
... |
... |
see arguments taken by function FSA or other functions. help(FSA). |
list of a matrix of all models obtained from FSA (fits) and their criteria. Also a matrix of the estimated frontiers that were requested. The Key column in fits, and pbound refers to the column number of the variables contined in the model fit. For instance, Key="42,96" would refer to the model which contains the variable in the 42nd column and 96th column of the designated dataset.
N <- 1000 #number of obs P <- 100 #number of variables data <- data.frame(matrix(rnorm(N*(P+1)), nrow = N, ncol = P+1)) sln <- pFSA(formula = "X101~1", data = data, m = 2, criterion = c(max_abs_resid,r.squared), minmax = c("min","max"),numrs = 10,numFronts = 2, pselExpr =rPref::low(max_abs_resid)*rPref::high(r.squared),plot.it = TRUE)
N <- 1000 #number of obs P <- 100 #number of variables data <- data.frame(matrix(rnorm(N*(P+1)), nrow = N, ncol = P+1)) sln <- pFSA(formula = "X101~1", data = data, m = 2, criterion = c(max_abs_resid,r.squared), minmax = c("min","max"),numrs = 10,numFronts = 2, pselExpr =rPref::low(max_abs_resid)*rPref::high(r.squared),plot.it = TRUE)
Diagnostic Plots for FSA solutions
## S3 method for class 'FSA' plot(x, ask = F, easy = T, ...)
## S3 method for class 'FSA' plot(x, ask = F, easy = T, ...)
x |
FSA object to see diagnostic plots on. |
ask |
logical; if TRUE, the user is asked before each plot. See help(plot.lm). |
easy |
logical; should diagnostic plots be presented in easy to read format? |
... |
arguments to be passed to other functions. |
diagnostic plots to plot window.
#use mtcars package see help(mtcars) data(mtcars) colnames(mtcars) fit<-lmFSA(formula="mpg~hp*wt",data=mtcars,fixvar="hp", quad=FALSE,m=2,numrs=10,save_solutions=FALSE,cores=1) plot(x=fit)
#use mtcars package see help(mtcars) data(mtcars) colnames(mtcars) fit<-lmFSA(formula="mpg~hp*wt",data=mtcars,fixvar="hp", quad=FALSE,m=2,numrs=10,save_solutions=FALSE,cores=1) plot(x=fit)
Prediction function for FSA solutions
## S3 method for class 'FSA' predict(object, ...)
## S3 method for class 'FSA' predict(object, ...)
object |
FSA object to conduct predictions on. |
... |
other parameters passed to fitmodels or predict functions. See help(fitmodels) or help(predict) for assistance. |
list of predicted values from each FSA model.
#use mtcars package see help(mtcars) data(mtcars) colnames(mtcars) fit<-lmFSA(formula="mpg~hp*wt",data=mtcars,fixvar="hp", quad=FALSE,m=2,numrs=10,save_solutions=FALSE,cores=1) predict(fit) predict(fit,newdata=mtcars[1:15,])
#use mtcars package see help(mtcars) data(mtcars) colnames(mtcars) fit<-lmFSA(formula="mpg~hp*wt",data=mtcars,fixvar="hp", quad=FALSE,m=2,numrs=10,save_solutions=FALSE,cores=1) predict(fit) predict(fit,newdata=mtcars[1:15,])
Printing function for FSA solutions
## S3 method for class 'FSA' print(x, ...)
## S3 method for class 'FSA' print(x, ...)
x |
FSA object to print details about. |
... |
arguments to be passed to other functions. |
list of Feasible Solution Formula's, Original Fitted model formula and criterion function and times converged to details.
#use mtcars package see help(mtcars) data(mtcars) colnames(mtcars) fit<-lmFSA(formula="mpg~hp*wt",data=mtcars,fixvar="hp", quad=FALSE,m=2,numrs=10,save_solutions=FALSE,cores=1) print(fit)
#use mtcars package see help(mtcars) data(mtcars) colnames(mtcars) fit<-lmFSA(formula="mpg~hp*wt",data=mtcars,fixvar="hp", quad=FALSE,m=2,numrs=10,save_solutions=FALSE,cores=1) print(fit)
Computes quasi-likelihood under the independence criterion (QICu)
QICu.geeglm(gee.obj)
QICu.geeglm(gee.obj)
gee.obj |
geeglm obj |
rFSA Criterion Function to compute R squared.
r.squared(model, name = "R Squared")
r.squared(model, name = "R Squared")
model |
lm or glm fit to be passed. |
name |
passed to print.FSA |
rFSA Criterion Function to compute Root Mean Squared Error.
rmse(model, name = "RMSE")
rmse(model, name = "RMSE")
model |
lm or glm fit to be passed. |
name |
passed to print.FSA |
Summary function for FSA solutions
## S3 method for class 'FSA' summary(object, ...)
## S3 method for class 'FSA' summary(object, ...)
object |
FSA object to see summaries on. |
... |
arguments to be passed to other functions. |
list of summarized lm or glm output.
#use mtcars package see help(mtcars) data(mtcars) colnames(mtcars) fit<-lmFSA(formula="mpg~hp*wt",data=mtcars,fixvar="hp", quad=FALSE,m=2,numrs=10,save_solutions=FALSE,cores=1) summary(fit)
#use mtcars package see help(mtcars) data(mtcars) colnames(mtcars) fit<-lmFSA(formula="mpg~hp*wt",data=mtcars,fixvar="hp", quad=FALSE,m=2,numrs=10,save_solutions=FALSE,cores=1) summary(fit)
Finds the swaps available given a current position.
swaps(cur, n, quad = FALSE, yindex)
swaps(cur, n, quad = FALSE, yindex)
cur |
A vector of length greater than 2 of what current explantory varialbes are being used in the model. |
n |
The number of explanatory variables in available to swap. |
quad |
Whether to include quadratic terms. ie (x1*x1) as potential swaps. |
yindex |
index of response variable. |
a matrix with the possible forms by column.
A function for termwise feasiblity
twFSA( formula, data, fitfunc = lm, fixvar = NULL, quad = FALSE, cores = 1, criterion = AIC, minmax = "min", checkfeas = NULL, var4int = NULL, min.nonmissing = 1, ... )
twFSA( formula, data, fitfunc = lm, fixvar = NULL, quad = FALSE, cores = 1, criterion = AIC, minmax = "min", checkfeas = NULL, var4int = NULL, min.nonmissing = 1, ... )
formula |
an object of class "formula" (or one that can be coerced to that class): a symbolic description of the model to be fitted. |
data |
a data frame, list or environment (or object coercible by as.data.frame to a data frame) containing the variables in the model. |
fitfunc |
the method that should be used to fit the model. For Example: lm, glm, or other methods that rely on formula, data, and other inputs. |
fixvar |
variable(s) to fix in the model. Usually a covariate that should always be included (Example: Age, Sex). Will still consider it with interactions. Default is NULL. |
quad |
Include quadratic terms or not. Logical. |
cores |
number of cores to use while running. Note: Windows can only use 1 core. See mclapply for details. If function detects a Windows user it will automatically set cores=1. |
criterion |
which criterion function to either maximize or minimize. For linear models one can use: r.squared, adj.r.squared, cv5.lmFSA (5 Fold Cross Validation error), cv10.lmFSA (10 Fold Cross Validation error), apress (Allen's Press Statistic), int.p.val (Interaction P-value), AIC, BIC. |
minmax |
whether to minimize or maximize the criterion function |
checkfeas |
vector of variables that could be a feasible solution. These variables will be used as the last random start. |
var4int |
specification of which variables to check for marginal feasiblilty. Default is NULL |
min.nonmissing |
the combination of predictors will be ignored unless this many of observations are not missing |
... |
other arguments passed to fitfunc. |
matrix of results
rFSA function to compute the maximum value from a vector with NA's.
which.max.na(vec)
which.max.na(vec)
vec |
Vector to be passed. |
rFSA function to compute the minimum value from a vector with NA's.
which.min.na(vec)
which.min.na(vec)
vec |
Vector to be passed. |