c++ - RcppArmadillo 中的 fastLm 和 fastLmPure 函数之间的区别

标签 c++ r rcpp armadillo

这是一个例子:

require(Rcpp)
require(RcppArmadillo)
require(zoo)
require(repmis)

myData <- source_DropboxData(file = "example.csv", 
                              key = "cbrmkkbssu5bn96", sep = ",", header = TRUE)

dolm = function(x) coef(fastLmPure(as.matrix(x[,2]), x[,1]))

myCoef = rollapply(myData, 260, dolm, by.column = FALSE)

summary(myCoef) # 80923 NA's

dolm2 = function(x) coef(fastLm(x[,1] ~ x[,2] + 0, data = as.data.frame(x)))

myCoef2 = rollapply(myData, 260, dolm2, by.column = FALSE)

summary(myCoef2) #  0 NA's 

在上面的示例中,使用 fastLmPure 的第一种方法在输出中产生 NA,而使用 fastLm 的第二种方法则不会。

这是用 R 编写的 fastLmfastLmPure 函数的链接:

https://github.com/RcppCore/RcppArmadillo/blob/master/R/fastLm.R

这里是用 C++ 编写的底层 fastLm 函数的链接:

https://github.com/RcppCore/RcppArmadillo/blob/master/src/fastLm.cpp

从这些链接和 RcppArmadillo 的文档中,我不明白是什么导致了输出的差异?为什么第二个输出中没有 NA?最重要的问题是什么例程/部分代码阻止 NA 出现在第二种方法中,它是如何实现的?

最佳答案

您正在使用两个不同的接口(interface)调用两个不同的函数

特别是,fastLm() 当通过公式 y ~ X 使用时,将依赖于 R 内部(而且很慢!!)函数为您创建对应于 fastLm(X, y) 的 vector 和矩阵。

这是一个简单的设置示例:

R> data(mtcars)
R> lm(mpg ~ cyl + disp + hp + wt - 1, data=mtcars)

Call:
lm(formula = mpg ~ cyl + disp + hp + wt - 1, data = mtcars)

Coefficients:
    cyl     disp       hp       wt  
 5.3560  -0.1206  -0.0313   5.6913  

R> fastLm(mpg ~ cyl + disp + hp + wt - 1, data=mtcars)

Call:
fastLm.formula(formula = mpg ~ cyl + disp + hp + wt - 1, data = mtcars)

Coefficients:
      cyl      disp        hp        wt 
 5.356014 -0.120609 -0.031306  5.691273 
R> fastLm(mtcars[, c("cyl","disp","hp","wt")], mtcars[,"mpg"])

Call:
fastLm.default(X = mtcars[, c("cyl", "disp", "hp", "wt")], y = mtcars[, 
    "mpg"])

Coefficients:
      cyl      disp        hp        wt 
 5.356014 -0.120609 -0.031306  5.691273 
R> 

现在让我们在左侧和右侧添加一个 NA。为了便于索引,我们将使用整行:

R> mtcars[7, ] <- NA
R> lm(mpg ~ cyl + disp + hp + wt - 1, data=mtcars)

Call:
lm(formula = mpg ~ cyl + disp + hp + wt - 1, data = mtcars)

Coefficients:
    cyl     disp       hp       wt  
 5.3501  -0.1215  -0.0332   5.8281  

R> fastLm(mpg ~ cyl + disp + hp + wt - 1, data=mtcars)

Call:
fastLm.formula(formula = mpg ~ cyl + disp + hp + wt - 1, data = mtcars)

Coefficients:
      cyl      disp        hp        wt 
 5.350102 -0.121478 -0.033184  5.828065 
R> fastLm(na.omit(mtcars[, c("cyl","disp","hp","wt")]), na.omit(mtcars[,"mpg"]))

Call:
fastLm.default(X = na.omit(mtcars[, c("cyl", "disp", "hp", "wt")]), 
    y = na.omit(mtcars[, "mpg"]))

Coefficients:
      cyl      disp        hp        wt 
 5.350102 -0.121478 -0.033184  5.828065 
R> 

更重要的是:结果在所有方法之间仍然相同,前提是我们对缺失值保持一致

关于c++ - RcppArmadillo 中的 fastLm 和 fastLmPure 函数之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33584389/

相关文章:

r - 在插入符号中并行操作的可重现结果的种子对象

r - 根据某些条件生成随机数

c++ - Rcpp 在不打印空行时产生不同的输出

c++ - Rcpp map /字典/列表

c++ - 在编译时使用 C++ 模板在 AbstractFactory 中动态注册构造方法

c++ - 包含类对象的最佳 C++ 设计是什么?

c++ - C++不等待输入

带有 R 的列表的所有对象中的 rowSums

c++ - 在 Rcpp 中控制编译顺序

C++ 检查元素是否为 std::vector