-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrcDF.cpp
More file actions
71 lines (53 loc) · 1.6 KB
/
rcDF.cpp
File metadata and controls
71 lines (53 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double square( double x ) {
return x*x;
}
// [[Rcpp::export]]
double rnd( double x ) {
return round(x);
}
// [[Rcpp::export]]
double diffs(NumericVector bill, NumericVector mc) {
NumericVector dist = mc - bill;
NumericVector absDist = abs(dist);
NumericVector powDist = sapply(absDist, square);
double distSum = sum(powDist);
return distSum;
}
// [[Rcpp::export]]
NumericVector adjud(NumericVector propDiffs, NumericVector sqDiffs) {
NumericVector billDiff = propDiffs - sqDiffs;
NumericVector voteProbs = pnorm(billDiff);
NumericVector votes = sapply(voteProbs, rnd);
return votes;
}
// [[Rcpp::export]]
Rcpp::NumericVector rcFn(NumericMatrix props, NumericMatrix sqs, NumericMatrix mcs) {
/* Pull dimensions of matrices */
int nProp = props.nrow(), nMC = mcs.nrow();
/* Create a blank matrix to populate */
NumericMatrix out(nMC, nProp);
/* Loop through each MC */
for (int i = 0; i < nMC; i++) {
/* Pull the vector representing the MC's ideal point */
NumericVector mc = mcs(i, _);
/* Empty vectors for sqs and proposals */
NumericVector propDiffs(nProp);
NumericVector sqDiffs(nProp);
/* Difference between MC ideal point and each sq/proposal */
for (int j = 0; j < nProp; j++) {
NumericVector prop = props(j, _);
NumericVector sq = sqs(j, _);
propDiffs[j] = diffs(prop, mc);
sqDiffs[j] = diffs(sq, mc);
}
/* Adjudicate between proposals and SQs */
NumericVector votes = adjud(propDiffs, sqDiffs);
/* Populate matrix of votes */
out(i, _) = votes;
}
/* Return the matrix of votes */
return out;
}