<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Scripting Help Requested - Making a simulator for Type 1 errors in equivalence testing in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Scripting-Help-Requested-Making-a-simulator-for-Type-1-errors-in/m-p/915460#M107574</link>
    <description>&lt;P&gt;Which part are you stuck with?&lt;/P&gt;</description>
    <pubDate>Wed, 26 Nov 2025 17:55:29 GMT</pubDate>
    <dc:creator>jthi</dc:creator>
    <dc:date>2025-11-26T17:55:29Z</dc:date>
    <item>
      <title>Scripting Help Requested - Making a simulator for Type 1 errors in equivalence testing</title>
      <link>https://community.jmp.com/t5/Discussions/Scripting-Help-Requested-Making-a-simulator-for-Type-1-errors-in/m-p/915439#M107573</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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).&amp;nbsp; 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.&amp;nbsp; The goal of this tool will be to give a probability of type 1 errors given a certain equivalence margin.&lt;/P&gt;
&lt;P&gt;I've landed on the following code but this is not working and I'm pretty much stuck.&amp;nbsp; Any direction/help that the group can provide would be much appreciated.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;Chris&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;
// 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 &amp;lt;= 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 &amp;lt;= nPerGroup, i++,
        dt:Group[i] = "Reference";
        dt:Value[i] = Random Normal(refMean, sd);
    );

    // Simulate Test group
    For(i = nPerGroup + 1, i &amp;lt;= nPerGroup * 2, i++,
        dt:Group[i] = "Test";
        dt:Value[i] = Random Normal(testMean, sd);
    );

    // Extract values
    refVals = dt &amp;lt;&amp;lt; Get As Matrix( :Value )[1::nPerGroup];
    testVals = dt &amp;lt;&amp;lt; 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 &amp;gt; -equivMargin &amp;amp; ciUpper &amp;lt; equivMargin,
        type1Errors++;
        eqResult = "Yes";
    );

    // Log results
    summaryTable &amp;lt;&amp;lt; 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 &amp;lt;&amp;lt; 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)) || "%")
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Nov 2025 16:46:28 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Scripting-Help-Requested-Making-a-simulator-for-Type-1-errors-in/m-p/915439#M107573</guid>
      <dc:creator>ClusterFerret68</dc:creator>
      <dc:date>2025-11-26T16:46:28Z</dc:date>
    </item>
    <item>
      <title>Re: Scripting Help Requested - Making a simulator for Type 1 errors in equivalence testing</title>
      <link>https://community.jmp.com/t5/Discussions/Scripting-Help-Requested-Making-a-simulator-for-Type-1-errors-in/m-p/915460#M107574</link>
      <description>&lt;P&gt;Which part are you stuck with?&lt;/P&gt;</description>
      <pubDate>Wed, 26 Nov 2025 17:55:29 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Scripting-Help-Requested-Making-a-simulator-for-Type-1-errors-in/m-p/915460#M107574</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2025-11-26T17:55:29Z</dc:date>
    </item>
    <item>
      <title>Re: Scripting Help Requested - Making a simulator for Type 1 errors in equivalence testing</title>
      <link>https://community.jmp.com/t5/Discussions/Scripting-Help-Requested-Making-a-simulator-for-Type-1-errors-in/m-p/915480#M107577</link>
      <description>&lt;P&gt;H Jarmo,&lt;/P&gt;
&lt;P&gt;At the moment the code breaks trying to extract the critical T values from the equivalence report.&amp;nbsp; But there are other issues as well.&amp;nbsp; I've had it in a state where it will run the simulation and equivalence tests but not be able to get values out of the report.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've tried throwing some of this at AI as well but I think some of the syntax and coding conventions might be incorrect.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Chris&lt;/P&gt;</description>
      <pubDate>Wed, 26 Nov 2025 20:55:09 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Scripting-Help-Requested-Making-a-simulator-for-Type-1-errors-in/m-p/915480#M107577</guid>
      <dc:creator>ClusterFerret68</dc:creator>
      <dc:date>2025-11-26T20:55:09Z</dc:date>
    </item>
    <item>
      <title>Re: Scripting Help Requested - Making a simulator for Type 1 errors in equivalence testing</title>
      <link>https://community.jmp.com/t5/Discussions/Scripting-Help-Requested-Making-a-simulator-for-Type-1-errors-in/m-p/915484#M107580</link>
      <description>&lt;P&gt;I would suggest these edits:&lt;/P&gt;
&lt;P&gt;There's no StudentTQuantile() function.&amp;nbsp; That should be t Quantile().&lt;/P&gt;
&lt;P&gt;Variance() is not a built-in function.&amp;nbsp; Define it in your script (or just square the Std Dev() instead).&lt;/P&gt;
&lt;P&gt;refVals = dt &amp;lt;&amp;lt; Get As Matrix( :Value )[1::nPerGroup] should be more like&amp;nbsp;refVals = dt:Value[Where( :Group == "Reference" )];&lt;/P&gt;
&lt;P&gt;Same for testVals =&lt;/P&gt;
&lt;P&gt;Set the visibility for dt to "Private" to speed things up greatly.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Variance = Function( {Values},
	Return( Std Dev( Values ) ^ 2 )
);
// Parameters
numSimulations = 1000;
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 = t Quantile( 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 &amp;lt;= numSimulations, sim++, 

    // Create new data table
	dt = New Table( "Sim" || Char( sim ), "Private", Add Rows( nPerGroup * 2 ), New Column( "Group", Character ), New Column( "Value", Numeric ) );

    // Simulate Reference group
	For( i = 1, i &amp;lt;= nPerGroup, i++,
		dt:Group[i] = "Reference";
		dt:Value[i] = Random Normal( refMean, sd );
	);

    // Simulate Test group
	For( i = nPerGroup + 1, i &amp;lt;= nPerGroup * 2, i++,
		dt:Group[i] = "Test";
		dt:Value[i] = Random Normal( testMean, sd );
	);

    // Extract values
	refVals = dt:Value[Where( :Group == "Reference" )];
	testVals = dt:Value[Where( :Group == "Test" )];
	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 &amp;gt; -equivMargin &amp;amp; ciUpper &amp;lt; equivMargin,
		type1Errors++;
		eqResult = "Yes";
	);

    // Log results
	summaryTable &amp;lt;&amp;lt; 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 &amp;lt;&amp;lt; 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 ) ) || "%" )
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Nov 2025 21:56:23 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Scripting-Help-Requested-Making-a-simulator-for-Type-1-errors-in/m-p/915484#M107580</guid>
      <dc:creator>mmarchandFSLR</dc:creator>
      <dc:date>2025-11-26T21:56:23Z</dc:date>
    </item>
    <item>
      <title>Re: Scripting Help Requested - Making a simulator for Type 1 errors in equivalence testing</title>
      <link>https://community.jmp.com/t5/Discussions/Scripting-Help-Requested-Making-a-simulator-for-Type-1-errors-in/m-p/918077#M107775</link>
      <description>&lt;P&gt;Jarmo,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for the suggestion and explanation.&amp;nbsp; This definitely works!&lt;/P&gt;
&lt;P&gt;I am going to work through the code to try to internalize how everything works...Appreciate your help!&lt;/P&gt;
&lt;P&gt;Chris&lt;/P&gt;</description>
      <pubDate>Mon, 08 Dec 2025 13:16:39 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Scripting-Help-Requested-Making-a-simulator-for-Type-1-errors-in/m-p/918077#M107775</guid>
      <dc:creator>ClusterFerret68</dc:creator>
      <dc:date>2025-12-08T13:16:39Z</dc:date>
    </item>
    <item>
      <title>Re: Scripting Help Requested - Making a simulator for Type 1 errors in equivalence testing</title>
      <link>https://community.jmp.com/t5/Discussions/Scripting-Help-Requested-Making-a-simulator-for-Type-1-errors-in/m-p/918090#M107776</link>
      <description>&lt;P&gt;The original code quite strongly looks like to be created by LLM (due to obvious errors they make if you know some JSL).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Working version has already been provided but here is how I would approach this. This is slightly more complicated version using matrices (generally they are faster) and I haven't verified the calculations&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

numSimulations = 1000;
nPerGroup = 3;
refMean = 99.1;
testMean = 98.8;
sd = 0.1;
equivMargin = 0.3;
type1Errors = 0;

df = 2 * nPerGroup - 2;
tCrit = t Quantile(0.950, df);


dt = 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),
    Invisible
);


For(sim = 1, sim &amp;lt;= numSimulations, sim++,
	m_ref = J(nperGroup, 1, Random Normal(refMean, sd));
	m_test = J(nperGroup, 1, Random Normal(testMean, sd));

	avgdiff = Mean(m_test) - Mean(m_ref);
	
	var_ref = Std Dev(m_ref)^2;
	var_test = Std Dev(m_test)^2;

    pooledVar = ((nPerGroup - 1) * var_ref + (nPerGroup - 1) * var_test) / df;
    pooledSD = Sqrt(pooledVar);

    seDiff = pooledSD * Sqrt(2 / nPerGroup);

    ciLower = avgdiff - tCrit * seDiff;
    ciUpper = avgdiff + tCrit * seDiff;

    eqResult = "No";
    If(ciLower &amp;gt; -equivMargin &amp;amp; ciUpper &amp;lt; equivMargin,
        type1Errors++;
        eqResult = "Yes";
    );

	dt &amp;lt;&amp;lt; Add Rows(
		{Simulation = sim, CI_Lower = ciLower, CI_Upper = ciUpper, Equivalence_Concluded = eqResult, Mean_Diff = avgdiff}
	);
);


dt &amp;lt;&amp;lt; Show Window(1);

nw = 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)) || "%")
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Dec 2025 13:53:08 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Scripting-Help-Requested-Making-a-simulator-for-Type-1-errors-in/m-p/918090#M107776</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2025-12-08T13:53:08Z</dc:date>
    </item>
  </channel>
</rss>

