Diversity prediction theorem: Difference between revisions

Added R.
m (→‎version 2: changed quote style, added whitespace to some statements.)
(Added R.)
Line 1,178:
Observation: '49'
"Expected number, saw: <class 'str'> '49'"</pre>
=={{header|R}}==
R's vectorisation shines here. The hardest part of this task was giving each estimate its own numbered column, which is little more than a printing luxury. The actual mathematics was trivial, with each part done in essentially one line.
<lang R>diversityStats<-function(trueValue, estimates)
{
collectivePrediction<-mean(estimates)
data.frame("True Value" = trueValue,
as.list(setNames(estimates, paste0("Guess-", seq_along(estimates)))), #Guesses, each with a title and column.
"Average Error" = mean((trueValue-estimates)^2),
"Crowd Error" = (trueValue-collectivePrediction)^2,
"Prediction Diversity" = mean((estimates-collectivePrediction)^2))
}</lang>
{{out}}
<pre>diversityStats(49, c(48, 47, 51))
True.Value Guess.1 Guess.2 Guess.3 Average.Error Crowd.Error Prediction.Diversity
1 49 48 47 51 3 0.1111111 2.888889
 
> diversityStats(49, c(48, 47, 51, 42))
True.Value Guess.1 Guess.2 Guess.3 Guess.4 Average.Error Crowd.Error Prediction.Diversity
1 49 48 47 51 42 14.5 4 10.5</pre>
=={{header|Raku}}==
(formerly Perl 6)
331

edits