Hi All,
I'm trying to write a script that will generate a random dataset (n=3 reference and test measurements) and then run a simulation with some number (1000 or whatever). For each set I'll run an equivalence test and I'd like the output to be a report/table with the frequency of events where equivalence was demonstrated. The goal of this tool will be to give a probability of type 1 errors given a certain equivalence margin.
I've landed on the following code but this is not working and I'm pretty much stuck. Any direction/help that the group can provide would be much appreciated.
Thanks!
Chris
// Parameters
numSimulations = 10;
nPerGroup = 3;
refMean = 99.1;
testMean = 98.8;
sd = 0.1;
equivMargin = 0.3;
type1Errors = 0;
// Critical t-value for 90% CI with df = 2n - 2
df = 2 * nPerGroup - 2;
tCrit = StudentTQuantile(0.950, df);
// Create summary table
summaryTable = New Table("Simulation Summary",
New Column("Simulation", Numeric),
New Column("CI_Lower", Numeric),
New Column("CI_Upper", Numeric),
New Column("Equivalence_Concluded", Character),
New Column("Mean_Diff", Numeric)
);
// Loop through simulations
For(sim = 1, sim <= numSimulations, sim++,
// Create new data table
dt = New Table("Sim" || Char(sim),
Add Rows(nPerGroup * 2),
New Column("Group", Character),
New Column("Value", Numeric)
);
// Simulate Reference group
For(i = 1, i <= nPerGroup, i++,
dt:Group[i] = "Reference";
dt:Value[i] = Random Normal(refMean, sd);
);
// Simulate Test group
For(i = nPerGroup + 1, i <= nPerGroup * 2, i++,
dt:Group[i] = "Test";
dt:Value[i] = Random Normal(testMean, sd);
);
// Extract values
refVals = dt << Get As Matrix( :Value )[1::nPerGroup];
testVals = dt << Get As Matrix( :Value )[nPerGroup+1::nPerGroup*2];
meanDiff = Mean(testVals) - Mean(refVals);
// Sample variances
varRef = Variance(refVals);
varTest = Variance(testVals);
// Pooled variance
pooledVar = ((nPerGroup - 1) * varRef + (nPerGroup - 1) * varTest) / df;
pooledSD = Sqrt(pooledVar);
// Standard error of difference
seDiff = pooledSD * Sqrt(2 / nPerGroup);
// 90% CI
ciLower = meanDiff - tCrit * seDiff;
ciUpper = meanDiff + tCrit * seDiff;
// Check equivalence
eqResult = "No";
If(ciLower > -equivMargin & ciUpper < equivMargin,
type1Errors++;
eqResult = "Yes";
);
// Log results
summaryTable << Add Rows(1);
summaryTable:Simulation[sim] = sim;
summaryTable:CI_Lower[sim] = ciLower;
summaryTable:CI_Upper[sim] = ciUpper;
summaryTable:Equivalence_Concluded[sim] = eqResult;
summaryTable:Mean_Diff[sim] = meanDiff;
// Close the table to avoid clutter
dt << Close Window;
);
// Show results
New Window("Type I Error Summary",
Text Box("Total Simulations: " || Char(numSimulations)),
Text Box("Type I Errors: " || Char(type1Errors)),
Text Box("Estimated Type I Error Rate: " || Char(Round(type1Errors / numSimulations * 100, 2)) || "%")
);