snpRF (version 0.4)

snpRF: Classification with Random Forest for SNP Data

Description

snpRF implements Breiman's random forest algorithm (based on Breiman and Cutler's original Fortran code) for classification and regression. It can also be used in unsupervised mode for assessing proximities among data points. This is a modified version of the randomForest function in the randomForest package addressing issues of X-chromosome SNP importance bias by simulating the process of X-inactivation.

Usage

snpRF(x.autosome=NULL,x.xchrom=NULL, xchrom.names=NULL, x.covar=NULL, y, xtest.autosome=NULL,xtest.xchrom=NULL, xtest.covar=NULL, ytest=NULL, ntree=500, mtry=floor(sqrt(sum(c(ncol(x.autosome),ncol(x.xchrom)/2, ncol(x.covar))))), replace=TRUE, classwt=NULL, cutoff, strata, sampsize = if (replace) max(c(nrow(x.autosome),nrow(x.xchrom), nrow(x.covar))) else ceiling(.632*max(c(nrow(x.autosome), nrow(x.xchrom),nrow(x.covar)))), nodesize = 1, maxnodes=NULL, importance=FALSE, localImp=FALSE, proximity, oob.prox=proximity, norm.votes=TRUE, do.trace=FALSE, keep.forest=!is.null(y) && (is.null(xtest.autosome) & is.null(xtest.xchrom) & is.null(xtest.covar)), keep.inbag=FALSE, ...)
"print"(x, ...)

Arguments

