Rcpp:Rcpp 中是否有实现 fisher.test()
Rcpp: Is there an implementation fisher.test() in Rcpp
是否有针对 Rcpp 的 fisher.test 实现?
Rcpp 中目前没有 fisher.test
函数的实现。 R函数中的一个特定的component of the test由于其计算量大,所以用C语言编写。非常欢迎您在 Rcpp 中重新实现测试。
关于此的一些注意事项,SEXP
对象中没有 factor
的表示。因此,factor
不是 Rcpp 支持的东西。因此,在将对象传递给 C++ 之前,您必须将对象转换为 integer
或 character
类型。
为了将它用于 Rcpp,您必须从 C++ 调用 fisher.test
R 函数。也就是说,您必须将数据传回 R。
例如
#include <Rcpp.h>
//' @title Accessing R's fisher.test function from Rcpp
// [[Rcpp::export]]
Rcpp::List fisher_test_cpp(const Rcpp::NumericMatrix& x, double conf_level = 0.95){
// Obtain environment containing function
Rcpp::Environment base("package:stats");
// Make function callable from C++
Rcpp::Function fisher_test = base["fisher.test"];
// Call the function and receive its list output
Rcpp::List test_out = fisher_test(Rcpp::_["x"] = x,
Rcpp::_["conf.level"] = conf_level);
// Return test object in list structure
return test_out;
}
/***R
Job = matrix(c(1,2,1,0, 3,3,6,1, 10,10,14,9, 6,7,12,11), 4, 4,
dimnames = list(income = c("< 15k", "15-25k", "25-40k", "> 40k"),
satisfaction = c("VeryD", "LittleD", "ModerateS", "VeryS")))
fisher.test(Job)
fisher_test_cpp(Job)
*/
注意列表形式下的 cpp 函数 returns 个对象:
List of 7
$ p.value : num 0.783
$ alternative: chr "two.sided"
$ method : chr "Fisher's Exact Test for Count Data"
$ data.name1 : chr "structure(c(1, 2, 1, 0, 3, 3, 6, 1, 10, 10, 14, 9, 6, 7, 12, "
$ data.name2 : chr "11), .Dim = c(4L, 4L), .Dimnames = structure(list(income = c(\"< 15k\", "
$ data.name3 : chr "\"15-25k\", \"25-40k\", \"> 40k\"), satisfaction = c(\"VeryD\", \"LittleD\", "
$ data.name4 : chr "\"ModerateS\", \"VeryS\")), .Names = c(\"income\", \"satisfaction\")))"
- attr(*, "class")= chr "htest"
可以使用以下方法访问这些值:
double p_value = test_out[0];
std::string alternative = test_out[1];
std::string method = test_out[2];
等等...
是否有针对 Rcpp 的 fisher.test 实现?
Rcpp 中目前没有 fisher.test
函数的实现。 R函数中的一个特定的component of the test由于其计算量大,所以用C语言编写。非常欢迎您在 Rcpp 中重新实现测试。
关于此的一些注意事项,SEXP
对象中没有 factor
的表示。因此,factor
不是 Rcpp 支持的东西。因此,在将对象传递给 C++ 之前,您必须将对象转换为 integer
或 character
类型。
为了将它用于 Rcpp,您必须从 C++ 调用 fisher.test
R 函数。也就是说,您必须将数据传回 R。
例如
#include <Rcpp.h>
//' @title Accessing R's fisher.test function from Rcpp
// [[Rcpp::export]]
Rcpp::List fisher_test_cpp(const Rcpp::NumericMatrix& x, double conf_level = 0.95){
// Obtain environment containing function
Rcpp::Environment base("package:stats");
// Make function callable from C++
Rcpp::Function fisher_test = base["fisher.test"];
// Call the function and receive its list output
Rcpp::List test_out = fisher_test(Rcpp::_["x"] = x,
Rcpp::_["conf.level"] = conf_level);
// Return test object in list structure
return test_out;
}
/***R
Job = matrix(c(1,2,1,0, 3,3,6,1, 10,10,14,9, 6,7,12,11), 4, 4,
dimnames = list(income = c("< 15k", "15-25k", "25-40k", "> 40k"),
satisfaction = c("VeryD", "LittleD", "ModerateS", "VeryS")))
fisher.test(Job)
fisher_test_cpp(Job)
*/
注意列表形式下的 cpp 函数 returns 个对象:
List of 7
$ p.value : num 0.783
$ alternative: chr "two.sided"
$ method : chr "Fisher's Exact Test for Count Data"
$ data.name1 : chr "structure(c(1, 2, 1, 0, 3, 3, 6, 1, 10, 10, 14, 9, 6, 7, 12, "
$ data.name2 : chr "11), .Dim = c(4L, 4L), .Dimnames = structure(list(income = c(\"< 15k\", "
$ data.name3 : chr "\"15-25k\", \"25-40k\", \"> 40k\"), satisfaction = c(\"VeryD\", \"LittleD\", "
$ data.name4 : chr "\"ModerateS\", \"VeryS\")), .Names = c(\"income\", \"satisfaction\")))"
- attr(*, "class")= chr "htest"
可以使用以下方法访问这些值:
double p_value = test_out[0];
std::string alternative = test_out[1];
std::string method = test_out[2];
等等...