// Numbas version: finer_feedback_settings {"name": "Perform chi-squared test for differences in preferences", "extensions": ["stats"], "custom_part_types": [], "resources": [], "navigation": {"allowregen": true, "showfrontpage": false, "preventleave": false, "typeendtoleave": false}, "question_groups": [{"pickingStrategy": "all-ordered", "questions": [{"variable_groups": [], "variables": {"w": {"templateType": "anything", "group": "Ungrouped variables", "definition": "random(1,-1)", "description": "", "name": "w"}, "tol": {"templateType": "anything", "group": "Ungrouped variables", "definition": "0.01", "description": "", "name": "tol"}, "dh": {"templateType": "anything", "group": "Ungrouped variables", "definition": "['Retain','Reject']", "description": "", "name": "dh"}, "a": {"templateType": "anything", "group": "Ungrouped variables", "definition": "random(60..90)", "description": "", "name": "a"}, "c": {"templateType": "anything", "group": "Ungrouped variables", "definition": "random(50..69)", "description": "", "name": "c"}, "pval": {"templateType": "anything", "group": "Ungrouped variables", "definition": "switch(chi1,\"there is evidence to suggest that customers have a preference between the brands.\",\"there is no evidence to suggest that customers have a preference between the brands.\")", "description": "", "name": "correctc"}, "thislong": {"templateType": "anything", "group": "Ungrouped variables", "definition": "random(\"month\",\"week\",\"fortnight\")", "description": "", "name": "thislong"}, "crit": {"templateType": "anything", "group": "Ungrouped variables", "definition": "[precround(chisquareinv(0.9,n-1),2),precround(chisquareinv(0.95,n-1),2),precround(chisquareinv(0.99,n-1),2)]", "description": "", "name": "crit"}, "this": {"templateType": "anything", "group": "Ungrouped variables", "definition": "random(\"apple juice\", \"orange juice\",\"baked beans\",\"pasta sauce\",\"vegetarian sausages\",\"frozen yorkshire puddings\",\"microwave lasagne\",\"pineapple juice\",\"tinned tuna\",\"low-fat margarine\",\"natural greek yoghurt\",\"mature cheddar\") ", "description": "", "name": "this"}, "n": {"templateType": "anything", "group": "Ungrouped variables", "definition": "5", "description": "", "name": "n"}, "x": {"templateType": "anything", "group": "Ungrouped variables", "definition": "map(precround((5*y-t)^2/(5*t),2),y,[a,b,c,d,f])", "description": "", "name": "x"}, "b": {"templateType": "anything", "group": "Ungrouped variables", "definition": "random(60..90)", "description": "", "name": "b"}, "chi": {"templateType": "anything", "group": "Ungrouped variables", "definition": "sum(x)", "description": "", "name": "chi"}, "correcth": {"templateType": "anything", "group": "Ungrouped variables", "definition": "if(pval>1,\"we reject the null hypothesis.\",\"we retain the null hypothesis. \")", "description": "", "name": "correcth"}, "dmm": {"templateType": "anything", "group": "Ungrouped variables", "definition": "if(pval < 2,[1,0],[0,1])", "description": "", "name": "dmm"}, "mm": {"templateType": "anything", "group": "Ungrouped variables", "definition": "switch(pval=0,[1,0,0,0],pval=1,[0,1,0,0],pval=2,[0,0,1,0],[0,0,0,1])", "description": "", "name": "mm"}, "e1": {"templateType": "anything", "group": "Ungrouped variables", "definition": "t/5", "description": "", "name": "e1"}, "d": {"templateType": "anything", "group": "Ungrouped variables", "definition": "b+v*random(10..15)", "description": "", "name": "d"}, "f": {"templateType": "anything", "group": "Ungrouped variables", "definition": "a+w*random(10..15)", "description": "", "name": "f"}, "choices": {"templateType": "anything", "group": "Ungrouped variables", "definition": "['is greater than $10$%','lies between $10$% and $5$%','lies between $5$% and $1$%','is smaller than $1$%']", "description": "", "name": "choices"}, "fac": {"templateType": "anything", "group": "Ungrouped variables", "definition": "if(pval<2,\"there is evidence to suggest that customers have a preference between the brands.\",\"there is no evidence to suggest that customers have a preference between the brands.\")", "description": "", "name": "fac"}, "v": {"templateType": "anything", "group": "Ungrouped variables", "definition": "random(1,-1)", "description": "", "name": "v"}, "evi": {"templateType": "anything", "group": "Ungrouped variables", "definition": "['None','Slight','Moderate','Strong']", "description": "", "name": "evi"}, "t": {"templateType": "anything", "group": "Ungrouped variables", "definition": "a+b+c+d+f", "description": "", "name": "t"}}, "ungrouped_variables": ["pval", "crit", "n", "chi", "tol", "correctc", "dh", "correcth", "fac", "e1", "a", "evi", "c", "b", "d", "f", "mm", "dmm", "thislong", "choices", "this", "t", "w", "v", "x"], "question_groups": [{"pickingStrategy": "all-ordered", "questions": [], "name": "", "pickQuestions": 0}], "name": "Perform chi-squared test for differences in preferences", "functions": {"table1": {"type": "html", "language": "jme", "definition": "tableformat(table2(data,headers,rtot,ctot),data,c,r,rtot,ctot)", "parameters": [["data", "list"], ["headers", "list"], ["c", "boolean"], ["r", "boolean"], ["rtot", "boolean"], ["ctot", "boolean"]]}, "tableformat": {"type": "html", "language": "javascript", "definition": "\n if(c){table.find('td').css('border-right','1px solid').css('border-left','1px solid');}\n \n if(r){table.find('td').css('border-bottom','1px solid');}\n /* Put a border below the headers */\n table.find('thead').css('border-bottom','1px solid');\n /*if row totals, then split off via gridlines*/\n if(rtot){table.find('tr').find('td:last').css('border-right','1px solid').css('border-left','1px solid');}\n \n /* put a border on the right and left of the first column, assuming there are row labels. */\n table.find('tr').find('td:first').css('border-right','1px solid').css('border-left','1px solid'); \n table.find('tr').find('td:last').css('border-right','1px solid'); \n /*make sure there is a border at the bottom*/\n table.find('tr:last').css('border-bottom','1px solid'); \n \n return table;\n \n \n ", "parameters": [["table", "html"], ["data", "list"], ["c", "boolean"], ["r", "boolean"], ["rtot", "boolean"], ["ctot", "boolean"]]}, "table2": {"type": "html", "language": "javascript", "definition": "\n /* create table element */\n var table = $('
');\n /* add headers */\n for(var i=0;i').html(headers[i]));\n }\n if(rtot){ table.find('thead').append('Totals');}\n \n /*for each record in data, add a row to the table*/\n for(var i=0;i');\n var record = data[i];\n var s=0;\n for(var j=0;j').html(record[j])); \n }\n for(var j=1;j').html(s));}\n table.find('tbody').append(row);\n }\n \n /* this function adds a row of totals to the bottom of the table created by the `table` function */\n \n /* compute the totals */\n var row = $('');\n var ctotals=[];\n for(var i=0;iTotals');\n var s=0;\n for(var j=1;j').html(ctotals[j]);\n s+=ctotals[j];\n row.append(cell);\n }\n if(rtot){cell= $('').html(s); row.append(cell);}\n /* add a border to the top and bottom of the totals row */\n row.css('border-top','1px solid');\n row.css('border-bottom','1px solid');\n /* add the totals row to the table */\n table.find('tbody').append(row);\n }\n return table;\n \n \n ", "parameters": [["data", "list"], ["headers", "list"], ["rtot", "boolean"], ["ctot", "boolean"]]}}, "showQuestionGroupNames": false, "parts": [{"type": "information", "prompt": "\n

Step 1: Null hypothesis

\n

$\\operatorname{H}_0:\\;$ Customers do not have a preference for a particular brand of {this}.

\n

Step 2: Alternative hypothesis

\n

$\\operatorname{H}_1:\\;$ Customers do have a preference for a particular brand of {this}.

\n

 

\n ", "showCorrectAnswer": true, "scripts": {}, "marks": 0}, {"scripts": {}, "gaps": [{"showCorrectAnswer": true, "allowFractions": false, "scripts": {}, "type": "numberentry", "maxValue": "e1", "minValue": "e1", "correctAnswerFraction": false, "marks": 0.2, "showPrecisionHint": false}, {"showCorrectAnswer": true, "allowFractions": false, "scripts": {}, "type": "numberentry", "maxValue": "x[0]+tol", "minValue": "x[0]-tol", "correctAnswerFraction": false, "marks": 0.6, "showPrecisionHint": false}, {"showCorrectAnswer": true, "allowFractions": false, "scripts": {}, "type": "numberentry", "maxValue": "e1", "minValue": "e1", "correctAnswerFraction": false, "marks": 0.2, "showPrecisionHint": false}, {"showCorrectAnswer": true, "allowFractions": false, "scripts": {}, "type": "numberentry", "maxValue": "x[1]+tol", "minValue": "x[1]-tol", "correctAnswerFraction": false, "marks": 0.6, "showPrecisionHint": false}, {"showCorrectAnswer": true, "allowFractions": false, "scripts": {}, "type": "numberentry", "maxValue": "e1", "minValue": "e1", "correctAnswerFraction": false, "marks": 0.2, "showPrecisionHint": false}, {"showCorrectAnswer": true, "allowFractions": false, "scripts": {}, "type": "numberentry", "maxValue": "x[2]+tol", "minValue": "x[2]-tol", "correctAnswerFraction": false, "marks": 0.6, "showPrecisionHint": false}, {"showCorrectAnswer": true, "allowFractions": false, "scripts": {}, "type": "numberentry", "maxValue": "e1", "minValue": "e1", "correctAnswerFraction": false, "marks": 0.2, "showPrecisionHint": false}, {"showCorrectAnswer": true, "allowFractions": false, "scripts": {}, "type": "numberentry", "maxValue": "x[3]+tol", "minValue": "x[3]-tol", "correctAnswerFraction": false, "marks": 0.6, "showPrecisionHint": false}, {"showCorrectAnswer": true, "allowFractions": false, "scripts": {}, "type": "numberentry", "maxValue": "e1", "minValue": "e1", "correctAnswerFraction": false, "marks": 0.2, "showPrecisionHint": false}, {"showCorrectAnswer": true, "allowFractions": false, "scripts": {}, "type": "numberentry", "maxValue": "x[4]+tol", "minValue": "x[4]-tol", "correctAnswerFraction": false, "marks": 0.6, "showPrecisionHint": false}, {"showCorrectAnswer": true, "allowFractions": false, "scripts": {}, "type": "numberentry", "maxValue": "chi+tol", "minValue": "chi-tol", "correctAnswerFraction": false, "marks": 1, "showPrecisionHint": false}], "type": "gapfill", "prompt": "\n

Step 3: Test statistic

\n

Complete the following table: (input all values in the  expected column $E$  as exact decimals and input in the last column to 2 decimal places).

\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
 $O$$E$$\\displaystyle \\frac{(O-E)^2}{E}$
A$\\var{a}$[[0]][[1]]
B$\\var{b}$[[2]][[3]]
C$\\var{c}$[[4]][[5]]
D$\\var{d}$[[6]][[7]]
E$\\var{f}$[[8]][[9]]
\n

Hence the test statistic is : $\\chi^2=\\;$[[10]]

\n

Input  the test statistic to 2 decimal places.

\n ", "showCorrectAnswer": true, "marks": 0}, {"scripts": {}, "gaps": [{"showCorrectAnswer": true, "allowFractions": false, "scripts": {}, "type": "numberentry", "maxValue": "n-1", "minValue": "n-1", "correctAnswerFraction": false, "marks": 1, "showPrecisionHint": false}, {"displayType": "radiogroup", "choices": ["{choices[0]}", "{choices[1]}", "{choices[2]}", "{choices[3]}"], "displayColumns": 0, "distractors": ["", "", "", ""], "shuffleChoices": false, "scripts": {}, "minMarks": 0, "type": "1_n_2", "maxMarks": 0, "showCorrectAnswer": true, "matrix": "mm", "marks": 0}], "type": "gapfill", "prompt": "\n

Step 4:  p-value range

\n

Calculate , the degrees of freedom, for this test: $\\nu=\\;?$[[0]]

\n

Use tables to find a range for your -value.  Choose the correct choice below. 

\n

[[1]]

\n ", "showCorrectAnswer": true, "marks": 0}, {"scripts": {}, "gaps": [{"displayType": "radiogroup", "choices": ["{evi[0]}", "{evi[1]}", "{evi[2]}", "{evi[3]}"], "displayColumns": 0, "distractors": ["", "", "", ""], "shuffleChoices": false, "scripts": {}, "minMarks": 0, "type": "1_n_2", "maxMarks": 0, "showCorrectAnswer": true, "matrix": "mm", "marks": 0}, {"displayType": "radiogroup", "choices": ["Retain", "Reject"], "displayColumns": 0, "distractors": ["", ""], "shuffleChoices": false, "scripts": {}, "minMarks": 0, "type": "1_n_2", "maxMarks": 0, "showCorrectAnswer": true, "matrix": "dmm", "marks": 0}, {"displayType": "radiogroup", "choices": ["{fac}", "{correctc}"], "displayColumns": 0, "distractors": ["", ""], "shuffleChoices": true, "scripts": {}, "minMarks": 0, "type": "1_n_2", "maxMarks": 0, "showCorrectAnswer": true, "matrix": [0, 1], "marks": 0}], "type": "gapfill", "prompt": "\n

Step 5: Conclusion

\n

Given the  - value and the range you have found what is the strength of evidence against the null hypothesis?

\n

[[0]]

\n

Your Decision in relation to the null hypothesis:

\n

[[1]]

\n

Conclusion:

\n

 [[2]]

\n ", "showCorrectAnswer": true, "marks": 0}], "statement": "\n

Some marketing research studies indicate the \"positive impact of store brand penetration on store profitability as measured by market share\" (Lal,M.C.(2000). Building Store Loyalty Through Store Brands, Journal of Marketing Research, 37, no. 3, pp281). 

\n

The manager of a local supermarket that sells four national brands (A, B, C and D) and one store brand (E) of {this} wants to find out whether or not customers have a preference for a particular brand. Over the course of a {thislong}, the number of customers buying each brand of {this} was noted; the results are shown in the table below:

\n

{table1([['A',{a}],['B',{b}],['C',{c}],['D',{d}],['E',{f}]],['Brand','No. of Customers'],true,false,false,true)}

\n

Test the null hypothesis that, in fact, customers at this supermarket do not have a preference for a particular brand of {this}.

\n

 

\n ", "tags": ["checked2015", "MAS1403"], "rulesets": {}, "preamble": {"css": "", "js": ""}, "type": "question", "metadata": {"notes": "\n \t\t \t\t \t\t

Code to generate tables of different forms, with row or column totals, with or without gridlines. Very messy and assumes that there are row labels, so have to sum from the start of the data for the various sums. Also the row sums and column sums are done in different codes - need to do the column sums in the table function and leave the tableformat function  to do the rest.*

\n \t\t \t\t \t\t

*This has has been done. So table function has options to put in row and column sums.

\n \t\t \t\t \t\t

table(records,['Name','Maths','English','Science'],true,true)

\n \t\t \t\t \t\t

Boolean variables in order give:

\n \t\t \t\t \t\t

Row totals or not.

\n \t\t \t\t \t\t

Column totals or not.

\n \t\t \t\t \t\t

(If column totals are on, then the totals have top and bottom grid lines).

\n \t\t \t\t \t\t

table1(records,['Name','Maths','English','Science'],true,true,true,true)

\n \t\t \t\t \t\t

Boolean variables in order give:

\n \t\t \t\t \t\t

column grid lines or not,

\n \t\t \t\t \t\t

row grid lines or not,

\n \t\t \t\t \t\t

row totals shown or not.

\n \t\t \t\t \t\t

column totals shown or not.

\n \t\t \t\t \n \t\t \n \t\t", "licence": "Creative Commons Attribution 4.0 International", "description": "

Uses the $\\chi^2$ test to see if there is any significant difference in preferences.

"}, "variablesTest": {"condition": "", "maxRuns": 100}, "advice": "\n

Step 3

\n

 

\n

Completing the table we have $E= \\var{t}/5=\\var{t/5}$ for all brands.

\n

We do the last column calculations for brand A.

\n

$\\displaystyle \\frac{(O -E) ^ 2}{ E} = \\frac{(\\var{a} -\\var{e1}) ^ 2} {\\var{e1}} = \\var{x[0]}$

\n

to 2 decimal places.

\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
 $O$$E$$\\displaystyle \\frac{(O-E)^2}{E}$
A$\\var{a}$$\\var{e1}$$\\var{x[0]}$
B$\\var{b}$$\\var{e1}$$\\var{x[1]}$
C$\\var{c}$$\\var{e1}$$\\var{x[2]}$
D$\\var{d}$$\\var{e1}$$\\var{x[3]}$
E$\\var{f}$$\\var{e1}$$\\var{x[4]}$
   $\\chi^2=\\;\\var{chi}$
\n

The test statistic is then:

\n

\\[\\displaystyle \\chi ^ 2 = \\sum \\frac{(O -E)^2}{E} = \\var{x[0]} + \\var{x[1]} + \\var{x[2]} + \\var{x[3]} + \\var{x[4]} = \\var{chi}\\]

\n

Step 4:

\n

The degrees of freedom is given by: $\\nu$= no. of categories $- 1 = 5-1=4$

\n

The following are the critical values for $\\nu=4$.

\n

{table1([['Critical Value',{crit[0]},{crit[1]},{crit[2]}]],['p-value','10%','5%','1%'],false,false,false,false)}

\n

Looking at this test statistic we see that the p-range {choices[pval]}.

\n

The conclusion we come to is that {correctc} Hence {correcth}

\n ", "contributors": [{"name": "Newcastle University Mathematics and Statistics", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/697/"}]}]}], "contributors": [{"name": "Newcastle University Mathematics and Statistics", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/697/"}]}