fastLmPure():RcppArmadilloを使ってみる

  • 線形回帰
data(trees, package="datasets")

fastLmPure( cbind(1, log(trees$Girth)), log(trees$Volume) )
fastLm( log(Volume) ~ log(Girth), data=trees)
lm(log(trees$Volume) ~ log(trees$Girth))  
> fastLmPure( cbind(1, log(trees$Girth)), log(trees$Volume) )
$coefficients
          [,1]
[1,] -2.353325
[2,]  2.199970

$stderr
           [,1]
[1,] 0.23066284
[2,] 0.08983455

$df.residual
[1] 29

> fastLm( log(Volume) ~ log(Girth), data=trees)

Call:
fastLm.formula(formula = log(Volume) ~ log(Girth), data = trees)

Coefficients:
(Intercept)  log(Girth) 
    -2.3533      2.2000 
> lm(log(trees$Volume) ~ log(trees$Girth))  

Call:
lm(formula = log(trees$Volume) ~ log(trees$Girth))

Coefficients:
     (Intercept)  log(trees$Girth)  
          -2.353             2.200 
library(rbenchmark)
microbenchmark(
  fastLmPure( cbind(1, log(trees$Girth)), log(trees$Volume) ),
  fastLm( log(Volume) ~ log(Girth), data=trees),
  lm(log(trees$Volume) ~ log(trees$Girth))
)
> microbenchmark(
+   fastLmPure( cbind(1, log(trees$Girth)), log(trees$Volume) ),
+   fastLm( log(Volume) ~ log(Girth), data=trees),
+   lm(log(trees$Volume) ~ log(trees$Girth))
+ )
Unit: microseconds
                                                      expr      min       lq    median
 fastLmPure(cbind(1, log(trees$Girth)), log(trees$Volume))  175.205  196.281  204.7115
            fastLm(log(Volume) ~ log(Girth), data = trees) 2813.534 2864.849 2920.3795
                  lm(log(trees$Volume) ~ log(trees$Girth)) 3405.123 3476.415 3531.3955
       uq      max neval
  221.205 2570.887   100
 3010.730 5548.262   100
 3657.118 6456.540   100