alval = {{2001,1007,523}, {2002,1006,544},{2003,1009,553},{2004,1007,551},{2005,1008,529},{2006,1008,547},{2007,1008,552}};
alvnl = {{2001,126,75},{2002,126,64},{2003,126,62},{2004,126,76},{2005,126,82},{2006,126,86},{2007,126,72}};
nlval = {{2001,126,69},{2002,126,67},{2003,126,73},{2004,126,75},{2005,126,72},{2006,126,58},{2007,126,61}};
nlvnl = {{2001,1170,606},{2002,1168,639},{2003,1169,646},{2004,1169,597},{2005,1171,622},{2006,1169,635},{2007,1171,633}};
winningpercentagearray = {{year, ALvAL, ALvNL, NLvAL, NLvNL}};
For[i = 1, i ≤ Length[alval], i++, 
{
templist = {alval[[i,1]]};
templist = AppendTo[templist,100.0  alval[[i,3]] / alval[[i,2]]];
templist = AppendTo[templist,100.0  alvnl[[i,3]] / alvnl[[i,2]]];
templist = AppendTo[templist,100.0  nlval[[i,3]] / nlval[[i,2]]];
templist = AppendTo[templist,100.0  nlvnl[[i,3]] / nlvnl[[i,2]]];
winningpercentagearray = AppendTo[winningpercentagearray, templist];
}];
MatrixForm[winningpercentagearray]
(* note: don't have number of games played for 2007 so just using 2006 numbers *)
(* x v y means x is the home team and y is the away team *)

(* we first compare winning percentage of ALvAL and NLvNL *)
(* we do chisquare tests to see if the percentages are statistically significantly different *)
(* we do this for each year*)
numyears = Length[alval];
Print["Chisq values: 3.841 (95%), 6.635 (99%)."];
For[i = 1, i ≤ numyears, i++,
{
chisq = 0;
theta0 = (alval[[i,3]] + nlvnl[[i,3]]) / (1.0 alval[[i,2]] + nlvnl[[i,2]]);
chisq = ((alval[[i,3]] - alval[[i,2]]*theta0)^2 / alval[[i,2]]) +  ((nlvnl[[i,3]] - nlvnl[[i,2]]*theta0)^2 / nlvnl[[i,2]]);
chisq = chisq / (theta0 * (1 - theta0));
Print["In ", alval[[i,1]], " the average WL% = ", 100*theta0, " and chisq = ", chisq];
}];
alhomewins = Sum[alval[[i,3]],{i,1,numyears}];
nlhomewins = Sum[nlvnl[[i,3]],{i,1,numyears}];
alhomegames = Sum[alval[[i,2]], {i,1,numyears}]; 
nlhomegames = Sum[nlvnl[[i,2]], {i,1,numyears}];
theta0 = (alhomewins + nlhomewins) / (1.0 alhomegames + nlhomegames);
chisq = ((alhomewins - alhomegames*theta0)^2 / alhomegames) + ((nlhomewins - nlhomegames*theta0)^2 / nlhomegames);
chisq = chisq / (theta0 * (1 - theta0));
Print["From ", alval[[1,1]], " to ", alval[[numyears,1]], " the average WL% = ", 100*theta0, " and chisq = ", chisq];

(* this seems to suggest that the AL beat AL teams at home with the same frequency NL teams beat NL teams at home *)

(* we now compare the winning percentages of AL v NL (so AL home) and NL v AL (so AL home) *)
(* same type of analysis as above, except we also look at certain subsets *)
numyears = Length[alval];
Print["Chisq values: 3.841 (95%), 6.635 (99%)."];
For[i =1, i ≤ numyears, i++,
{
chisq = 0;
theta0 = (alvnl[[i,3]] + nlval[[i,3]]) / (1.0 alvnl[[i,2]] + nlval[[i,2]]);
chisq = ((alvnl[[i,3]] - alvnl[[i,2]]*theta0)^2 / alvnl[[i,2]]) +  ((nlval[[i,3]] - nlval[[i,2]]*theta0)^2 / nlval[[i,2]]);
chisq = chisq / (theta0 * (1 - theta0));
Print["In ", alval[[i,1]], " the average WL% = ", 100*theta0, " and chisq = ", chisq];
}];
For[yearblock = 1, yearblock ≤ 7, yearblock++,
{
Switch[yearblock, 
1,yearsdo = {2001, 2002, 2003, 2004, 2005, 2006,2007},
2, yearsdo = {2001, 2002, 2003},
3, yearsdo = {2002, 2003},
4, yearsdo = {2004,2005,2006,2007},
5, yearsdo = {2001,2002,2003,2004,2005,2007},
6, yearsdo = {2001, 2002, 2004, 2005, 2007},
7, yearsdo = {2004,2005,2007}];
alhomewins = Sum[alvnl[[i,3]] *If[MemberQ[yearsdo,alvnl[[i,1]]] == True,1,0],{i,1,numyears}];
nlhomewins = Sum[nlval[[i,3]] *If[MemberQ[yearsdo,alvnl[[i,1]]] == True,1,0],{i,1,numyears}];
alhomegames = Sum[alvnl[[i,2]] *If[MemberQ[yearsdo,alvnl[[i,1]]] == True,1,0], {i,1,numyears}]; 
nlhomegames = Sum[nlval[[i,2]] *If[MemberQ[yearsdo,alvnl[[i,1]]] == True,1,0], {i,1,numyears}];
theta0 = (alhomewins + nlhomewins) / (1.0 alhomegames + nlhomegames);
chisq = ((alhomewins - alhomegames*theta0)^2 / alhomegames) + ((nlhomewins - nlhomegames*theta0)^2 / nlhomegames);
chisq = chisq / (theta0 * (1 - theta0));
(*Print["From ", alval[[1,1]], " to ", alval[[numyears,1]], " the average WL% = ", 100*theta0, " and chisq = ", chisq];*)
Print["Average home WL% = ", 100*theta0, " and chisq = ", chisq, ". Years are ", yearsdo];
}];