Skip to content

Instantly share code, notes, and snippets.

@agarie
Created November 6, 2014 18:04
Show Gist options
  • Save agarie/154aa034854862b067f5 to your computer and use it in GitHub Desktop.
Save agarie/154aa034854862b067f5 to your computer and use it in GitHub Desktop.
Item Characteristic Curve plotting with d3js.
var N = 100,
a = 2.0,
b = 0.0,
c = 0.2;
var defaultScale = { mu: 0, sigma: 1 },
enemScale = { mu: 500, sigma: 100 };
// @return: The probability of hit at `theta`, or P(right | theta, a, b, c).
var prob = function (a, b, c, theta) {
return c + (1 - c) / (1 + Math.exp(- a * (theta - b)));
};
var irt3p = function (theta) {
return prob(a, b, c, theta);
};
var icc = [];
for (var i = 0; i < N; i++) {
// Change this to select the correct scale.
var scale = enemScale;
// The interval to be plotted is +- 3 standard deviations from the mean.
var theta = 3 * (i - (N/2.0)) / (N / 2.0);
icc.push({"theta": (theta * scale.sigma) + scale.mu, "prob": irt3p(theta)});
}
// D3.
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
x.domain(d3.extent(icc, function(d) { return d.theta; }));
y.domain([0.0, 1.0]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("x", width - 6)
.attr("dx", "0.71em")
.attr("dy", "-0.31em")
.style("text-anchor", "end")
.text("θ")
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.style("text-anchor", "end")
.text("Probability of hit");
svg.selectAll(".dot")
.data(icc)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 1.0)
.attr("cx", function(d) { return x(d.theta); })
.attr("cy", function(d) { return y(d.prob); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment