我写了下面的函数来计算一个矩阵(威 bool 模型的信息矩阵)
#include <RcppEigen.h>
#include <math.h>
#include <vector>
using namespace std;
using Eigen::MatrixXd;
// [[Rcpp::depends(RcppEigen)]]
// [[Rcpp::export]]
MatrixXd Weibull_FIM(const vector<double> x, const vector<double> w, const vector<double> param)
{
if(x.size() != w.size()){
Rcpp::Rcout<<"The length of x and w is not equal."<<std::endl;
exit(1);
}
double a, b, lambda, h, constant;
a = param[0];
b = param[1];
lambda = param[2];
h = param[3];
a = a + 0; //just to not get a warning
Eigen::MatrixXd Fisher_mat(4, 4);
size_t i;
for(i=0; i < x.size(); i++)
{
constant = exp(-lambda * pow(x[i], h));
Eigen::MatrixXd f(4, 1);
f(0, 0) = 1;
f(1, 0) = -constant;
f(2, 0) = b*pow(x[i], h)*constant;
f(3, 0) = b*pow(x[i], h)*constant * lambda * log(x[i]);
Fisher_mat = w[i] * f * f.transpose() + Fisher_mat;
}
return Fisher_mat;
}
现在我想计算 R 中某些值的矩阵:
Weibull_FIM(x=c(1, 1.239, 1.749371, 5), w = rep(.25, 4), param=c(1, 1, 1, 2))
# [,1] [,2] [,3] [,4]
#[1,] 1.00000000 -0.157545687 0.210509365 0.037774174
#[2,] -0.15754569 0.045985587 -0.053326010 -0.004757122
#[3,] 0.21050936 -0.053326010 0.066320481 0.008736574
#[4,] 0.03777417 -0.004757122 0.008736574 0.002864707
这个矩阵没问题。现在,如果我首先分配输出然后打印它,我将有一个不同的矩阵(一些元素将更改为一些大值!)。
res <- Weibull_FIM(x=c(1, 1.239, 1.749371, 5), w = rep(.25, 4), param=c(1, 1, 1, 2))
print(res)
# [,1] [,2] [,3] [,4]
#[1,] 1.00000000 4.727161e+180 0.210509365 2.267126e+161
#[2,] -0.15754569 5.104678e+199 -0.053326010 -4.757122e-03
#[3,] 0.21050936 2.079498e+64 0.066320481 8.736574e-03
#[4,] 0.03777417 -4.757122e-03 0.008736574 2.864707e-03
可以看出,元素 (1, 2), (1, 4), (2, 2) 和 (3, 2) 得到另一个大值。
我将不胜感激任何帮助。
Update1 回应@Ronald:
最佳答案
您忘记初始化 Fisher_mat
在求和之前用零,所以它有时可能包含随机垃圾。添加例如Fisher_mat.setZero();
之前 for
循环会给你一个一致的输出。
检查此引用:RcppEigen Introduction .
关于r - 矩阵乘法与 Rcpp - 分配输出时的不同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29796170/