quanteda / quanteda.textmodels

Compare 34017dc ... +6 ... 639f6b8

No flags found

Use flags to group coverage reports by test type, project and/or folders.
Then setup custom commit statuses and notifications for each flag.

e.g., #unittest #integration

#production #enterprise

#frontend #backend

Learn more about Codecov Flags here.

Showing 2 of 3 files from the diff.

@@ -1,9 +1,11 @@
Loading
1 1
#include "lib.h"
2 2
#include <math.h>
3 3
using namespace Rcpp;
4 +
using namespace arma;
5 +
4 6
5 7
// [[Rcpp::export]]
6 -
Rcpp::List qatd_cpp_wordfish_dense(SEXP wfm, SEXP dir, SEXP priors, SEXP tol, SEXP disp, SEXP dispfloor, bool abs_err){
8 +
Rcpp::List qatd_cpp_wordfish_dense(SEXP wfm, SEXP dir, SEXP priors, SEXP tol, SEXP disp, SEXP dispfloor, bool abs_err) {
7 9
8 10
    // DEFINE INPUTS
9 11
@@ -18,6 +20,8 @@
Loading
18 20
    double priorprecpsi = priorvec(1);
19 21
    double priorprecbeta = priorvec(2);
20 22
    double priorprectheta = priorvec(3);
23 +
    int method = priorvec(4);
24 +
    double zeta = 1.0;
21 25
22 26
    int N = Y.nrow();
23 27
    int K = Y.ncol();
@@ -27,8 +31,15 @@
Loading
27 31
    Rcpp::NumericVector psi(K);
28 32
    Rcpp::NumericVector beta(K);
29 33
    Rcpp::NumericVector theta(N);
34 +
    Rcpp::NumericVector beta2(K);
35 +
    Rcpp::NumericVector theta2(N);
30 36
31 37
    Rcpp::NumericVector thetaSE(N); // document position standard errors
38 +
    Rcpp::NumericVector theta2SE(N); // document position standard errors
39 +
    Rcpp::NumericVector alphaSE(N); // document position standard errors
40 +
    Rcpp::NumericVector psiSE(K); // document position standard errors
41 +
    Rcpp::NumericVector betaSE(K); // document position standard errors
42 +
    Rcpp::NumericVector beta2SE(K); // document position standard errors
32 43
    Rcpp::NumericVector phi(K,1.0); // word-level dispersion parameters
33 44
34 45
    // Construct Chi-Sq Residuals
@@ -38,7 +49,7 @@
Loading
38 49
    double asum = sum(rsum);
39 50
    for (int i = 0; i < N; i++){
40 51
        for (int k=0; k < K; k++){
41 -
            C(i,k) = (Y(i,k) - rsum(i)*csum(k)/asum)/sqrt(rsum(i)*csum(k)/asum);
52 +
            C(i,k) = (Y(i,k) - rsum(i) * csum(k) / asum) / sqrt(rsum(i) * csum(k) / asum);
42 53
        }
43 54
    }
44 55
@@ -51,9 +62,13 @@
Loading
51 62
    // Load initial values
52 63
    for (int i = 0; i < N; i++) {
53 64
        theta(i) = pow(rsum(i) / asum,-0.5) * U(i,0);
65 +
        theta2(i) = 0;
54 66
        //Rcout<<"theta starting values:"<<theta(i)<<std::endl;
55 67
    }
56 -
    for (int k=0; k < K; k++) beta(k) = 0; // pow(csum(k)/asum,-0.5) * V(k,0);
68 +
    for (int k=0; k < K; k++){
69 +
      beta(k) = -0.5; // pow(csum(k)/asum,-0.5) * V(k,0);
70 +
      beta2(k) = 0;
71 +
    }
57 72
    for (int i=0; i < N; i++) alpha = log(rsum);
58 73
    psi = log(csum/N);
59 74
@@ -66,26 +81,33 @@
Loading
66 81
    Rcpp::NumericMatrix G(2,1);
67 82
    Rcpp::NumericMatrix H(2,2);
68 83
    double loglambdaik;
84 +
    double lambdaik;
69 85
    double mutmp;
70 86
    double phitmp;
87 +
    double zetatmp;
71 88
    Rcpp::NumericVector lambdai(K);
72 89
    Rcpp::NumericVector lambdak(N);
73 90
    double stepsize = 1.0;
74 91
    double cc = 0.0;
92 +
    double l_mean = 0.0;
93 +
    double l2_mean = 0.0;
94 +
    double gg = 0;
95 +
    double hh = 0;
75 96
    int inneriter = 0;
76 97
    int outeriter = 0;
77 98
78 99
    double lastlp = -2000000000000.0;
79 100
    double lp = -1.0 * (sum(0.5 * ((alpha*alpha) * (priorprecalpha))) +
80 101
                        sum(0.5 * ((psi * psi) * (priorprecpsi))) +
81 -
                        sum(0.5 * ((beta*beta)*(priorprecbeta))) +
82 -
                        sum(0.5 * ((theta*theta)*(priorprectheta))));
102 +
                        sum(0.5 * ((beta*beta) * (priorprecbeta))) +
103 +
                        sum(0.5 * ((theta*theta) * (priorprectheta))));
104 +
    // double lp_P = lp;
83 105
84 106
    for (int i = 0; i < N; i++){
85 -
        for (int k=0; k < K; k++){
86 -
            loglambdaik = alpha(i) + psi(k) + beta(k) * theta(i);
87 -
            lp = lp + loglambdaik * Y(i,k) - exp(loglambdaik);
88 -
        }
107 +
      for (int k = 0; k < K; k++){
108 +
        loglambdaik = alpha(i) + psi(k) + beta(k) * theta(i);
109 +
        lp = lp + loglambdaik * Y(i,k) - exp(loglambdaik);
110 +
      }
89 111
    }
90 112
91 113
    // BEGIN WHILE LOOP
@@ -99,22 +121,22 @@
Loading
99 121
            inneriter = 0;
100 122
            if (outeriter == 1) stepsize = 0.5;
101 123
            while ((cc > tolvec(1)) && inneriter < 10){
102 -
                inneriter++;
103 -
                lambdak = exp(alpha + psi(k) + beta(k) * theta);
104 -
                G(0,0) = sum(Y(_,k) - lambdak) / phi(k) - psi(k) * (priorprecpsi);
105 -
                G(1,0) = sum(theta * (Y(_,k) - lambdak)) / phi(k) - beta(k) * (priorprecbeta);
106 -
                H(0,0) = -sum(lambdak) / phi(k) - priorprecpsi;
107 -
                H(1,0) = -sum(theta * lambdak)/phi(k);
108 -
                H(0,1) = H(1,0);
109 -
                H(1,1) = -sum((theta * theta) * lambdak) / phi(k) - priorprecbeta;
110 -
                pars(0,0) = psi(k);
111 -
                pars(1,0) = beta(k);
112 -
                newpars(0,0) = pars(0,0) - stepsize * (H(1,1) * G(0,0) - H(0,1) * G(1,0)) / (H(0,0) * H(1,1) - H(0,1) * H(1,0));
113 -
                newpars(1,0) = pars(1,0) - stepsize * (H(0,0) * G(1,0) - H(1,0) * G(0,0)) / (H(0,0) * H(1,1) - H(0,1) * H(1,0));
114 -
                psi(k) = newpars(0,0);
115 -
                beta(k) = newpars(1,0);
116 -
                cc = max(abs(newpars - pars));
117 -
                stepsize = 1.0;
124 +
              inneriter++;
125 +
              lambdak = exp(alpha + psi(k) + beta(k) * theta);
126 +
              G(0,0) = sum(Y(_,k) - lambdak) / phi(k) - psi(k) * (priorprecpsi);
127 +
              G(1,0) = sum(theta * (Y(_,k) - lambdak)) / phi(k) - beta(k) * (priorprecbeta);
128 +
              H(0,0) = -sum(lambdak) / phi(k) - priorprecpsi;
129 +
              H(1,0) = -sum(theta * lambdak)/phi(k);
130 +
              H(0,1) = H(1,0);
131 +
              H(1,1) = -sum((theta * theta) * lambdak) / phi(k) - priorprecbeta;
132 +
              pars(0,0) = psi(k);
133 +
              pars(1,0) = beta(k);
134 +
              newpars(0,0) = pars(0,0) - stepsize * (H(1,1) * G(0,0) - H(0,1) * G(1,0)) / (H(0,0) * H(1,1) - H(0,1) * H(1,0));
135 +
              newpars(1,0) = pars(1,0) - stepsize * (H(0,0) * G(1,0) - H(1,0) * G(0,0)) / (H(0,0) * H(1,1) - H(0,1) * H(1,0));
136 +
              psi(k) = newpars(0,0);
137 +
              beta(k) = newpars(1,0);
138 +
              cc = max(abs(newpars - pars));
139 +
              stepsize = 1.0;
118 140
            }
119 141
        }
120 142
@@ -125,22 +147,22 @@
Loading
125 147
            inneriter = 0;
126 148
            if (outeriter == 1) stepsize = 0.5;
127 149
            while ((cc > tolvec(1)) && inneriter < 10){
128 -
                inneriter++;
129 -
                lambdai = exp(alpha(i) + psi + beta * theta(i));
130 -
                G(0,0) = sum((Y(i,_) - lambdai) / phi) - alpha(i) * priorprecalpha;
131 -
                G(1,0) = sum((beta * (Y(i,_) - lambdai)) / phi) - theta(i) * priorprectheta;
132 -
                H(0,0) = -sum(lambdai/phi) - priorprecalpha;
133 -
                H(1,0) = -sum((beta * lambdai) / phi);
134 -
                H(0,1) = H(1,0);
135 -
                H(1,1) = -sum(((beta * beta) * lambdai) / phi) - priorprectheta;
136 -
                pars(0,0) = alpha(i);
137 -
                pars(1,0) = theta(i);
138 -
                newpars(0,0) = pars(0,0) - stepsize * (H(1,1) * G(0,0) - H(0,1) * G(1,0)) / (H(0,0) * H(1,1) - H(0,1) * H(1,0));
139 -
                newpars(1,0) = pars(1,0) - stepsize * (H(0,0) * G(1,0) - H(1,0) * G(0,0)) / (H(0,0) * H(1,1) - H(0,1) * H(1,0));
140 -
                alpha(i) = newpars(0,0);
141 -
                theta(i) = newpars(1,0);
142 -
                cc = max(abs(newpars - pars));
143 -
                stepsize = 1.0;
150 +
              inneriter++;
151 +
              lambdai = exp(alpha(i) + psi + beta * theta(i));
152 +
              G(0,0) = sum((Y(i,_) - lambdai) / phi) - alpha(i) * priorprecalpha;
153 +
              G(1,0) = sum((beta * (Y(i,_) - lambdai)) / phi) - theta(i) * priorprectheta;
154 +
              H(0,0) = -sum(lambdai/phi) - priorprecalpha;
155 +
              H(1,0) = -sum((beta * lambdai) / phi);
156 +
              H(0,1) = H(1,0);
157 +
              H(1,1) = -sum(((beta * beta) * lambdai) / phi) - priorprectheta;
158 +
              pars(0,0) = alpha(i);
159 +
              pars(1,0) = theta(i);
160 +
              newpars(0,0) = pars(0,0) - stepsize * (H(1,1) * G(0,0) - H(0,1) * G(1,0)) / (H(0,0) * H(1,1) - H(0,1) * H(1,0));
161 +
              newpars(1,0) = pars(1,0) - stepsize * (H(0,0) * G(1,0) - H(1,0) * G(0,0)) / (H(0,0) * H(1,1) - H(0,1) * H(1,0));
162 +
              alpha(i) = newpars(0,0);
163 +
              theta(i) = newpars(1,0);
164 +
              cc = max(abs(newpars - pars));
165 +
              stepsize = 1.0;
144 166
            }
145 167
        }
146 168
@@ -177,7 +199,10 @@
Loading
177 199
178 200
        // CHECK LOG-POSTERIOR FOR CONVERGENCE
179 201
        lastlp = lp;
180 -
        lp = -1.0 * (sum(0.5 * ((alpha*alpha) * (priorprecalpha))) + sum(0.5 * ((psi*psi) * (priorprecpsi))) + sum(0.5 * ((beta*beta)*(priorprecbeta))) + sum(0.5 * ((theta*theta)*(priorprectheta))));
202 +
        lp = -1.0 * (sum(0.5 * ((alpha*alpha) * (priorprecalpha))) +
203 +
                     sum(0.5 * ((psi*psi) * (priorprecpsi))) +
204 +
                     sum(0.5 * ((beta*beta)*(priorprecbeta))) +
205 +
                     sum(0.5 * ((theta*theta)*(priorprectheta))));
181 206
        for (int i = 0; i < N; i++){
182 207
            for (int k = 0; k < K; k++){
183 208
                loglambdaik = alpha(i) + psi(k) + beta(k) * theta(i);
@@ -188,6 +213,382 @@
Loading
188 213
        //Rcout<<"outeriter="<<outeriter<<"  lp - lastlp= "<<lp - lastlp<<std::endl;
189 214
        err = (abs_err == true) ? fabs(lp - lastlp) : (lp - lastlp);
190 215
        // END WHILE LOOP
216 +
217 +
    }
218 +
    // lp = lp_P;
219 +
220 +
    // IF TWO-DIMENSIONAL POISSON
221 +
    if (method == 4){
222 +
      // CALCULATE THE START VALUES OF THETA_2 AND BETA_2
223 +
      // Construct Chi-Sq Residuals
224 +
      for (int i = 0; i < N; i++){
225 +
        for (int k=0; k < K; k++){
226 +
          lambdaik = exp(alpha(i) + psi(k) + beta(k) * theta(i));
227 +
          C(i,k) = Y(i,k) - lambdaik;
228 +
        }
229 +
      }
230 +
231 +
      // Singular Value Decomposition of Chi-Sq Residuals
232 +
      svd(U,s,V,C);
233 +
234 +
      // Load initial values
235 +
      for (int i = 0; i < N; i++) {
236 +
        theta2(i) = U(i,0);
237 +
        //Rcout<<"theta starting values:"<<theta(i)<<std::endl;
238 +
      }
239 +
      for (int k=0; k < K; k++){
240 +
        beta2(k) = V(k,0);
241 +
      }
242 +
243 +
      // cout << "theta2: " << theta2 << endl;
244 +
      //
245 +
      // cout << "beta2: " << beta2 << endl;
246 +
247 +
248 +
      gg = 0;
249 +
      hh = 0;
250 +
      cc = 0.0;
251 +
      inneriter = 0;
252 +
      outeriter = 0;
253 +
      lastlp = -2000000000000.0;
254 +
      lp = lp - sum(0.5 * ((beta2 * beta2) * (priorprecbeta))) +
255 +
           sum(0.5 * ((theta2 * theta2) * (priorprectheta)));
256 +
      err = (abs_err == true) ? fabs(lp - lastlp) : (lp - lastlp);
257 +
      while ((err > tolvec(0)) && outeriter < 100) {
258 +
        outeriter++;
259 +
260 +
        // UPDATE WORD PARAMETERS
261 +
        for (int k = 0; k < K; k++) {
262 +
          cc = 1;
263 +
          inneriter = 0;
264 +
          if (outeriter == 1) stepsize = 0.5;
265 +
          while ((cc > tolvec(1)) && inneriter < 10){
266 +
            inneriter++;
267 +
            lambdak = exp(alpha + psi(k) + beta(k) * theta + beta2(k) * theta2);
268 +
            gg = sum(theta2 * (Y(_,k) - lambdak)) / phi(k) - beta2(k) * (priorprecbeta);
269 +
            hh = -sum((theta2 * theta2) * lambdak) / phi(k) - priorprecbeta;
270 +
            pars(0,0) = beta2(k);
271 +
            newpars(0,0) = pars(0,0) - stepsize * gg / hh;
272 +
            beta2(k) = newpars(0,0);
273 +
            cc = max(abs(newpars - pars));
274 +
            stepsize = 1.0;
275 +
          }
276 +
        }
277 +
278 +
        // UPDATE DOCUMENT PARAMETERS
279 +
        for (int i = 0; i < N; i++) {
280 +
          cc = 1;
281 +
          inneriter = 0;
282 +
          if (outeriter == 1) stepsize = 0.5;
283 +
          while ((cc > tolvec(1)) && inneriter < 10) {
284 +
            inneriter++;
285 +
            lambdai = exp(alpha(i) + psi + beta * theta(i));
286 +
            gg = sum(beta2 * (Y(i,_) - lambdai) / phi) - theta2(i) * (priorprectheta);
287 +
            hh = -sum((beta2 * beta2) * lambdai / phi) - priorprectheta;
288 +
            pars(0,0) = theta2(i);
289 +
            newpars(0,0) = pars(0,0) - stepsize * gg / hh;
290 +
            theta2(i) = newpars(0,0);
291 +
            cc = max(abs(newpars - pars));
292 +
            stepsize = 1.0;
293 +
          }
294 +
        }
295 +
296 +
        // UPDATE DISPERSION PARAMETERS
297 +
298 +
        if (disptype(0) == 2) { // single dispersion parameter for all words
299 +
          phitmp = 0.0;
300 +
          for (int k = 0; k < K; k++){
301 +
            for (int i=0; i < N; i++){
302 +
              mutmp = exp(alpha(i) + psi(k) + beta(k) * theta(i) + beta2(k) * theta2(i));
303 +
              phitmp = phitmp + (Y(i,k) - mutmp) * (Y(i,k) - mutmp) / mutmp;
304 +
            }
305 +
          }
306 +
          phitmp = phitmp / (N * K - 2 * N - 2 * K);
307 +
          for (int k = 0; k < K; k++) phi(k) = phitmp;
308 +
        }
309 +
310 +
        if (disptype(0) >= 3) { // individual dispersion parameter for each word
311 +
          for (int k = 0; k < K; k++){
312 +
            phitmp = 0.0;
313 +
            for (int i = 0; i < N; i++){
314 +
              mutmp = exp(alpha(i) + psi(k) + beta(k) * theta(i) + beta2(k) * theta2(i));
315 +
              phitmp = phitmp + (Y(i,k) - mutmp) * (Y(i,k) - mutmp) / mutmp;
316 +
            }
317 +
            phitmp = (K * phitmp) / (N * K - 2 * N - 2 * K);
318 +
            phi(k) = phitmp;
319 +
            // set ceiling on underdispersion
320 +
            if (disptype(0) == 4) phi(k) = fmax(dispmin(0), phi(k));
321 +
          }
322 +
        }
323 +
324 +
        theta2 = (theta2 - mean(theta2))/sd(theta2);
325 +
326 +
        // CHECK LOG-POSTERIOR FOR CONVERGENCE
327 +
        lastlp = lp;
328 +
        lp = -1.0 * (sum(0.5 * ((alpha*alpha) * (priorprecalpha))) +
329 +
          sum(0.5 * ((psi * psi) * (priorprecpsi))) +
330 +
          sum(0.5 * ((beta * beta) * (priorprecbeta))) +
331 +
          sum(0.5 * ((theta * theta) * (priorprectheta))) +
332 +
          sum(0.5 * ((beta2 * beta2) * (priorprecbeta))) +
333 +
          sum(0.5 * ((theta2 * theta2) * (priorprectheta))));
334 +
        for (int i = 0; i < N; i++){
335 +
          for (int k = 0; k < K; k++){
336 +
            loglambdaik = alpha(i) + psi(k) + beta(k) * theta(i) + beta2(k) * theta2(i);
337 +
            lp = lp + loglambdaik * Y(i,k) - exp(loglambdaik);
338 +
          }
339 +
        }
340 +
        // Rprintf("%d: %f2\\n",outeriter,lp);
341 +
        //Rcout<<"outeriter="<<outeriter<<"  lp - lastlp= "<<lp - lastlp<<std::endl;
342 +
        err = (abs_err == true) ? fabs(lp - lastlp) : (lp - lastlp);
343 +
        // END WHILE LOOP
344 +
345 +
      }
346 +
    }
347 +
348 +
349 +
    // cout << "skipped?" << endl;
350 +
351 +
    // IF USE THE NB MODEL
352 +
    if (method == 99 || method == 10000){
353 +
      cc = 0.0;
354 +
      inneriter = 0;
355 +
      outeriter = 0;
356 +
      lastlp = -2000000000000.0;
357 +
      lp = -1.0 * (sum(0.5 * ((alpha*alpha) * (priorprecalpha))) +
358 +
                   sum(0.5 * ((psi * psi) * (priorprecpsi))) +
359 +
                   sum(0.5 * ((beta*beta) * (priorprecbeta))) +
360 +
                   sum(0.5 * ((theta*theta) * (priorprectheta))));
361 +
      // double lp_NB = lp;
362 +
363 +
      for (int i = 0; i < N; i++) {
364 +
        for (int k = 0; k < K; k++) {
365 +
          loglambdaik = alpha(i) + psi(k) + beta(k) * theta(i);
366 +
          lp = lp + loglambdaik * Y(i,k) - (log(exp(loglambdaik)+zeta)) * (Y(i,k) + zeta);
367 +
        }
368 +
      }
369 +
      // BEGIN WHILE LOOP
370 +
      double err = (abs_err == true) ? fabs(lp - lastlp) : (lp - lastlp);
371 +
      while ((err > tolvec(0)) && outeriter < 100) {
372 +
        outeriter++;
373 +
374 +
        // UPDATE WORD PARAMETERS
375 +
        for (int k = 0; k < K; k++) {
376 +
          cc = 1;
377 +
          inneriter = 0;
378 +
          if (outeriter == 1) stepsize = 0.5;
379 +
380 +
          while ((cc > tolvec(1)) && inneriter < 10) {
381 +
            inneriter++;
382 +
            lambdak = exp(alpha + psi(k) + beta(k) * theta);
383 +
            G(0,0) = sum(zeta * (Y(_,k) - lambdak)/(lambdak + zeta)) - psi(k) * (priorprecpsi);
384 +
            G(1,0) = sum(theta * zeta * (Y(_,k) - lambdak)/(lambdak + zeta)) - beta(k) * (priorprecbeta);
385 +
            H(0,0) = -sum(zeta * lambdak * (Y(_,k) + zeta)/((lambdak + zeta) * (lambdak + zeta))) - priorprecpsi;
386 +
            H(1,0) = -sum(zeta * lambdak * theta * (Y(_,k) + zeta)/((lambdak + zeta) * (lambdak + zeta)));
387 +
            H(0,1) = H(1,0);
388 +
            H(1,1) = -sum(zeta * lambdak * theta * theta * (Y(_,k) + zeta)/((lambdak + zeta) * (lambdak + zeta))) - priorprectheta;
389 +
            pars(0,0) = psi(k);
390 +
            pars(1,0) = beta(k);
391 +
            newpars(0,0) = pars(0,0) - stepsize * (H(1,1) * G(0,0) - H(0,1) * G(1,0)) / (H(0,0) * H(1,1) - H(0,1) * H(1,0));
392 +
            newpars(1,0) = pars(1,0) - stepsize * (H(0,0) * G(1,0) - H(1,0) * G(0,0)) / (H(0,0) * H(1,1) - H(0,1) * H(1,0));
393 +
            psi(k) = newpars(0,0);
394 +
            beta(k) = newpars(1,0);
395 +
            cc = max(abs(newpars - pars));
396 +
            stepsize = 1;
397 +
          }
398 +
        }
399 +
400 +
401 +
        // UPDATE DOCUMENT PARAMETERS
402 +
        for (int i = 0; i < N; i++){
403 +
          cc = 1;
404 +
          inneriter = 0;
405 +
          if (outeriter == 1) stepsize = 0.5;
406 +
          while ((cc > tolvec(1)) && inneriter < 10){
407 +
            inneriter++;
408 +
            lambdai = exp(alpha(i) + psi + beta * theta(i));
409 +
            G(0,0) = sum(zeta * (Y(i,_) - lambdai)/(lambdai + zeta)) - alpha(i) * priorprecalpha;
410 +
            G(1,0) = sum(beta * zeta * (Y(i,_) - lambdai)/(lambdai + zeta)) - theta(i) * priorprectheta;
411 +
            H(0,0) = -sum(zeta * lambdai * (Y(i,_) + zeta)/((lambdai + zeta) * (lambdai + zeta))) - priorprecalpha;
412 +
            H(1,0) = -sum(zeta * lambdai * beta * (Y(i,_) + zeta)/((lambdai + zeta) * (lambdai + zeta)));
413 +
            H(0,1) = H(1,0);
414 +
            H(1,1) = -sum(zeta * lambdai * beta * beta * (Y(i,_) + zeta)/((lambdai + zeta) * (lambdai + zeta))) - priorprectheta;
415 +
            pars(0,0) = alpha(i);
416 +
            pars(1,0) = theta(i);
417 +
            newpars(0,0) = pars(0,0) - stepsize * (H(1,1) * G(0,0) - H(0,1) * G(1,0)) / (H(0,0) * H(1,1) - H(0,1) * H(1,0));
418 +
            newpars(1,0) = pars(1,0) - stepsize * (H(0,0) * G(1,0) - H(1,0) * G(0,0)) / (H(0,0) * H(1,1) - H(0,1) * H(1,0));
419 +
            alpha(i) = newpars(0,0);
420 +
            theta(i) = newpars(1,0);
421 +
            cc = max(abs(newpars - pars));
422 +
            stepsize = 1;
423 +
          }
424 +
        }
425 +
426 +
        // UPDATE DISPERSION PARAMETERS
427 +
        zetatmp = 0.0;
428 +
        l_mean = 0.0;
429 +
        l2_mean = 0.0;
430 +
        for (int k = 0; k < K; k++){
431 +
          for (int i=0; i < N; i++){
432 +
            mutmp = exp(alpha(i) + psi(k) + beta(k) * theta(i));
433 +
            l_mean = l_mean + mutmp;
434 +
            l2_mean = l2_mean + mutmp * mutmp;
435 +
            // zetatmp = zetatmp + mutmp * mutmp / ((Y(i,k) - mutmp) * (Y(i,k) - mutmp) - mutmp);
436 +
            // zetatmp = zetatmp + (Y(i,k) - mutmp) * (Y(i,k) - mutmp);
437 +
          }
438 +
        }
439 +
        l_mean = l_mean / (N * K);
440 +
        l2_mean = l2_mean / (N * K);
441 +
442 +
443 +
        for (int k = 0; k < K; k++) {
444 +
          for (int i=0; i < N; i++) {
445 +
            zetatmp = zetatmp + (Y(i,k) - l_mean) * (Y(i,k) - l_mean);
446 +
          }
447 +
        }
448 +
449 +
        // zeta = zetatmp;
450 +
        zeta = l2_mean / ((zetatmp / (N * K - 1)) - l_mean);
451 +
452 +
        alpha = alpha - mean(alpha);
453 +
        theta = (theta - mean(theta))/sd(theta);
454 +
455 +
        // CHECK LOG-POSTERIOR FOR CONVERGENCE
456 +
        lastlp = lp;
457 +
        lp = -1.0 * (sum(0.5 * ((alpha*alpha) * (priorprecalpha))) +
458 +
          sum(0.5 * ((psi*psi) * (priorprecpsi))) +
459 +
          sum(0.5 * ((beta*beta) * (priorprecbeta))) +
460 +
          sum(0.5 * ((theta*theta) * (priorprectheta))));
461 +
        for (int i = 0; i < N; i++) {
462 +
          for (int k = 0; k < K; k++) {
463 +
            loglambdaik = alpha(i) + psi(k) + beta(k) * theta(i);
464 +
            lp = lp + loglambdaik * Y(i,k) - (log(exp(loglambdaik) + zeta)) * (Y(i,k) + zeta);
465 +
          }
466 +
        }
467 +
468 +
        // Rprintf("%d: %f2\\n",outeriter,lp);
469 +
        //Rcout<<"outeriter="<<outeriter<<"  lp - lastlp= "<<lp - lastlp<<std::endl;
470 +
        err = (abs_err == true) ? fabs(lp - lastlp) : (lp - lastlp);
471 +
        // END WHILE LOOP
472 +
      }
473 +
      // END NB MODEL
474 +
      // lp = lp_NB;
475 +
      // IF TWO-DIMENSIONAL NB
476 +
477 +
      if (method == 10000){
478 +
        for (int i = 0; i < N; i++){
479 +
          for (int k=0; k < K; k++){
480 +
            lambdaik = exp(alpha(i) + psi(k) + beta(k) * theta(i));
481 +
            C(i,k) = Y(i,k) - lambdaik;
482 +
          }
483 +
        }
484 +
485 +
486 +
        // Singular Value Decomposition of Chi-Sq Residuals
487 +
        svd(U,s,V,C);
488 +
489 +
        // Load initial values
490 +
        for (int i = 0; i < N; i++) {
491 +
          theta2(i) = U(i,0);
492 +
          //Rcout<<"theta starting values:"<<theta(i)<<std::endl;
493 +
        }
494 +
        for (int k=0; k < K; k++) {
495 +
          beta2(k) = V(k,0);
496 +
        }
497 +
498 +
499 +
        gg = 0;
500 +
        hh = 0;
501 +
        cc = 0.0;
502 +
        inneriter = 0;
503 +
        outeriter = 0;
504 +
        lastlp = -2000000000000.0;
505 +
        lp = lp - sum(0.5 * ((beta2 * beta2) * (priorprecbeta))) +
506 +
          sum(0.5 * ((theta2 * theta2) * (priorprectheta)));
507 +
        err = (abs_err == true) ? fabs(lp - lastlp) : (lp - lastlp);
508 +
        while ((err > tolvec(0)) && outeriter < 100) {
509 +
          outeriter++;
510 +
511 +
          // UPDATE WORD PARAMETERS
512 +
          for (int k = 0; k < K; k++) {
513 +
            cc = 1;
514 +
            inneriter = 0;
515 +
            if (outeriter == 1) stepsize = 0.5;
516 +
            while ((cc > tolvec(1)) && inneriter < 10){
517 +
              inneriter++;
518 +
              lambdak = exp(alpha + psi(k) + beta(k) * theta + beta2(k) * theta2);
519 +
              gg = sum(zeta * theta2 * (Y(_,k) - lambdak) / (lambdak + zeta)) - beta2(k) * (priorprecbeta);
520 +
              hh = -sum(zeta * (theta2 * theta2) * lambdak * (Y(_,k) + lambdak) / ((lambdak + zeta) * (lambdak + zeta))) - priorprecbeta;
521 +
              pars(0,0) = beta2(k);
522 +
              newpars(0,0) = pars(0,0) - stepsize * gg / hh;
523 +
              beta2(k) = newpars(0,0);
524 +
              cc = max(abs(newpars - pars));
525 +
              stepsize = 1.0;
526 +
            }
527 +
          }
528 +
529 +
530 +
          // UPDATE DOCUMENT PARAMETERS
531 +
          for (int i = 0; i < N; i++){
532 +
            cc = 1;
533 +
            inneriter = 0;
534 +
            if (outeriter == 1) stepsize = 0.5;
535 +
            while ((cc > tolvec(1)) && inneriter < 10){
536 +
              inneriter++;
537 +
              lambdai = exp(alpha(i) + psi + beta * theta(i));
538 +
              gg = sum(zeta * beta2 * (Y(i,_) - lambdai) / (lambdai + zeta)) - theta2(i) * (priorprectheta);
539 +
              hh = -sum(zeta * (beta2 * beta2) * lambdai * (Y(i,_) + lambdai) / ((lambdai + zeta) * (lambdai + zeta))) - priorprectheta;
540 +
              pars(0,0) = theta2(i);
541 +
              newpars(0,0) = pars(0,0) - stepsize * gg / hh;
542 +
              theta2(i) = newpars(0,0);
543 +
              cc = max(abs(newpars - pars));
544 +
              stepsize = 1.0;
545 +
            }
546 +
          }
547 +
548 +
          // UPDATE DISPERSION PARAMETERS
549 +
          zetatmp = 0.0;
550 +
          l_mean = 0.0;
551 +
          l2_mean = 0.0;
552 +
          for (int k = 0; k < K; k++){
553 +
            for (int i=0; i < N; i++){
554 +
              mutmp = exp(alpha(i) + psi(k) + beta(k) * theta(i) + beta2(k) * theta2(i));
555 +
              l_mean = l_mean + mutmp;
556 +
              l2_mean = l2_mean + mutmp * mutmp;
557 +
            }
558 +
          }
559 +
          l_mean = l_mean / (N*K);
560 +
          l2_mean = l2_mean / (N*K);
561 +
562 +
          for (int k = 0; k < K; k++){
563 +
            for (int i=0; i < N; i++){
564 +
              zetatmp = zetatmp + (Y(i,k) - l_mean) * (Y(i,k) - l_mean);
565 +
            }
566 +
          }
567 +
568 +
          // zeta = zetatmp;
569 +
          zeta = l2_mean / ((zetatmp / (N*K-1)) - l_mean);
570 +
571 +
          theta2 = (theta2 - mean(theta2))/sd(theta2);
572 +
573 +
          // CHECK LOG-POSTERIOR FOR CONVERGENCE
574 +
          lastlp = lp;
575 +
          lp = -1.0 * (sum(0.5 * ((alpha*alpha) * (priorprecalpha))) + sum(0.5 * ((psi*psi) * (priorprecpsi))) +
576 +
            sum(0.5 * ((beta*beta)*(priorprecbeta))) + sum(0.5 * ((theta*theta)*(priorprectheta))) +
577 +
            sum(0.5 * ((beta2*beta2)*(priorprecbeta))) + sum(0.5 * ((theta2*theta2)*(priorprectheta))));
578 +
          for (int i = 0; i < N; i++){
579 +
            for (int k = 0; k < K; k++){
580 +
              loglambdaik = alpha(i) + psi(k) + beta(k) * theta(i) + beta2(k) * theta2(i);
581 +
              lp = lp + loglambdaik * Y(i,k) - (log(exp(loglambdaik)+zeta)) * (Y(i,k) + zeta);
582 +
            }
583 +
          }
584 +
          // Rprintf("%d: %f2\\n",outeriter,lp);
585 +
          //Rcout<<"outeriter="<<outeriter<<"  lp - lastlp= "<<lp - lastlp<<std::endl;
586 +
          err = (abs_err == true) ? fabs(lp - lastlp) : (lp - lastlp);
587 +
          // END OF WHILE
588 +
        }
589 +
        // END OF TWO-DIMENSIONAL MODEL
590 +
      }
591 +
      // END OF NB MODEL
191 592
    }
192 593
193 594
@@ -202,20 +603,85 @@
Loading
202 603
    // COMPUTE DOCUMENT STANDARD ERRORS
203 604
    for (int i = 0; i < N; i++) {
204 605
        lambdai = exp(alpha(i) + psi + beta*theta(i));
205 -
        H(0,0) = -sum(lambdai / phi) - priorprecalpha;
206 -
        H(1,0) = -sum((beta * lambdai) / phi);
207 -
        H(0,1) = H(1,0);
208 -
        H(1,1) = -sum(((beta* beta) * lambdai)/phi) - priorprectheta;
606 +
        if (method == 1){
607 +
          H(0,0) = -sum(lambdai / phi) - priorprecalpha;
608 +
          H(1,0) = -sum((beta * lambdai) / phi);
609 +
          H(0,1) = H(1,0);
610 +
          H(1,1) = -sum(((beta * beta) * lambdai)/phi) - priorprectheta;
611 +
        }else if (method == 4){
612 +
          H(0,0) = -sum(lambdai / phi) - priorprecalpha;
613 +
          H(1,0) = -sum((beta * lambdai) / phi);
614 +
          H(0,1) = H(1,0);
615 +
          H(1,1) = -sum(((beta * beta) * lambdai)/phi) - priorprectheta;
616 +
          hh = -sum((beta2 * beta2) * lambdai / phi) - priorprectheta;
617 +
          theta2SE(i) = sqrt(-1.0 / hh);
618 +
        }else if (method == 99){
619 +
          H(0,0) = -sum(zeta * lambdai * (Y(i,_) + zeta)/((lambdai + zeta) * (lambdai + zeta))) - priorprecalpha;
620 +
          H(1,0) = -sum(zeta * lambdai * beta * (Y(i,_) + zeta)/((lambdai + zeta) * (lambdai + zeta)));
621 +
          H(0,1) = H(1,0);
622 +
          H(1,1) = -sum(zeta * lambdai * beta * beta * (Y(i,_) + zeta)/((lambdai + zeta) * (lambdai + zeta))) - priorprectheta;
623 +
        }else if (method == 10000){
624 +
          H(0,0) = -sum(zeta * lambdai * (Y(i,_) + zeta)/((lambdai + zeta) * (lambdai + zeta))) - priorprecalpha;
625 +
          H(1,0) = -sum(zeta * lambdai * beta * (Y(i,_) + zeta)/((lambdai + zeta) * (lambdai + zeta)));
626 +
          H(0,1) = H(1,0);
627 +
          H(1,1) = -sum(zeta * lambdai * beta * beta * (Y(i,_) + zeta)/((lambdai + zeta) * (lambdai + zeta))) - priorprectheta;
628 +
          hh = -sum(zeta * (beta2 * beta2) * lambdai * (Y(i,_) + lambdai) / ((lambdai + zeta) * (lambdai + zeta))) - priorprectheta;
629 +
          theta2SE(i) = sqrt(-1.0 / hh);
630 +
        }
209 631
        thetaSE(i) = sqrt(-1.0 * H(0,0) / (H(0,0) * H(1,1) - H(1,0) * H(0,1)));
632 +
        alphaSE(i) = sqrt(-1.0 * H(1,1) / (H(0,0) * H(1,1) - H(1,0) * H(0,1)));
633 +
    }
634 +
635 +
    // COMPUTE WORD STANDARD ERRORS
636 +
    for (int k = 0; k < K; k++) {
637 +
      lambdak = exp(alpha + psi(k) + beta(k) * theta);
638 +
      arma::mat H(2,2);
639 +
      if (method == 1){
640 +
        H(0,0) = -sum(lambdak / phi[k]) - priorprecpsi;
641 +
        H(1,0) = -sum((theta * lambdak) / phi[k]);
642 +
        H(0,1) = H(1,0);
643 +
        H(1,1) = -sum(((theta * theta) * lambdak) / phi[k]) - priorprecbeta;
644 +
      }else if (method == 4){
645 +
        H(0,0) = -sum(lambdak / phi[k]) - priorprecpsi;
646 +
        H(1,0) = -sum((theta * lambdak) / phi[k]);
647 +
        H(0,1) = H(1,0);
648 +
        H(1,1) = -sum(((theta * theta) * lambdak) / phi[k]) - priorprecbeta;
649 +
        hh = -sum((theta2 * theta2) * lambdak) / phi(k) - priorprecbeta;
650 +
        beta2SE(k) = sqrt(-1.0 / hh);
651 +
      }else if (method == 99){
652 +
        H(0,0) = -sum(zeta * lambdak * (Y(_,k) + zeta)/((lambdak + zeta) * (lambdak + zeta))) - priorprecpsi;
653 +
        H(1,0) = -sum(zeta * lambdak * theta * (Y(_,k) + zeta)/((lambdak + zeta) * (lambdak + zeta)));
654 +
        H(0,1) = H(1,0);
655 +
        H(1,1) = -sum(zeta * lambdak * theta * theta * (Y(_,k) + zeta)/((lambdak + zeta) * (lambdak + zeta))) - priorprectheta;
656 +
      }else if (method == 10000){
657 +
        H(0,0) = -sum(zeta * lambdak * (Y(_,k) + zeta)/((lambdak + zeta) * (lambdak + zeta))) - priorprecpsi;
658 +
        H(1,0) = -sum(zeta * lambdak * theta * (Y(_,k) + zeta)/((lambdak + zeta) * (lambdak + zeta)));
659 +
        H(0,1) = H(1,0);
660 +
        H(1,1) = -sum(zeta * lambdak * theta * theta * (Y(_,k) + zeta)/((lambdak + zeta) * (lambdak + zeta))) - priorprectheta;
661 +
        hh = -sum(zeta * (theta2 * theta2) * lambdak * (Y(_,k) + lambdak) / ((lambdak + zeta) * (lambdak + zeta))) - priorprecbeta;
662 +
        beta2SE(k) = sqrt(-1.0 / hh);
663 +
      }
664 +
665 +
      betaSE(k) = sqrt(-1.0 * H(0,0) / (H(0,0) * H(1,1) - H(1,0) * H(0,1)));
666 +
      psiSE(k) = sqrt(-1.0 * H(1,1) / (H(0,0) * H(1,1) - H(1,0) * H(0,1)));
210 667
    }
211 668
212 669
    // DEFINE OUTPUT
213 670
214 671
    return Rcpp::List::create(Rcpp::Named("theta") = theta,
215 672
                              Rcpp::Named("alpha") = alpha,
216 673
                              Rcpp::Named("psi") = psi,
674 +
                              Rcpp::Named("zeta") = zeta,
217 675
                              Rcpp::Named("beta") = beta,
676 +
                              Rcpp::Named("beta2") = beta2,
677 +
                              Rcpp::Named("theta2") = theta2,
218 678
                              Rcpp::Named("phi") = phi,
219 -
                              Rcpp::Named("thetaSE") = thetaSE);
679 +
                              Rcpp::Named("thetaSE") = thetaSE,
680 +
                              Rcpp::Named("alphaSE") = alphaSE,
681 +
                              Rcpp::Named("psiSE") = psiSE,
682 +
                              Rcpp::Named("betaSE") = betaSE,
683 +
                              Rcpp::Named("theta2SE") = theta2SE,
684 +
                              Rcpp::Named("beta2SE") = beta2SE,
685 +
                              Rcpp::Named("LogL") = lp);
220 686
221 687
}

@@ -16,6 +16,11 @@
Loading
16 16
#'   tolerance in the difference in parameter values from the iterative
17 17
#'   conditional maximum likelihood (from conditionally estimating
18 18
#'   document-level, then feature-level parameters).
19 +
#' @param method specifies whether to use the Poisson model or the negative
20 +
#'   binomial model
21 +
#' @param dim2 a boolean variable that specifies whether to estimate the second
22 +
#'   dimension of theta and beta
23 +
#' @param prior_values NEEDS DOCUMENTING
19 24
#' @param dispersion sets whether a quasi-Poisson quasi-likelihood should be
20 25
#'   used based on a single dispersion parameter (`"poisson"`), or
21 26
#'   quasi-Poisson (`"quasipoisson"`)
@@ -65,7 +70,7 @@
Loading
65 70
#'   \doi{10.1093/pan/mpt002}. *Political Analysis*, 21(3), 298--313.
66 71
#' @author Benjamin Lauderdale, Haiyan Wang, and Kenneth Benoit
67 72
#' @examples
68 -
#' (tmod1 <- textmodel_wordfish(data_dfm_lbgexample, dir = c(1,5)))
73 +
#' (tmod1 <- textmodel_wordfish(data_dfm_lbgexample, dir = c(1, 5)))
69 74
#' summary(tmod1, n = 10)
70 75
#' coef(tmod1)
71 76
#' predict(tmod1)
@@ -75,14 +80,16 @@
Loading
75 80
#' \dontrun{
76 81
#' library("quanteda")
77 82
#' dfmat <- dfm(tokens(data_corpus_irishbudget2010))
78 -
#' (tmod2 <- textmodel_wordfish(dfmat, dir = c(6,5)))
79 -
#' (tmod3 <- textmodel_wordfish(dfmat, dir = c(6,5),
83 +
#' (tmod2 <- textmodel_wordfish(dfmat, dir = c(6, 5)))
84 +
#' (tmod3 <- textmodel_wordfish(dfmat, dir = c(6, 5),
80 85
#'                              dispersion = "quasipoisson", dispersion_floor = 0))
81 -
#' (tmod4 <- textmodel_wordfish(dfmat, dir = c(6,5),
86 +
#' (tmod4 <- textmodel_wordfish(dfmat, dir = c(6, 5),
82 87
#'                              dispersion = "quasipoisson", dispersion_floor = .5))
83 -
#' plot(tmod3$phi, tmod4$phi, xlab = "Min underdispersion = 0", ylab = "Min underdispersion = .5",
88 +
#' plot(tmod3$phi, tmod4$phi,
89 +
#'      xlab = "Min underdispersion = 0", ylab = "Min underdispersion = .5",
84 90
#'      xlim = c(0, 1.0), ylim = c(0, 1.0))
85 -
#' plot(tmod3$phi, tmod4$phi, xlab = "Min underdispersion = 0", ylab = "Min underdispersion = .5",
91 +
#' plot(tmod3$phi, tmod4$phi,
92 +
#'      xlab = "Min underdispersion = 0", ylab = "Min underdispersion = .5",
86 93
#'      xlim = c(0, 1.0), ylim = c(0, 1.0), type = "n")
87 94
#' underdispersedTerms <- sample(which(tmod3$phi < 1.0), 5)
88 95
#' which(featnames(dfmat) %in% names(topfeatures(dfmat, 20)))
@@ -100,6 +107,9 @@
Loading
100 107
textmodel_wordfish <- function(x, dir = c(1, 2),
101 108
                               priors = c(Inf, Inf, 3, 1),
102 109
                               tol = c(1e-6, 1e-8),
110 +
                               method = "Poisson",
111 +
                               dim2 = FALSE,
112 +
                               prior_values = NULL,
103 113
                               dispersion = c("poisson", "quasipoisson"),
104 114
                               dispersion_level = c("feature", "overall"),
105 115
                               dispersion_floor = 0,
@@ -114,6 +124,9 @@
Loading
114 124
textmodel_wordfish.default <- function(x, dir = c(1, 2),
115 125
                                       priors = c(Inf, Inf, 3, 1),
116 126
                                       tol = c(1e-6, 1e-8),
127 +
                                       method = "Poisson",
128 +
                                       dim2 = FALSE,
129 +
                                       prior_values = NULL,
117 130
                                       dispersion = c("poisson", "quasipoisson"),
118 131
                                       dispersion_level = c("feature", "overall"),
119 132
                                       dispersion_floor = 0,
@@ -128,6 +141,9 @@
Loading
128 141
textmodel_wordfish.dfm <- function(x, dir = c(1, 2),
129 142
                                   priors = c(Inf, Inf, 3, 1),
130 143
                                   tol = c(1e-6, 1e-8),
144 +
                                   method = "Poisson",
145 +
                                   dim2 = FALSE,
146 +
                                   prior_values = NULL,
131 147
                                   dispersion = c("poisson", "quasipoisson"),
132 148
                                   dispersion_level = c("feature", "overall"),
133 149
                                   dispersion_floor = 0,
@@ -187,19 +203,41 @@
Loading
187 203
    } else {
188 204
        stop("Illegal option combination.")
189 205
    }
190 -
    if (sparse == TRUE) {
206 +
207 +
    if (method == "Poisson") {
208 +
209 +
      if (dim2 == TRUE) {
210 +
        priors <- c(priors, 0.5)
211 +
      }else if (dim2 == FALSE) {
212 +
        priors <- c(priors, 1)
213 +
      }
214 +
      if (sparse == TRUE) {
191 215
        result <- qatd_cpp_wordfish(x, as.integer(dir), 1 / (priors ^ 2),
192 216
                                    tol, disp,
193 217
                                    dispersion_floor, abs_err, svd_sparse,
194 218
                                    residual_floor)
195 -
    } else{
219 +
      } else{
196 220
        result <- qatd_cpp_wordfish_dense(as.matrix(x),
197 221
                                          as.integer(dir), 1 / (priors ^ 2),
198 222
                                          tol, disp,
199 223
                                          dispersion_floor, abs_err)
200 -
    }
201 -
    # NOTE: psi is a 1 x nfeat matrix, not a numeric vector
202 -
    #       alpha is a ndoc x 1 matrix, not a numeric vector
224 +
      }
225 +
      # NOTE: psi is a 1 x nfeat matrix, not a numeric vector
226 +
      #       alpha is a ndoc x 1 matrix, not a numeric vector
227 +
    }else if (method == "NB") {
228 +
      if (dim2 == TRUE) {
229 +
        priors <- c(priors, 0.01)
230 +
      } else if (dim2 == FALSE) {
231 +
        priors <- c(priors, 0.1)
232 +
      }
233 +
234 +
      result <- qatd_cpp_wordfish_dense(as.matrix(x),
235 +
                                        as.integer(dir), 1 / (priors ^ 2),
236 +
                                        tol, disp,
237 +
                                        dispersion_floor, abs_err)
238 +
     }
239 +
240 +
203 241
    if (any(is.nan(result$theta)))
204 242
        warning("Warning: The algorithm did not converge.")
205 243
@@ -215,13 +253,23 @@
Loading
215 253
        psi = as.numeric(result$psi),
216 254
        alpha = as.numeric(result$alpha),
217 255
        phi = as.numeric(result$phi),
218 -
        se.theta = as.numeric(result$thetaSE) ,
256 +
        zeta = as.numeric(result$zeta),
257 +
        theta2 = as.numeric(result$theta2),
258 +
        beta2 = as.numeric(result$beta2),
259 +
        se.alpha = as.numeric(result$alphaSE),
260 +
        se.theta = as.numeric(result$thetaSE),
261 +
        se.psi = as.numeric(result$psiSE),
262 +
        se.beta = as.numeric(result$betaSE),
263 +
        se.theta2 = as.numeric(result$theta2SE),
264 +
        se.beta2 = as.numeric(result$beta2SE),
265 +
        lp = as.numeric(result$LogL),
219 266
        call = match.call()
220 267
    )
221 268
    class(result) <- c("textmodel_wordfish", "textmodel", "list")
222 269
    result
223 270
}
224 271
272 +
225 273
#' Prediction from a textmodel_wordfish method
226 274
#'
227 275
#' `predict.textmodel_wordfish()` returns estimated document scores and
@@ -273,7 +321,7 @@
Loading
273 321
    print(x$call)
