Why does fastLm()
return results when I run regressions with one observation?
In the following, why aren't the lm()
and fastLm()
results equal?
library(Rcpp)
library(RcppArmadillo)
library(data.table)
set.seed(1)
DT <- data.table(y = rnorm(5), x1 = rnorm(5), x2 = rnorm(5), my.key = 1:5)
# y x1 x2 my.key
# 1: -0.6264538 -0.8204684 1.5117812 1
# 2: 0.1836433 0.4874291 0.3898432 2
# 3: -0.8356286 0.7383247 -0.6212406 3
# 4: 1.5952808 0.5757814 -2.2146999 4
# 5: 0.3295078 -0.3053884 1.1249309 5
lm(y ~ 1 + x1 + x2, data = DT[my.key == 1])
# Coefficients:
# (Intercept) x1 x2
# -0.6265 NA NA
fastLm(X = model.matrix(y ~ 1 + x1 + x2, data = DT[my.key == 1]), y = DT[my.key == 1]$y)
# Coefficients:
# (Intercept) x1 x2
# -0.15825 0.12984 -0.23924
model.matrix(y ~ 1 + x1 + x2, data = DT[my.key == 1])
# (Intercept) x1 x2
# 1 -0.8204684 1.511781
# attr(,"assign")
# [1] 0 1 2
DT[my.key == 1]$y
# [1] -0.6264538
When I use the whole DT
the results are equal:
all.equal(fastLm(X = model.matrix(y ~ 1 + x1 + x2, data = DT), y = DT$y)$coef,
lm.fit(x = model.matrix(y ~ 1 + x1 + x2, data = DT), y = DT$y)$coef)
# [1] TRUE
From the RcppArmadillo
library with a modified fLmTwoCasts I also get the same behavior:
src <- '
Rcpp::List fLmTwoCastsOnlyCoefficients(Rcpp::NumericMatrix Xr, Rcpp::NumericVector yr) {
int n = Xr.nrow(), k = Xr.ncol();
arma::mat X(Xr.begin(), n, k, false);
arma::colvec y(yr.begin(), yr.size(), false);
arma::colvec coef = arma::solve(X, y);
return Rcpp::List::create(Rcpp::Named("coefficients")=trans(coef));
}
'
cppFunction(code=src, depends="RcppArmadillo")
XX <- model.matrix(y ~ 1 + x1 + x2, data = DT[my.key == 1])
YY <- DT[my.key == 1]$y
fLmTwoCastsOnlyCoefficients(XX, YY)$coef
# [,1] [,2] [,3]
# [1,] -0.1582493 0.1298386 -0.2392384
Using the whole DT
the coefficients are identical as they should:
lm(y ~ 1 + x1 + x2, data = DT)$coef
# (Intercept) x1 x2
# 0.2587933 -0.7709158 -0.6648270
XX.whole <- model.matrix(y ~ 1 + x1 + x2, data = DT)
YY.whole <- DT$y
fLmTwoCastsOnlyCoefficients(XX.whole, YY.whole)
# [,1] [,2] [,3]
# [1,] 0.2587933 -0.7709158 -0.664827