Skip to contents

Mirrors the C# LinearRegression class of the Numerics library: estimates Y = alpha + beta*X + e, e ~ N(0, sigma), via SVD.

Usage

linear_regression(x, y, intercept = TRUE)

Arguments

x

a numeric matrix of predictors with one row per observation, or a numeric vector for a single predictor.

y

numeric vector of responses, one per row of x.

intercept

whether to fit an intercept. Default TRUE.

Value

a corehydro_lm list with coefficients, standard_errors, covariance, residuals, r_squared, adj_r_squared, sigma, df, and n. covariance is the coefficient covariance matrix, i.e. sqrt(diag(covariance)) equals standard_errors; the underlying C# LinearRegression.Covariance is the unscaled cross-product term ((X'X)^-1), scaled here by sigma^2 to match standard_errors.

Examples

x <- cbind(c(1, 2, 3, 4, 5), c(2, 1, 4, 3, 5))
y <- c(3.1, 4.2, 8.1, 9.2, 13.0)
fit <- linear_regression(x, y)
coef(fit)
#> (Intercept)          x1          x2 
#>  -0.3800000   1.8666667   0.7666667