Learn R Programming

metR (version 0.3.0)

FitLm: Fast estimates of linear regression

Description

Computes a linear regression with stats::.lm.fit and returns the estimate and, optionally, standard error for each regressor.

Usage

FitLm(y, ..., se = FALSE)

Arguments

y

numeric vector of observations to model

...

numeric vectors of variables used in the modeling

se

logical indicating whether to compute the standard error

Value

a list with elements

term

the name of the regressor

estimate

estimate of the regression

std.error

standard error

df

degrees of freedom

If there's no complete cases in the regression, NAs are returned with no warning.

Examples

Run this code
# NOT RUN {
# Linear trend with "signficant" areas shaded with points
library(data.table)
library(ggplot2)
system.time({
  regr <- geopotential[, FitLm(gh, date, se = TRUE), by = .(lon, lat)]
})

ggplot(regr[term != "(intercept)"], aes(lon, lat)) +
    geom_contour(aes(z = estimate, color = ..level..)) +
    stat_subset(aes(subset = abs(estimate) > 2*std.error), size = 0.05)

# Using stats::lm() is much slower and with no names.
# }
# NOT RUN {
system.time({
  regr <- geopotential[, coef(lm(gh ~ date)), by = .(lon, lat)]
})
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab