// Numbas version: exam_results_page_options {"name": "Linear regression and correlation - pub sales", "extensions": ["stats", "jsxgraph", "random_person"], "custom_part_types": [], "resources": [], "navigation": {"allowregen": true, "showfrontpage": false, "preventleave": false, "typeendtoleave": false}, "question_groups": [{"pickingStrategy": "all-ordered", "questions": [{"name": "Linear regression and correlation - pub sales", "tags": [], "metadata": {"description": "

Conduct a linear regression analysis using a spreadsheet or stats program.

", "licence": "Creative Commons Attribution-ShareAlike 4.0 International"}, "statement": "

{owner['name']} owns the {pub}; {owner['pronouns']['they']} believes that sales of {beverage} in the pub are linked to the average monthly temperature, with higher sales being recorded in months with higher temperatures. To investigate, {owner['name']} records the average monthly temperature in the local town over a period of one year ($x$, in units of $^\\circ$C), along with total monthly sales of {beverage} ($y$, in units of £1000). The results are shown in the table below: 

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Monthxy
{obj[0]}{r1[0]}{r2[0]}
{obj[1]}{r1[1]}{r2[1]}
{obj[2]}{r1[2]}{r2[2]}
{obj[3]}{r1[3]}{r2[3]}
{obj[4]}{r1[4]}{r2[4]}
{obj[5]}{r1[5]}{r2[5]}
{obj[6]}{r1[6]}{r2[6]}
{obj[7]}{r1[7]}{r2[7]}
{obj[8]}{r1[8]}{r2[8]}
{obj[9]}{r1[9]}{r2[9]}
{obj[10]}{r1[10]}{r2[10]}
{obj[11]}{r1[11]}{r2[11]}
", "advice": "

Review linear correlation and regression (e.g., Chapter 8 in OpenIntro Statistics).

\n

Part a) and b): use Excel's CORREL function or Minitab's regression analysis.

\n

Part c): The regression line has equation:

\n

$\\simplify[all,!collectNumbers]{y={b0}+{b1}x}$ and this is displayed below:  

\n

{regfun(r1,r2,max(r1)+10,max(r2)+10,rsquared,sumr)}

\n

Part d):

\n

Predicted sales when $x=\\var{thisval}^{\\small o}$C:

\n

\\[\\begin{align} y&=\\simplify[all,!collectNumbers]{{b0}+{b1}* {thisval}}\\\\
&=\\var{{b0+b1*thisval}}\\\\
&=\\var{prediction}
\\end{align}\\] in thousands of pounds.

", "rulesets": {"std": ["all", "fractionNumbers", "!collectNumbers", "!noLeadingMinus"]}, "extensions": ["jsxgraph", "random_person", "stats"], "variables": {"tol": {"name": "tol", "group": "Ungrouped variables", "definition": "0.05", "description": "", "templateType": "anything"}, "r1": {"name": "r1", "group": "Ungrouped variables", "definition": "[random(0..6),random(1..7),random(4..10),random(7..13),random(12..18),random(4..20),random(16..22),random(19..25),random(17..23),random(13..19),random(8..14),random(3..9)]", "description": "", "templateType": "anything"}, "spxy": {"name": "spxy", "group": "Ungrouped variables", "definition": "sxy-t[0]*t[1]/n", "description": "", "templateType": "anything"}, "b0": {"name": "b0", "group": "Ungrouped variables", "definition": "siground(1/n*(t[1]-spxy/ss[0]*t[0]),3)", "description": "", "templateType": "anything"}, "tcorr": {"name": "tcorr", "group": "Ungrouped variables", "definition": "spxy/sqrt(ss[0]*ss[1])", "description": "", "templateType": "anything"}, "ssq": {"name": "ssq", "group": "Ungrouped variables", "definition": "[sum(map(x^2,x,r1)),sum(map(x^2,x,r2))]", "description": "", "templateType": "anything"}, "prediction": {"name": "prediction", "group": "Ungrouped variables", "definition": "round(b0+b1*thisval)", "description": "", "templateType": "anything"}, "t": {"name": "t", "group": "Ungrouped variables", "definition": "[sum(r1),sum(r2)]", "description": "", "templateType": "anything"}, "a": {"name": "a", "group": "Ungrouped variables", "definition": "random(10..20)", "description": "", "templateType": "anything"}, "sumr": {"name": "sumr", "group": "Ungrouped variables", "definition": "precround(sum(map(res[x]^2,x,0..n-1)),3)", "description": "", "templateType": "anything"}, "beverage": {"name": "beverage", "group": "Ungrouped variables", "definition": "random(\"home-brewed beer\",\"home-brewed lager\",\"specially-brewed beer\",\"super-strength lager\",\"cold-filtered lager\",\"ice-filtered cider\",\"cherry cider\")", "description": "", "templateType": "anything"}, "n": {"name": "n", "group": "Ungrouped variables", "definition": "12", "description": "", "templateType": "anything"}, "ss": {"name": "ss", "group": "Ungrouped variables", "definition": "[ssq[0]-t[0]^2/n,ssq[1]-t[1]^2/n]", "description": "", "templateType": "anything"}, "corr": {"name": "corr", "group": "Ungrouped variables", "definition": "siground(tcorr,3)", "description": "", "templateType": "anything"}, "b1": {"name": "b1", "group": "Ungrouped variables", "definition": "siground(spxy/ss[0],3)", "description": "", "templateType": "anything"}, "pub": {"name": "pub", "group": "Ungrouped variables", "definition": "random(\"Black Bull Inn\",\"County Inn\",\"Dog and Duck Pub\",\"Slug and Lettuce Pub\", \"Cross Keys Pub\",\"Newcastle Arms Pub\",\"Red Lion Pub\")", "description": "", "templateType": "anything"}, "res": {"name": "res", "group": "Ungrouped variables", "definition": "map(precround(r2[x]-(b0+b*r1[x]),2),x,0..n-1)", "description": "", "templateType": "anything"}, "ch": {"name": "ch", "group": "Ungrouped variables", "definition": "random(0..11)", "description": "", "templateType": "anything"}, "sc": {"name": "sc", "group": "Ungrouped variables", "definition": "r1[ch]", "description": "", "templateType": "anything"}, "b": {"name": "b", "group": "Ungrouped variables", "definition": "random(0.25..0.45#0.05)", "description": "", "templateType": "anything"}, "sxy": {"name": "sxy", "group": "Ungrouped variables", "definition": "sum(map(r1[x]*r2[x],x,0..n-1))", "description": "", "templateType": "anything"}, "thisval": {"name": "thisval", "group": "Ungrouped variables", "definition": "random(15..22)", "description": "", "templateType": "anything"}, "obj": {"name": "obj", "group": "Ungrouped variables", "definition": "['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']", "description": "", "templateType": "anything"}, "owner": {"name": "owner", "group": "Ungrouped variables", "definition": "random_person()", "description": "", "templateType": "anything"}, "r2": {"name": "r2", "group": "Ungrouped variables", "definition": "map(round(a+b*x+random(-9..9)),x,r1)", "description": "", "templateType": "anything"}, "ls": {"name": "ls", "group": "Ungrouped variables", "definition": "precround(b0+b1*sc,2)", "description": "", "templateType": "anything"}, "tsqovern": {"name": "tsqovern", "group": "Ungrouped variables", "definition": "[t[0]^2/n,t[1]^2/n]", "description": "", "templateType": "anything"}, "rsquared": {"name": "rsquared", "group": "Ungrouped variables", "definition": "siground(corr^2,3)", "description": "", "templateType": "anything"}}, "variablesTest": {"condition": "", "maxRuns": 100}, "ungrouped_variables": ["ch", "prediction", "b", "owner", "sxy", "res", "spxy", "ls", "tol", "tcorr", "b0", "ssq", "sumr", "pub", "a", "thisval", "corr", "tsqovern", "b1", "obj", "r1", "r2", "ss", "n", "beverage", "t", "sc", "rsquared"], "variable_groups": [], "functions": {"regressline": {"parameters": [["r1", "list"], ["r2", "list"], ["minx", "number"], ["maxx", "number"], ["miny", "number"], ["maxy", "number"]], "type": "html", "language": "javascript", "definition": "\n var div = Numbas.extensions.jsxgraph.makeBoard('600px','600px',\n{boundingBox:[-5,maxy,maxx,-5],\n axis:true,\n showNavigation:false,\n grid:false});\n var board = div.board; \nvar l1=board.create('text',[maxx/2,-2,'Temperature']);\nvar l2=board.create('text',[-2,maxy/2,'Sales']);\nvar names = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];\nfor (j=0;j<12;j++){ board.create('point',[r1[j],r2[j]],{fixed:true, style:3,name:'\\\\\\\\['+names[j]+'\\\\\\\\]'})};\nvar a1 = board.create('point',[minx+5,miny+5],{color:'blue'});\nvar b1 = board.create('point',[minx+7,miny+5],{color:'blue'});\nfunction updr(a,b){\n var s=0;\n for(var i=0;i<12;i++){\ns=s+Math.pow(r2[i]-a*r1[i]-b,2);}\ns=Numbas.math.niceNumber(Numbas.math.precround(s,2));\n$('#rsquared').text(s);}\n var li=board.create('line',[a1,b1], {straightFirst:false, straightLast:false});\n var a=0;\n var b=0;\n function dr(p){\n p.on('drag',function(){\n a = Numbas.math.niceNumber((b1.Y()-a1.Y())/(b1.X()-a1.X()));\n b = Numbas.math.niceNumber((a1.Y()*b1.X()-a1.X()*b1.Y())/(b1.X()-a1.X()));\n Numbas.exam.currentQuestion.parts[1].gaps[0].display.studentAnswer(a);\n Numbas.exam.currentQuestion.parts[1].gaps[1].display.studentAnswer(b);\n updr(a,b);\n })};\n dr(a1);\n dr(b1);\n \nreturn div;\n\n \n"}, "regfun": {"parameters": [["r1", "list"], ["r2", "list"], ["maxx", "number"], ["maxy", "number"], ["rsquared", "number"], ["sumr", "number"]], "type": "html", "language": "javascript", "definition": "\n var div = Numbas.extensions.jsxgraph.makeBoard('600px','600px',\n{boundingBox:[-5,maxy,maxx,-5],\n axis:true,\n showNavigation:false,\n grid:true});\n var board = div.board; \nvar l1=board.create('text',[maxx/2,-2,'Temperature']);\nvar l2=board.create('text',[-2,maxy/2,'Sales']);\n var names = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];\n for (j=0;j<12;j++){ board.create('point',[r1[j],r2[j]],{fixed:true, style:3, strokecolor:\"#0000a0\", name:'\\\\\\\\['+names[j]+'\\\\\\\\]'})};\nvar regressionPolynomial = JXG.Math.Numerics.regressionPolynomial(1, r1, r2);\nvar reg = board.create('functiongraph',[regressionPolynomial],{strokeColor:'blue',name:'Regression Line.',withLabel:true}); \n //for(var i=0;i<12;i++){board.create(\"segment\",[[r1[i],r2[i]],[r1[i],regressionPolynomial(r1[i])]])};\nvar regExpression = regressionPolynomial.getTerm();\nvar regTeX = Numbas.jme.display.exprToLaTeX(regExpression,[],scope);\n\n//var t = board.create('text',[1,5,\n//function(){ return \"\\\\[r(Y) = \" + regExpression +'\\\\]';}\n//],\n//{strokeColor:'black',fontSize:18}); \n\nreturn div;\n \n"}}, "preamble": {"js": "", "css": ""}, "parts": [{"type": "gapfill", "useCustomName": false, "customName": "", "marks": 0, "scripts": {}, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "showCorrectAnswer": true, "showFeedbackIcon": true, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "nextParts": [], "suggestGoingBack": false, "adaptiveMarkingPenalty": 0, "exploreObjective": null, "prompt": "

Use a spreadsheet or stats software to obtain the correlation coefficient $R$ between the two variables:

\n

$R=\\;$[[0]]

", "gaps": [{"type": "numberentry", "useCustomName": false, "customName": "", "marks": "3", "scripts": {}, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "showCorrectAnswer": true, "showFeedbackIcon": true, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "nextParts": [], "suggestGoingBack": false, "adaptiveMarkingPenalty": 0, "exploreObjective": null, "minValue": "corr*(1-tol)", "maxValue": "corr*(1+tol)", "correctAnswerFraction": false, "allowFractions": false, "mustBeReduced": false, "mustBeReducedPC": 0, "showFractionHint": true, "notationStyles": ["plain", "en", "si-en"], "correctAnswerStyle": "plain"}], "sortAnswers": false}, {"type": "gapfill", "useCustomName": false, "customName": "", "marks": 0, "scripts": {}, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "showCorrectAnswer": true, "showFeedbackIcon": true, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "nextParts": [], "suggestGoingBack": false, "adaptiveMarkingPenalty": 0, "exploreObjective": null, "prompt": "

What is the corresponding $R^2$?

\n

$R^2=\\;$[[0]]

", "gaps": [{"type": "numberentry", "useCustomName": false, "customName": "", "marks": "1", "scripts": {}, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "showCorrectAnswer": true, "showFeedbackIcon": true, "variableReplacements": [{"variable": "corr", "part": "p0g0", "must_go_first": false}], "variableReplacementStrategy": "originalfirst", "nextParts": [], "suggestGoingBack": false, "adaptiveMarkingPenalty": 0, "exploreObjective": null, "minValue": "rsquared*(1-tol)", "maxValue": "rsquared*(1+tol)", "correctAnswerFraction": false, "allowFractions": false, "mustBeReduced": false, "mustBeReducedPC": 0, "showFractionHint": true, "notationStyles": ["plain", "en", "si-en"], "correctAnswerStyle": "plain"}], "sortAnswers": false}, {"type": "gapfill", "useCustomName": false, "customName": "", "marks": 0, "scripts": {}, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "showCorrectAnswer": true, "showFeedbackIcon": true, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "nextParts": [], "suggestGoingBack": false, "adaptiveMarkingPenalty": 0, "exploreObjective": null, "prompt": "

Use a spreadsheet or stats software to obtain the regression model $y = b_0 + b_1 \\times x$:

\n

$b_0=\\;$[[0]]k£   

\n

$b_1=\\;$[[1]]k£ / $^\\circ$C  

", "gaps": [{"type": "numberentry", "useCustomName": false, "customName": "", "marks": 2, "scripts": {}, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "showCorrectAnswer": true, "showFeedbackIcon": true, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "nextParts": [], "suggestGoingBack": false, "adaptiveMarkingPenalty": 0, "exploreObjective": null, "minValue": "b0*(1-tol)", "maxValue": "b0*(1+tol)", "correctAnswerFraction": false, "allowFractions": false, "mustBeReduced": false, "mustBeReducedPC": 0, "showFractionHint": true, "notationStyles": ["plain", "en", "si-en"], "correctAnswerStyle": "plain"}, {"type": "numberentry", "useCustomName": false, "customName": "", "marks": 2, "scripts": {}, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "showCorrectAnswer": true, "showFeedbackIcon": true, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "nextParts": [], "suggestGoingBack": false, "adaptiveMarkingPenalty": 0, "exploreObjective": null, "minValue": "b1*(1-tol)", "maxValue": "b1*(1+tol)", "correctAnswerFraction": false, "allowFractions": false, "mustBeReduced": false, "mustBeReducedPC": 0, "showFractionHint": true, "notationStyles": ["plain", "en", "si-en"], "correctAnswerStyle": "plain"}], "sortAnswers": false}, {"type": "gapfill", "useCustomName": false, "customName": "", "marks": 0, "scripts": {}, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "showCorrectAnswer": true, "showFeedbackIcon": true, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "nextParts": [], "suggestGoingBack": false, "adaptiveMarkingPenalty": 0, "exploreObjective": null, "prompt": "

Next month, the average temperature in {owner['name']}'s town is forecast to be  $\\var{thisval}^{\\small o}$C; using the regression equation you found previously, predict the corresponding value of sales:

\n

[[0]] thousands of pounds

", "gaps": [{"type": "numberentry", "useCustomName": false, "customName": "", "marks": 2, "scripts": {}, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "showCorrectAnswer": true, "showFeedbackIcon": true, "variableReplacements": [{"variable": "b0", "part": "p2g0", "must_go_first": false}, {"variable": "b1", "part": "p2g1", "must_go_first": false}], "variableReplacementStrategy": "originalfirst", "nextParts": [], "suggestGoingBack": false, "adaptiveMarkingPenalty": 0, "exploreObjective": null, "minValue": "prediction-1", "maxValue": "prediction+1", "correctAnswerFraction": false, "allowFractions": false, "mustBeReduced": false, "mustBeReducedPC": 0, "showFractionHint": true, "notationStyles": ["plain", "en", "si-en"], "correctAnswerStyle": "plain"}], "sortAnswers": false}], "partsMode": "all", "maxMarks": 0, "objectives": [], "penalties": [], "objectiveVisibility": "always", "penaltyVisibility": "always", "contributors": [{"name": "Mario Orsi", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/427/"}, {"name": "Newcastle University Mathematics and Statistics", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/697/"}]}]}], "contributors": [{"name": "Mario Orsi", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/427/"}, {"name": "Newcastle University Mathematics and Statistics", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/697/"}]}