`;
};
// --- CHI-SQUARED LOGIC ---
runChiTestBtn.addEventListener('click', () => {
const dataStr = document.getElementById('chi-test-data').value.trim();
const rows = dataStr.split(/[\n;]+/).map(row => parseData(row));
if (rows.length < 2 || rows[0].length < 2 || rows.some(r => r.length !== rows[0].length)) {
chiTestResultsDiv.innerHTML = '
`;
};
// --- PDF DOWNLOAD ---
downloadPdfBtn.addEventListener('click', () => {
if (!lastResults.type) {
alert("Please run a test before downloading results.");
return;
}
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
doc.setFontSize(22);
doc.text('Hypothesis Test Results', 105, 20, { align: 'center' });
doc.setFontSize(16);
doc.text(lastResults.type, 105, 28, { align: 'center' });
if (lastResults.type === 'T-Test') {
const { tStatistic, df, criticalT, isSignificant, mean1, mean2, n1, n2 } = lastResults.data;
doc.autoTable({
startY: 40,
head: [['Metric', 'Value']],
body: [
['Test Type', 'Independent Samples T-Test'],
['Group 1 Sample Size (n1)', n1],
['Group 1 Mean', mean1.toFixed(3)],
['Group 2 Sample Size (n2)', n2],
['Group 2 Mean', mean2.toFixed(3)],
['T-Statistic', tStatistic.toFixed(3)],
['Degrees of Freedom', df],
['Conclusion', `Statistically ${isSignificant ? 'Significant' : 'Not Significant'} (p < 0.05)`]
]
});
} else if (lastResults.type === 'Chi-Squared Test') {
const { chiSquared, df, criticalChi, isSignificant } = lastResults.data;
doc.autoTable({
startY: 40,
head: [['Metric', 'Value']],
body: [
['Test Type', 'Chi-Squared Test of Independence'],
['Chi-Squared (χ²) Value', chiSquared.toFixed(3)],
['Degrees of Freedom', df],
['Conclusion', `Statistically ${isSignificant ? 'Significant' : 'Not Significant'} (p < 0.05)`]
]
});
}
doc.save('statistical-test-results.pdf');
});
});
Error: Data must be a table with at least 2 rows and 2 columns.
'; return; } const numRows = rows.length; const numCols = rows[0].length; const rowTotals = rows.map(sum); const colTotals = Array(numCols).fill(0).map((_, i) => sum(rows.map(r => r[i]))); const grandTotal = sum(rowTotals); let chiSquared = 0; const expected = []; for (let r = 0; r < numRows; r++) { expected[r] = []; for (let c = 0; c < numCols; c++) { const expectedVal = (rowTotals[r] * colTotals[c]) / grandTotal; expected[r][c] = expectedVal; chiSquared += ((rows[r][c] - expectedVal) ** 2) / expectedVal; } } const df = (numRows - 1) * (numCols - 1); if (df <= 0) { chiTestResultsDiv.innerHTML = 'Error: Degrees of freedom must be greater than 0.
'; return; } const chi_crit_05 = { 1: 3.84, 2: 5.99, 3: 7.81, 4: 9.49, 5: 11.07, 10: 18.31, 20: 31.41 }; const criticalChi = chi_crit_05[Object.keys(chi_crit_05).reverse().find(key => df >= key)] || 3.84; const isSignificant = chiSquared > criticalChi; lastResults = { type: 'Chi-Squared Test', data: { chiSquared, df, criticalChi, isSignificant } }; renderChiTestResults(lastResults.data); }); const renderChiTestResults = (results) => { chiTestResultsDiv.innerHTML = `Chi-Squared (χ²) Value: ${results.chiSquared.toFixed(3)}
Degrees of Freedom (df): ${results.df}
Critical χ² Value (p=0.05): ${results.criticalChi}
Conclusion:
The result is statistically ${results.isSignificant ? 'significant' : 'not significant'} at p < 0.05.
This means there is ${results.isSignificant ? 'a' : 'no'} significant association between the categorical variables.
