15 std::vector<raggedArray> nb(arma::vec y,
20 double laplacesmooth) {
22 if (ylev.n_elem == 0) {
23 ylev = arma::unique(y);
25 unsigned K = ylev.n_elem;
26 unsigned n = y.n_elem;
27 unsigned p = x.n_cols;
28 if (xlev.n_elem < p) {
30 for (
unsigned j=0; j<p; j++) xlev(j) = j;
32 if (weights.n_elem < n) {
33 weights = arma::vec(n);
34 for (
unsigned i=0; i<n; i++) weights(i) = 1;
37 std::vector<raggedArray> res(K);
38 for (
unsigned k=0; k<K; k++) {
40 for (
unsigned i=0; i<n; i++) {
41 if (y[i]==ylev[k]) N++;
45 for (
unsigned i=0; i<n; i++) {
51 res[k] = pcond(idx,x,xlev,weights,laplacesmooth);
57 raggedArray pcond(
const arma::uvec &idx,
59 const arma::uvec &xlev,
60 const arma::vec &weights,
61 double laplacesmooth) {
62 unsigned p = x.n_cols;
65 for (
unsigned i=0; i<idx.n_elem; i++) sw += weights(i);
67 for (
unsigned j=0; j<p; j++) {
69 arma::vec est(xlev[j]);
70 for (
unsigned i=0; i<idx.n_elem; i++) {
71 double xval = x(idx(i),j);
72 est((
int)xval) += weights(idx(i));
76 for (
unsigned i=0; i<est.n_elem; i++) est(i) += laplacesmooth;
77 for (
unsigned i=0; i<est.n_elem; i++) s += est(i);
78 for (
unsigned i=0; i<est.n_elem; i++) est(i) /= s;
83 for (
unsigned i=0; i<idx.n_elem; i++) {
84 double xval = x(idx(i), j);
85 double w = weights(idx(i))/sw;
90 if (idx.n_elem == 1) {
93 est(1) = std::sqrt((ss-m*m)*sw/(sw-1));
103 arma::mat prednb(
const arma::mat &X,
104 const raggedArray &condprob,
106 arma::uvec multinomial,