Skip to content

Instantly share code, notes, and snippets.

Created November 18, 2016 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7a484841ab454ccd5bae0f9bf1162fc6 to your computer and use it in GitHub Desktop.
Save anonymous/7a484841ab454ccd5bae0f9bf1162fc6 to your computer and use it in GitHub Desktop.
Codea script for figuring out chi-squared equations at 95%
--# Main
-- Chi Squared
-- Use this function to perform your initial setup
function setup()
-- Write your results in the left column and your expected results in the right column
tableofresults = {
{22, 18.6},
{7, 6.2},
{4, 6.2},
{0, 2.1},
}
chisqu(tableofresults)
supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
end
function chisqu(tabler)
chi2 = 0
constanttable = {3.841, 5.991, 7.815, 9.488, 11.070,
12.592, 14.067, 15.507, 16.919, 18.307,
19.675, 21.026, 22.362, 23.685, 24.996,
26.296, 27.587, 28.869, 30.144, 31.410
}
degoffree = table.count(tabler)-1
print("Degree of Freedom: "..degoffree)
for o,c in pairs(tabler) do
for y,t in pairs(c) do
if y == 1 then
local solve = (c[1]-c[2])^2/c[2]
chi2 = chi2 + solve
end
end
end
print("x2: "..chi2)
if chi2 <= constanttable[degoffree] then
print(constanttable[degoffree])
print("accepted")
else
print(constanttable[degoffree])
print("rejected")
end
end
-- This function gets called once every frame
function draw()
background(40, 40, 50)
text("Degree of Freedom: "..degoffree, WIDTH/2, HEIGHT/2 + 20)
text("x2: "..chi2, WIDTH/2, HEIGHT/2)
if chi2 <= constanttable[degoffree] then
fill(81, 255, 0, 255)
text(constanttable[degoffree], WIDTH/2, HEIGHT/2 - 20)
text("accepted", WIDTH/2, HEIGHT/2 - 40)
else
fill(255, 0, 0, 255)
text(constanttable[degoffree], WIDTH/2, HEIGHT/2 - 20)
text("rejected", WIDTH/2, HEIGHT/2 - 40)
end
end
function table.count(tablec)
local number = 0
for i,v in pairs(tablec) do
number = number + 1
end
return number
end
function keyboard(key)
if key == RETURN then
hideKeyboard()
elseif string.match(key, "%d") then
print(string.match(key, "%d"))
end
end
function touched(touch)
showKeyboard()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment