// Numbas version: exam_results_page_options {"name": "Use data uploaded by the student", "extensions": ["stats"], "custom_part_types": [], "resources": [["question-resources/Data.csv", "/srv/numbas/media/question-resources/Data.csv"]], "navigation": {"allowregen": true, "showfrontpage": false, "preventleave": false, "typeendtoleave": false}, "question_groups": [{"pickingStrategy": "all-ordered", "questions": [{"name": "Use data uploaded by the student", "metadata": {"licence": "Creative Commons Attribution 4.0 International", "description": "
Get the student to upload their experimental data in a CSV file, then ask them to compute statistics on it.
"}, "extensions": ["stats"], "type": "question", "statement": "This question will only work in browsers that support the HTML5 File API! For Internet Explorer users, that means version 10 onwards.
\nHow does the length of a spring change when you suspend different masses from it? Make measurements of your spring's length when hanging various different weights from it.
\nDownload the data spreadsheet and open it in a program such as Microsoft Excel. Enter your data, save the file, and then upload it using the button below.
", "variable_groups": [], "preamble": {"css": "#data-table {\n max-height: 30em;\n overflow-y: auto;\n display: inline-block;\n}\na, a:hover {\n color: blue;\n}\na:hover {\n text-decoration: underline;\n}", "js": "// read a csv file\nfunction readCSV(text) {\n var lines = text.split('\\n');\n return lines.map(function(line){return line.split(',')});\n}\n\n// wait until the question has been generated\nquestion.signals.on('HTMLAttached',function(e) {\n var display = question.display;\n\n // set up an observable for the data array\n var _data = display.data = ko.observableArray([]);\n \n // has the student uploaded data?\n display.gotData = ko.computed(function() {\n return _data().length>0;\n });\n \n // an observable object containing various computed statistics about the data - updates automatically when the data changes\n var stats = question.stats = ko.computed(function() {\n var data = _data();\n \n // weights are in the first column\n var weights = data.map(function(r){return r[0]});\n // lengths in the second column\n var lengths = data.map(function(r){return r[1]});\n \n // how many samples?\n var n = data.length;\n \n // components of the linear regression calculation\n var sx = jStat.sum(weights);\n var sy = jStat.sum(lengths);\n var sxx = jStat.sum(weights.map(function(x){return x*x}));\n var syy = jStat.sum(lengths.map(function(x){return x*x}));\n var sxy = jStat.sum(data.map(function(r){ return r[0]*r[1]}));\n \n // coefficients of the linear regression model\n var beta = (sxy - (sx*sy)/n)/(sxx-(sx*sx)/n);\n var alpha = (sy-beta*sx)/n;\n \n var stats = {\n sx: sx,\n sy: sy,\n sxx: sxx,\n syy: syy,\n sxy: sxy,\n alpha: alpha,\n beta: beta,\n n: data.length\n };\n \n return stats;\n });\n \n // format a number to 3 d.p.\n function niceNumber(n) {\n return Numbas.math.niceNumber(n,{precisionType: 'dp', precision: 3});\n }\n \n // code to read the file when it's uploaded\n $('#file').on('change',function(e) {\n var f = e.target.files[0];\n var reader = new FileReader();\n reader.onload = function(e) {\n var data = readCSV(e.target.result);\n _data(data.map(function(row){ return row.map(parseFloat)}).filter(function(row){return !isNaN(row[0])}));\n }\n reader.readAsText(f);\n });\n});"}, "variables": {}, "tags": ["csv", "demo", "experiment", "statistics"], "parts": [{"variableReplacements": [], "showFeedbackIcon": true, "prompt": "Your data
\nWeight (N) | \nLength (cm) | \n
---|---|
\n | \n |
$n$:
\n$\\sum x$:
\n$\\sum y$:
\n$\\sum x^2$:
\n$\\sum y^2$:
\n$\\sum xy$:
\n$\\alpha$:
\n$\\beta$:
\nCompute the following statistics from your data.
\n$\\sum x$ | \n$\\sum y$ | \n$\\sum x^2$ | \n$\\sum y^2$ | \n$\\sum xy$ | \n
---|---|---|---|---|
[[0]] | \n[[1]] | \n[[2]] | \n[[3]] | \n[[4]] | \n