Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

speedlm

Fitting Linear Models to Large Data Sets


Description

The functions of class 'speedlm' may speed up the fitting of LMs to large data sets. High performances can be obtained especially if R is linked against an optimized BLAS, such as ATLAS.

Usage

# S3 method of class 'data.frame'
speedlm(formula, data, weights = NULL, offset = NULL, sparse = NULL, 
        set.default = list(), method=c('eigen','Cholesky','qr'), 
        model = FALSE, y = FALSE, fitted = FALSE, subset=NULL, ...)

# S3 method of class 'matrix'
speedlm.fit(y, X, intercept = FALSE, offset = NULL, row.chunk = NULL, 
            sparselim = 0.9, camp = 0.01, eigendec = TRUE, 
            tol.solve = .Machine$double.eps, sparse = NULL, tol.values = 1e-07, 
            tol.vectors = 1e-07, method=c('eigen','Cholesky','qr'), ...)

speedlm.wfit(y, X, w, intercept = FALSE, offset = NULL, row.chunk = NULL, 
             sparselim = 0.9, camp = 0.01, eigendec = TRUE, 
             tol.solve = .Machine$double.eps, sparse = NULL, tol.values = 1e-07, 
             tol.vectors = 1e-07, method=c('eigen','Cholesky','qr'), ...)
                      
# S3 method of class 'speedlm' (object) and 'data.frame' (data)                    
## S3 method for class 'speedlm'
update(object, formula, data, add=TRUE, evaluate=TRUE, 
                           subset=NULL, offset=NULL, weights=NULL,...)

# S3 method of class 'speedlm' (object) and 'data.frame' (data)                    
updateWithMoreData(object, data, weights = NULL, offset = NULL, sparse = NULL, 
                   all.levels = FALSE, set.default = list(), subset=NULL,...)

Arguments

Most of arguments are the same of functions lm but with some difference.

formula

the same of function lm.

data

the same of function lm, but it must always specified.

weights

the same of function lm, but it must be specified as data$weights.

w

the same of weights.

intercept

a logical value which indicates if an intercept is used.

offset

the same of function lm.

X

the same of x in function lm.

y

the same of lm,lm.wfit and lm.fit .

sparse

logical. Is the model matrix sparse? By default is NULL, so a quickly sample survey will be made.

set.default

a list in which to specify the parameters to pass to the functions cp, control and is.sparse.

sparselim

a value in the interval [0, 1]. It indicates the minimal proportion of zeroes, in the model matrix X, in order to consider X as sparse.

camp

see function is.sparse.

eigendec

logical. Do you want to investigate on rank of X? You may set it to false if you are sure that X is full rank.

row.chunk

an integer, see the function cp for details.

tol.solve

see function solve.

tol.values

see function control.

tol.vectors

see function control.

method

see function control.

object

an object of class 'speedlm'.

all.levels

are all levels of eventual factors present in each data chunk? If so, set all.levels to true to speed up the fitting.

model

logical. Should the model frame be returned?

fitted

logical. Should the fitted values be returned?

subset

the same of function lm

add

logical. Are additional data coming from a new chunk provided?

evaluate

logical. If true evaluate the new call else return the call.

...

further optional arguments.

Details

Unlikely from lm or biglm, the functions of class 'speedlm' do not use the QR decomposition but directly solve the normal equations. Further, the most recent version of the package include method 'qr'. However such qr decomposition is not applied directly on matrix X, but on X'WX. In some extreme case, this might have some problem of numerical stability but may take advantage from the use of an optimized BLAS. The memory size of an object of class 'speedlm' is O(p^2), where p is the number of covariates. If an optimized BLAS library is not installed, an attempt to speed up calculations may be done by setting row.chunk to some value, usually less than 1000, in set.default. See the function cp for details. Factors are permitted without limitations.
In the most recent versions, function update.speedlm is now a wrapper to call either updateWithMoreData (the new name of the old update.speedlm, for additional data chunks), or update from package stats.

Value

coefficients

the estimated coefficients.

df.residual

the residual degrees of freedom.

XTX

the product X'X (weighted, if the case).

A

the product X'X (weighted, if the case) not checked for singularity.

Xy

the product X'y (weighted, if the case).

ok

the set of column indeces of the model matrix where the model has been fitted.

rank

the numeric rank of the fitted linear model.

pivot

see the function control.

RSS

the estimated residual sums of squares of the fitted model.

sparse

a logical value indicating if the model matrix is sparse.

deviance

the estimated deviance of the fitted model.

weigths

the weights used in the last updating.

zero.w

the number of non-zero weighted observations.

nobs

the number of observations.

nvar

the number of independent variables.

terms

the terms object used.

intercept

a logical value which indicates if an intercept has been used.

call

the matched call.

model

Either NULL or the model frame, if model was previously set to TRUE.

y

Either NULL or the response variable, if y was previously set to TRUE.

fitted.values

Either NULL or the fitted values, if fitted was previously set to TRUE.

offset

the model offset.

...

others values necessary to update the estimation.

Note

All the above functions make an object of class 'speedlm'.

Author(s)

Marco Enea, with contribution from Ronen Meiri.

References

Enea, M. (2009) Fitting Linear Models and Generalized Linear Models With Large Data Sets in R. In book of short papers, conference on “Statistical Methods for the analysis of large data-sets”, Italian Statistical Society, Chieti-Pescara, 23-25 September 2009, 411-414.

Klotz, J.H. (1995) Updating Simple Linear Regression. Statistica Sinica, 5, 399-403.

Bates, D. (2009) Comparing Least Square Calculations. Technical report. Available at https://CRAN.R-project.org/package=Matrix/vignettes/Comparisons.pdf

Lumley, T. (2009) biglm: bounded memory linear and generalized linear models. R package version 0.7 https://CRAN.R-project.org/package=biglm.

See Also

Examples

## Not run: 
n <- 1000
k <- 3
y <- rnorm(n)
x <- round(matrix(rnorm(n * k), n, k), digits = 3)
colnames(x) <- c("s1", "s2", "s3") 
da <- data.frame(y, x)
do1 <- da[1:300,]
do2 <- da[301:700,]
do3 <- da[701:1000,]

m1 <- speedlm(y ~ s1 + s2 + s3, data = do1)
m1 <- update(m1, data = do2)
m1 <- update(m1, data = do3)

m2 <- lm(y ~ s1 + s2 + s3, data = da)
summary(m1)
summary(m2)

## End(Not run)

## Not run: 
  # as before but recursively
  make.data <- function(filename, chunksize,...){       
    conn <- NULL
    function(reset=FALSE, header=TRUE){
      if(reset){
        if(!is.null(conn)) close(conn)
         conn<<-file(filename,open="r") 
      } else{
        rval <- read.table(conn, nrows=chunksize,header=header,...)
        if (nrow(rval)==0) {
          close(conn)
          conn<<-NULL
          rval<-NULL
        }
        return(rval)
      }
    }
  }

  write.table(da,"da.txt",col.names=TRUE,row.names=FALSE,quote=FALSE) 
  x.names <- c("s1", "s2", "s3")
  dat <- make.data("da.txt",chunksize=300,col.names=c("y",x.names))
  dat(reset=TRUE) 
  da2 <- dat(reset=FALSE)
  
  # the first model runs on the first 300 rows.
  m3 <- speedlm(y ~ s1 + s2 + s3, data=da2)

  # the last three models run on the subsequent 300, 300 and 100 rows, respectively
  for (i in 1:3){
    da2 <- dat(reset=FALSE, header=FALSE)
    m3 <- update(m3, data=da2, add=TRUE)
  }  
  all.equal(coef(m1),coef(m3))  
  file.remove("da.txt")

## End(Not run)

speedglm

Fitting Linear and Generalized Linear Models to Large Data Sets

v0.3-3
GPL
Authors
Marco Enea [aut, cre], Ronen Meiri [ctb] (on behalf of DMWay Analytics LTD), Tomer Kalimi [ctb] (on behalf of DMWay Analytics LTD)
Initial release
2020-12-09

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.