274 322
    cat("\n",
275 323
        "Dispersion: ", x$dispersion, "; ",
276 -
        "direction: ", x$dir[1], ' < ' , x$dir[2], "; ",
324 +
        "direction: ", x$dir[1], " < ", x$dir[2], "; ",
277 325
        ndoc(x), " documents; ",
278 326
        nfeat(x), " features.",
279 327
        "\n",
@@ -298,9 +346,9 @@
Loading
298 346
    )
299 347
300 348
    result <- list(
301 -
        'call' = object$call,
302 -
        'estimated.document.positions' = as.statistics_textmodel(stat),
303 -
        'estimated.feature.scores' = as.coefficients_textmodel(head(coef(object)$features, n))
349 +
        "call" = object$call,
350 +
        "estimated.document.positions" = as.statistics_textmodel(stat),
351 +
        "estimated.feature.scores" = as.coefficients_textmodel(head(coef(object)$features, n))
304 352
    )
305 353
    return(as.summary.textmodel(result))
306 354
}

Everything is accounted for!

No changes detected that need to be reviewed.
What changes does Codecov check for?
Lines, not adjusted in diff, that have changed coverage data.
Files that introduced coverage data that had none before.
Files that have missing coverage data that once were tracked.
Files Coverage
R -0.49% 82.33%
src -6.38% 40.94%
Project Totals (19 files) 58.72%
Loading