x
a snpRF object.
x.autosome
A matrix of autosomal markers with each column corresponding to a SNP coded as the count of a particular allele (i.e. 0,1 or 2), and each row corresponding to a sample/individual.
x.xchrom
A matrix of X chromosome markers, each marker coded as two adjacent columns, alleles of a marker are coded as 0 or 1 for carrying a particular allele. Although males only have one X-chromosome, their markers are coded as 2 columns as well, the second column being a duplicate of the first. Each row of this matrix corresponsponds to a sample/individual. This data must be phased in chromosomal order.
xchrom.names
A vector of names for markers (1 name per marker) in the x.xchrom matrix ordered in the same manner as markers in x.xchrom.
x.covar
A matrix of covariates, each column being a different covariate and each row, a sample/individual.
y
A response vector. Must be a factor, regression has not been implemented. If omitted, snpRF will run in unsupervised mode.
xtest.autosome
a matrix (like x.autosome) containing predictors for the test set.
xtest.xchrom
a matrix (like x.xchrom) containing predictors for the test set.
xtest.covar
a matrix (like x.covar) containing predictors for the test set.
ytest
response for the test set.
ntree
Number of trees to grow. This should not be set to too small a number, to ensure that every input row gets predicted at least a few times.
mtry
Number of variables randomly sampled as candidates at each split. Note that the default values are different for classification (sqrt(p) where p is number of variables in: x.autosome, half of x.xchrom, and x.covar)
replace
Should sampling of cases be done with or without replacement?
classwt
Priors of the classes. Need not add up to one.
cutoff
A vector of length equal to number of classes. The `winning' class for an observation is the one with the maximum ratio of proportion of votes to cutoff. Default is 1/k where k is the number of classes (i.e., majority vote wins).
strata
A (factor) variable that is used for stratified sampling.
sampsize
Size(s) of sample to draw. For classification, if sampsize is a vector of the length the number of strata, then sampling is stratified by strata, and the elements of sampsize indicate the numbers to be drawn from the strata.
nodesize
Minimum size of terminal nodes. Setting this number larger causes smaller trees to be grown (and thus take less time).
maxnodes
Maximum number of terminal nodes trees in the forest can have. If not given, trees are grown to the maximum possible (subject to limits by nodesize). If set larger than maximum possible, a warning is issued.
importance
Should importance of predictors be assessed?
localImp
Should casewise importance measure be computed? (Setting this to TRUE will override importance.)
proximity
Should proximity measure among the rows be calculated?
oob.prox
Should proximity be calculated only on ``out-of-bag'' data?
norm.votes
If TRUE (default), the final result of votes are expressed as fractions. If FALSE, raw vote counts are returned (useful for combining results from different runs). Ignored for regression.
do.trace
If set to TRUE, give a more verbose output as snpRF is run. If set to some integer, then running output is printed for every do.trace trees.
keep.forest
If set to FALSE, the forest will not be retained in the output object. If xtest is given, defaults to FALSE.
keep.inbag
Should an n by ntree matrix be returned that keeps track of which samples are ``in-bag'' in which trees (but not how many times, if sampling with replacement)
...
optional parameters to be passed to the low level function snpRF.

Value

An object of class snpRF, which is a list with the following components:
call
the original call to snpRF
type
classification, or unsupervised.
predicted
the predicted values of the input data based on out-of-bag samples.
importance
a matrix with nclass + 2 columns. The first nclass columns are the class-specific measures computed as mean descrease in accuracy. The nclass + 1st column is the mean descrease in accuracy over all classes. The last column is the mean decrease in Gini index.
importanceSD
The ``standard errors'' of the permutation-based importance measure. For classification, a p by nclass + 1 matrix corresponding to the first nclass + 1 columns of the importance matrix. For regression, a length p vector.
localImp
a p by n matrix containing the casewise importance measures, the [i,j] element of which is the importance of i-th variable on the j-th case. NULL if localImp=FALSE.
ntree
number of trees grown.
mtry
number of predictors sampled for spliting at each node.
forest
(a list that contains the entire forest; NULL if snpRF is run in unsupervised mode or if keep.forest=FALSE.
err.rate
(classification only) vector error rates of the prediction on the input data, the i-th element being the (OOB) error rate for all trees up to the i-th.
confusion
(classification only) the confusion matrix of the prediction (based on OOB data).
votes
(classification only) a matrix with one row for each input data point and one column for each class, giving the fraction or number of (OOB) `votes' from the random forest.
oob.times
number of times cases are `out-of-bag' (and thus used in computing OOB error estimate)
proximity
if proximity=TRUE when snpRF is called, a matrix of proximity measures among the input (based on the frequency that pairs of data points are in the same terminal nodes).
test
if test set is given (through the xtest or additionally ytest arguments), this component is a list which contains the corresponding predicted, err.rate, confusion, votes for the test set. If proximity=TRUE, there is also a component, proximity, which contains the proximity among the test set as well as proximity between test and training data.

References

Breiman, L. (2001), Random Forests, Machine Learning 45(1), 5-32.

Breiman, L (2002), ``Manual On Setting Up, Using, And Understanding Random Forests V3.1'', http://oz.berkeley.edu/users/breiman/Using_random_forests_V3.1.pdf.

Jenkins, G., Biernacka J., Winham S., Random forest for genetic analysis: Integrating the X chromosome; (Abstract #1853). Presented at the 64th Annual Meeting of The American Society of Human Genetics, Date, October 21, 2014 in San Diego, CA.

See Also

predict.snpRF, varImpPlot

Examples

Run this code
## Classification:
data(snpRFexample)
set.seed(71)
eg.rf <- snpRF(x.autosome=autosome.snps,x.xchrom=xchrom.snps,
               xchrom.names=xchrom.snps.names,x.covar=covariates, 
               y=phenotype,importance=TRUE, proximity=TRUE)

print(eg.rf)
## Look at variable importance:
round(importance(eg.rf), 2)
## Do MDS on 1 - proximity:
eg.mds <- cmdscale(1 - eg.rf$proximity, eig=TRUE)

print(eg.mds$GOF)



## Grow no more than 4 nodes per tree:
(treesize(snpRF(x.autosome=autosome.snps,x.xchrom=xchrom.snps,
                xchrom.names=xchrom.snps.names,x.covar=covariates, 
                y=phenotype, maxnodes=4, ntree=30)))

Run the code above in your browser using DataLab